Skip to content

Convert relative refs when resolving an external schema #322

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

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.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function inventory$Ref(
options: $RefParserOptions,
) {
const $ref = $refKey === null ? $refParent : $refParent[$refKey];
const $refPath = url.resolve(path, $ref.$ref);
const $refPath = url.resolve(pathFromRoot, $ref.$ref);
const pointer = $refs._resolve($refPath, pathFromRoot, options);
if (pointer === null) {
return;
Expand Down
25 changes: 18 additions & 7 deletions lib/resolve-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function resolveExternal(parser: $RefParser, options: Options) {
* @param path - The full path of `obj`, possibly with a JSON Pointer in the hash
* @param $refs
* @param options
* @param external - Whether `obj` was found in an external document.
* @param seen - Internal.
*
* @returns
Expand All @@ -55,7 +56,8 @@ function crawl(
path: string,
$refs: $Refs,
options: Options,
seen?: Set<any>,
external: boolean,
seen?: Set<any>
) {
seen ||= new Set();
let promises: any = [];
Expand All @@ -65,15 +67,17 @@ function crawl(
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 (const key of Object.keys(obj)) {
const keyPath = Pointer.join(path, key);
const value = obj[key] as string | JSONSchema | Buffer | undefined;

if ($Ref.isExternal$Ref(value)) {
promises.push(resolve$Ref(value, keyPath, $refs, options));
} else {
promises = promises.concat(crawl(value, keyPath, $refs, options, seen));
}
promises = promises.concat(crawl(value, keyPath, $refs, options, external, seen));
}
}
}
Expand All @@ -99,6 +103,13 @@ async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options
const resolvedPath = url.resolve(path, $ref.$ref);
const 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 @@ -112,7 +123,7 @@ async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options

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

return Promise.all(promises);
} catch (err) {
Expand Down
21 changes: 20 additions & 1 deletion lib/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform :
protocolPattern = /^(\w{2,}):\/\//i,
jsonPointerSlash = /~1/g,
jsonPointerTilde = /~0/g;
import { join } from "path";
import { join, dirname, relative } from "path";

const projectDir = join(__dirname, "..", "..");
// RegExp patterns to URL-encode special characters in local filesystem paths
Expand Down Expand Up @@ -270,3 +270,22 @@ export function safePointerToPath(pointer: any) {
return decodeURIComponent(value).replace(jsonPointerSlash, "/").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 from
* @param to
* @returns
*/
export function relative(from: string, to: string): string {
if (!isFileSystemPath(from) || !isFileSystemPath(to)) {
return resolve(from, to);
}

const fromDir = dirname(stripHash(from));
const toPath = stripHash(to);

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