APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
942 stars 226 forks source link

Relative references not working correctly. #339

Closed alexkaufman06 closed 4 months ago

alexkaufman06 commented 5 months ago

I'm seeing the issue mentioned here with "@apidevtools/json-schema-ref-parser": "^11.5.4",: https://github.com/APIDevTools/json-schema-ref-parser/issues/251

jonluca commented 5 months ago

Can you share an example?

alexkaufman06 commented 5 months ago

Can you share an example?

Here is an example repo: https://github.com/alexkaufman06/example-issue If you clone it and run npm i followed by npm run dref:json you should see an error opening file and no such directory.

If I change the refs from ../common/components to ./common/components it works but I don't think I should have to.

alexkaufman06 commented 4 months ago

@jonluca let me know if you need anymore info. Looks like this PR attempted to fix it but didn't have tests: https://github.com/APIDevTools/json-schema-ref-parser/pull/261/files

denver-HJS commented 4 months ago

I have this same issue in my repository. @jonluca would it make sense to resurrect #261?

jonluca commented 4 months ago

@alexkaufman06 the issue with your example is that you are loading the yaml into an object in memory. This loses the information about where the file is on disk, so we have to fall back to making the relative references relative to the current working directory.

When you do

const fileContents = fs.readFileSync(`./api/${file}`, 'utf8');
const mySchema = yaml.load(fileContents);

The contents of mySchema does not contain its original location on disk - it does not know that it is in the api directory, so we now it falls back to thinking that we're in os.cwd(), which is the root folder.

I dont believe this is an issue with the library. Theres no way for us to know where the file is once you pass in an object and not a file path. If you change your code to pass in the file path instead, it works properly:

let clonedSchema = await $RefParser.dereference(./api/${file}, { mutateInputSchema: false });

CleanShot 2024-04-21 at 13 15 22@2x

CleanShot 2024-04-21 at 13 15 50@2x

jonluca commented 4 months ago

I have this same issue in my repository. @jonluca would it make sense to resurrect #261?

Does #261 solve a different issue? Could you include a separate relative referencing parsing error example?

denver-HJS commented 4 months ago

I have this same issue in my repository. @jonluca would it make sense to resurrect #261?

Does #261 solve a different issue? Could you include a separate relative referencing parsing error example?

Actually I was using the same approach as @alexkaufman06, so your suggestion addresses both of our issues/questions. Thank you for pointing us to the fix! 🙇

alexkaufman06 commented 4 months ago

Thanks for the fix @jonluca! 😅 🙇