Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert relative refs when resolving an external schema #220

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function crawl (parent, key, path, pathFromRoot, indirections, inventory, $refs,
*/
function inventory$Ref ($refParent, $refKey, path, pathFromRoot, indirections, inventory, $refs, options) {
let $ref = $refKey === null ? $refParent : $refParent[$refKey];
let $refPath = url.resolve(path, $ref.$ref);
let $refPath = url.resolve(pathFromRoot, $ref.$ref);
let pointer = $refs._resolve($refPath, pathFromRoot, options);
if (pointer === null) {
return;
Expand Down
24 changes: 16 additions & 8 deletions lib/resolve-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,33 @@ function resolveExternal (parser, options) {
* @param {string} path - The full path of `obj`, possibly with a JSON Pointer in the hash
* @param {$Refs} $refs
* @param {$RefParserOptions} options
* @param {boolean} external - Whether `obj` was found in an external document.
*
* @returns {Promise[]}
* Returns an array of promises. There will be one promise for each JSON reference in `obj`.
* If `obj` does not contain any JSON references, then the array will be empty.
* If any of the JSON references point to files that contain additional JSON references,
* then the corresponding promise will internally reference an array of promises.
*/
function crawl (obj, path, $refs, options) {
function crawl (obj, path, $refs, options, external) {
let promises = [];

if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) {
if ($Ref.isExternal$Ref(obj)) {
promises.push(resolve$Ref(obj, path, $refs, options));
}
else {
if (external && $Ref.is$Ref(obj)) {
/* Correct the reference in the external document so we can resolve it */
const withoutHash = url.stripHash(path);
obj.$ref = withoutHash + obj.$ref;
}

for (let key of Object.keys(obj)) {
let keyPath = Pointer.join(path, key);
let value = obj[key];

if ($Ref.isExternal$Ref(value)) {
promises.push(resolve$Ref(value, keyPath, $refs, options));
}
else {
promises = promises.concat(crawl(value, keyPath, $refs, options));
}
promises = promises.concat(crawl(value, keyPath, $refs, options, external));
}
}
}
Expand All @@ -94,6 +96,12 @@ async function resolve$Ref ($ref, path, $refs, options) {
let resolvedPath = url.resolve(path, $ref.$ref);
let withoutHash = url.stripHash(resolvedPath);

/* Correct the $ref to use a path relative to the root, so that $Refs._resolve can resolve it,
otherwise transitive relative external references will be incorrect if the second external
relative ref doesn't work relative to the root document.
*/
$ref.$ref = url.relative($refs._root$Ref.path, resolvedPath);

// Do we already have this $ref?
$ref = $refs._$refs[withoutHash];
if ($ref) {
Expand All @@ -107,7 +115,7 @@ async function resolve$Ref ($ref, path, $refs, options) {

// Crawl the parsed value
// console.log('Resolving $ref pointers in %s', withoutHash);
let promises = crawl(result, withoutHash + "#", $refs, options);
let promises = crawl(result, withoutHash + "#", $refs, options, true);

return Promise.all(promises);
}
Expand Down
21 changes: 21 additions & 0 deletions lib/util/url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

const pathModule = require("path");

let isWindows = /^win/.test(process.platform),
forwardSlashPattern = /\//g,
protocolPattern = /^(\w{2,}):\/\//i,
Expand Down Expand Up @@ -269,3 +271,22 @@ exports.safePointerToPath = function safePointerToPath (pointer) {
.replace(jsonPointerTilde, "~");
});
};

/**
* Like path.relative(from, to) but for URLs. It will return a relative
* URL if it can, otherwise an absolute URL is returned.
* @param {string} from
* @param {string} to
* @returns {string}
*/
exports.relative = function relative (from, to) {
if (!exports.isFileSystemPath(from) || !exports.isFileSystemPath(to)) {
return exports.resolve(from, to);
}

const fromDir = pathModule.dirname(exports.stripHash(from));
const toPath = exports.stripHash(to);

const result = pathModule.relative(fromDir, toPath);
return result + exports.getHash(to);
};