Skip to content

doesn't resolve $refs if a parent had a $ref #44

Open
@skyqrose

Description

@skyqrose

If a schema has a $ref and other fields (an extendedRef), then the other fields will be kept. However, if those other fields themselves have $refs, they will not be resolved.

Context

I'm trying to use this library to dereference a schema that uses refs like this, and it means I can't use this library on this schema, at least not without recursing through the object and resolving references myself when the library misses them.

Current Behavior

Some refs are not resolved. If the schema is dereferenced, those missing refs will cause the library to crash.

Expected Behavior

refs should always be resolved, even deep in the schema.

Possible Workaround/Solution

Not sure how to workaround this. Write my own recursion into the schema to resolve the refs when the library misses them?

I poked around in the code, and the cause (and the fix) is likely to be somewhere near a call to the function $Ref.isExtendedRef.

Steps to Reproduce

code snippet
const $RefParser = require("@stoplight/json-schema-ref-parser");

const logJson = (json) => {
  console.log(JSON.stringify(json, null, 2));
};

const opts = {
  resolve: { file: { read: () => Promise.resolve('{"const":"file"}') } },
};

async function main() {
  console.log("example 1:");
  const schema1 = JSON.parse(`
  {
    "properties": {
      "foo": {
        "$ref": "file2.json"
      }
    }
  }
  `);
  const refs1 = await $RefParser.resolve(schema1, opts);
  // logs [".../", ".../file2.json"]
  logJson(refs1.paths());
  // correctly dereferences file2.json
  const dereference1 = await $RefParser.dereference(schema1, opts);
  logJson(dereference1);

  console.log("example 2:");
  const schema2 = JSON.parse(`
  {
    "$ref": "file1.json",
    "properties": {
      "foo": {
        "$ref": "file2.json"
      }
    }
  }
  `);
  const refs2 = await $RefParser.resolve(schema2, opts);
  // logs [".../", ".../file1.json"]
  // should log [".../", ".../file1.json", ".../file2.json"]
  logJson(refs2.paths());
  // crashes with "Error resolving $ref pointer ...file2.json"
  const dereference2 = await $RefParser.dereference(schema2, opts);
  logJson(dereference2);
}

main();

Environment

Version 9.2.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