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

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

Open
skyqrose opened this issue Jun 9, 2023 · 0 comments · May be fixed by #45
Open

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

skyqrose opened this issue Jun 9, 2023 · 0 comments · May be fixed by #45

Comments

@skyqrose
Copy link

skyqrose commented Jun 9, 2023

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

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

Successfully merging a pull request may close this issue.

1 participant