Resolve "reference resolves to more than one schema" errors when AJV processes OpenAPI document and encounters unknown properties whose values include an id
parameter.
#853
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello!
πββοΈ This PR addresses an issue with how AJV processes attributes that are outside of JSON Schema and are objects with an
id
parameter (which is treats as a special case).Background
When someone uses Stoplight Studio to manage their OpenAPI specification, the editor injects
x-stoplight
attributes into various objects in order to create a link between the document and the editor. Thisx-stoplight
attribute can look something like this:Referencing this schema in an Operation causes the middleware to throw an exception from AJV that looks something like this:
When you dig into AJV internals, you see that when AJV does not know about a particular OpenAPI specified property (beyond JSON Schema), it will treat
id
as a special case and does a validation step to make sure identifiers are not duplicated. There are other examples of this happening in Spectral where a user reports that duplicateid
parameters in examples cause the same issue.Their solution was to "de-examplify" the document before handing off to AJV; stripping examples from the document entirely for purposes of validation and error reporting in their tool.
So I went about this the same way!
This PR strips
x-stoplight
values from theOpenAPIV3.Document
any timecreateAjv
is used. Admittedly, this is fairly ham-handed and you may have a better way of getting AJV to "chill out". However, this does work as expected and I imagine for folks using Stoplight, alleviates a headache moving to 5.x+ of this package.Hehe, in fact, one of our projects is on the 4.x branch and I only became aware of this because two of my colleagues started a new project on 5.x, which upgrades AJV to 8.x.