Skip to content

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

@rolfl

Description

@rolfl

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions