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

Unexpected relative path $ref behaviour when nearby absolute $ref #160

Closed
rolfl opened this issue Mar 15, 2020 · 2 comments
Closed

Unexpected relative path $ref behaviour when nearby absolute $ref #160

rolfl opened this issue Mar 15, 2020 · 2 comments

Comments

@rolfl
Copy link

rolfl commented Mar 15, 2020

Relative paths like $ref: ./somefile are expected to load the file in the same directory as the file that is being dereferenced.

This behaviour is not obvious in the example:

{
  $ref: 'mine://host/folder/path',
  root: {
    child: {
      $ref: './relativefile'
    }
  }
}

I need to find a way for the relative link ./relativefile to load a file in the same folder... but instead it looks for mine://host/folder/relativefile

When there's a "peer" $ref though in a parent element, the relative file paths become relative to the "peer" reference. This is unexpected. I am trying to find either:

  • is this a bug in the code that imposes a basePath on a relative file link

or

  • is there a way to "break out" of a peer basePath and force a relative $ref to be applies as a file ref relative to the enclosing file?

Here's a self-contained example of the behaviour... that throws the error Error: Error resolving $ref pointer "mine://host/folder/relativefile".

'use strict';

const parser = require('json-schema-ref-parser');

const doc = {
  $ref: 'mine://host/folder/path',
  root: {
    child: {
      $ref: './relativefile'
    }
  }
};

const resolver = {
  canRead: file => file.url.startsWith('mine://'),
  read: (file) => {
    if (file.url !== 'mine://host/folder/path') {
      throw new Error(`Should not be asked to handle ${JSON.stringify(file)}`);
    }
    console.warn(`resolved ${JSON.stringify(file)}`)
    return {
      mine: file.url
    };
  }
};

const prom = parser.dereference(doc, { resolve: { mine: resolver } });

prom.then(
  () => console.error('Expected an error to say ./relativefile does not exist'),
  err => console.error(err.stack)
);

The full output of the above code is:

/tmp$ node bugref.js 
resolved {"url":"mine://host/folder/path","extension":""}
Error: Error resolving $ref pointer "mine://host/folder/relativefile". 
"mine://host/folder/relativefile" not found.
    at $Refs._resolve (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/refs.js:152:11)
    at dereference$Ref (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js:99:23)
    at crawl (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js:58:26)
    at crawl (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js:64:28)
    at dereference$Ref (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js:112:24)
    at crawl (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js:46:22)
    at dereference (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js:19:22)
    at $RefParser.dereference (/tmp/node_modules/@apidevtools/json-schema-ref-parser/lib/index.js:247:5)
@JamesMessinger
Copy link
Member

Nested $refs like that aren't currently supported. See PR #146

@philsturgeon
Copy link
Member

I'm not sure this is particularly valid $ref usage anyhow. $ref and $id will be getting a bit of an overhaul for JSON Schema 2019-09 support, which is being handled by #145, so let's track all that work there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants