-
-
Notifications
You must be signed in to change notification settings - Fork 234
fix bundle producing schema with corrupted refs in some cases #62
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
fix bundle producing schema with corrupted refs in some cases #62
Conversation
Coverage increased (+0.04%) to 94.842% when pulling 7b55cfd0f7f2f5287ac338a062592dc9fcbfab2d on RomanGotsiy:fix-broken-refs into 707e626 on BigstickCarpet:master. |
Coverage increased (+0.04%) to 94.842% when pulling 7b55cfd0f7f2f5287ac338a062592dc9fcbfab2d on RomanGotsiy:fix-broken-refs into 707e626 on BigstickCarpet:master. |
Sorry, just noticed that the fix doesn't solve all the cases. I've investigated a bit deeper and it seems something is wrong with sorting here: https://github.com/BigstickCarpet/json-schema-ref-parser/blob/master/lib/bundle.js#L161 |
Coverage increased (+0.007%) to 94.813% when pulling ad183641e760f5e8cdf280ed6fad06886d057684 on RomanGotsiy:fix-broken-refs into 707e626 on BigstickCarpet:master. |
Seems that sorting is not stable. Check out the following two screen-shots of here is same spec, just with one more reference added: Check the order of item 3 and 4 at the first screenshot and 4 and 5 at the second: while all the details are the same except I wasn't able to minimize this use case to small reproducible sample (as it depends somehow on refs number in the array and is just crazy). But here is a repo with the spec that reproduces issue: https://github.com/mikunn/openapi-test-refs Sorry, maybe I am not clear enough, but this issue is hard to explain for me :( |
Thanks for finding and debugging this, @RomanGotsiy. I'll take a look at it as soon as I get a chance (probably next week). As long as it doesn't break existing tests/functionality, I'll merge it. |
🙌 , did you have a chance to look into this? |
Sorry, @RomanGotsiy - This week has been crazy. But I'm off next week for the holidays, so I'll have time to take a look at this. |
From what I figured, and what works in my case, a simple fix is needed.
(Sorry, I'm not very familiar with pull requests) |
@RomanGotsiy and @VatroslavByte, thank you both for your input on this. Here's my analysis: The behavior was actually by design. The If you look at @RomanGotsiy's example. The bundled output is actually valid: {
"paths": {
"$ref": "#/components"
},
"external": {
"go": {
"deeper": {
"$ref": "#/paths/test" // <--- This is valid
}
}
},
"components": {
"test": {
"type": "string"
}
}
} The reference to @RomanGotsiy's PR produces a direct reference in this particular scenario, which is an improvement. However, the logic relies on the sequence order of the nodes in the document, so it can still produce indirect references depending on the order of nodes. I'll experiment with the code a bit today and see if I can come up with a more reliable method of preventing indirect references. This PR serves as a good starting point for that. 👍 |
…inimize the number of indirect references in the bundled output (see #62 (comment))
@RomanGotsiy and @VatroslavByte - 👉 I need your help to test this, please 👈 I've modified the I've published a beta version of
Please give this a try and let me know if it works for your use-cases. I really appreciate your help and feedback. |
The beta didn't fix the issue I was having. The problem is that I was referencing objects through root json file, rather than directly referencing the json file where the object actually is. The behavior that I was expecting is that it would merge root json with all objects from external files, and refs in external files would be replaced with shortest path to that object in root file. Example: The funny thing is, I created an test app that is literally copy/paste of my existing app (same code, jsons and json-schema-ref-parser version), and I get the output which I described in the example above. NOTE: |
@VatroslavByte - thanks for your feedback. I tried reproducing the issue that you mentioned, but was unable to. Here is my reproduction. If you look at the output, you'll see that the reference to Perhaps I'm missing something and failing to accurately reproduce your example. If you could post reproduction code, similar to mine above, I'd really appreciate it. |
Well, I'm also having trouble reproducing it. That's why I mentioned that it works in one app and not the other. I'll try and figure out why that is and whip up an example for you these days. |
I decided to go ahead and release this change publicly as version 4.1.0, since it definitely fixes the original problem that was pointed out by @RomanGotsiy, and all of my tests show that it improves the algorithm without breaking any existing functionality. |
Hey @BigstickCarpet, Sorry for the long reply. This seems awesome and is fixing my original problem. Thanks so much! Also, thanks for the explanation that the produced references are valid. Although I can't find anything about this behavior in specs (would be really great if you can point me to), I checked a few JSON schema implementations and they actually work fine with transitive refs like those. |
@BigstickCarpet any plans to fix this for all scenarios? Greatly appreciated. |
@brettstack - Not sure what you mean. Is there a specific scenario where it doesn't work? If so, can you post a repro? AFAIK, this fix solves the problem for all scenarios that I've tested. |
Simplest repro I could make: definitions:
Resource:
properties:
resourceId:
$ref: '#/definitions/indirectId'
indirectId:
$ref: '#/definitions/id'
id:
title: ID yields {
"definitions": {
"Resource": {
"properties": {
"resourceId": {
"$ref": "#/definitions/id"
}
}
},
"indirectId": {
"$ref": "#/definitions/id"
},
"id": {
"title": "ID"
}
}
} Expected |
@brettstack - In your example above, that's exactly the output that I would expect, especially after applying this fix. |
Thanks. What if I wanted to keep the indirect references? Is that possible? |
@brettstack - If you want to leave the references as-is, rather than re-mapping them, then just call the |
Hey @BigstickCarpet,
ReDoc users found an interesting issue in
json-schema-ref-parser
. This is hard to explain so I will just show an example. If we have two files:index.yaml
and
other.yaml
:bundle produced the following output:
It actually depends on the deep level of the ref, I believe it has something to sorting order.
I've fixed this in this PR. The fix seems reasonable to me but I'm not sure if this is correct so please check it carefully. Also added test case but not sure if the names are so good.