fix(json-schema): use $defs instead of definitions for openapi-3.0 target#5767
Open
mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
Open
fix(json-schema): use $defs instead of definitions for openapi-3.0 target#5767mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
mahmoodhamdi wants to merge 1 commit intocolinhacks:mainfrom
Conversation
…rget When using toJSONSchema() with target: "openapi-3.0" on recursive schemas, the output contained #/definitions/ refs and a "definitions" key. This is a Swagger 2.0 / JSON Schema Draft 4 convention not recognized by OpenAPI 3.0 Schema Objects, which broke tools like Redoc that strictly follow the OAS 3.0 spec. Changed the logic to use $defs and #/$defs/ for openapi-3.0 (same as draft-2020-12), reserving "definitions" only for draft-04 and draft-07. This is more forward-compatible since OAS 3.1 uses JSON Schema 2020-12. Closes colinhacks#5693
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fixes
toJSONSchema()withtarget: "openapi-3.0"to use$defsand#/$defs/refs instead ofdefinitionsand#/definitions/for recursive schemas.Root cause
The
defsSegmentlogic into-json-schema.tsandjson-schema-processors.tsused a binary choice:$defsfordraft-2020-12,definitionsfor everything else. This meantopenapi-3.0was gettingdefinitions— a Swagger 2.0 / JSON Schema Draft 4 concept that is not a recognized keyword in OpenAPI 3.0 Schema Objects.For a recursive schema like:
The output contained
"$ref": "#/definitions/__schema0"and a"definitions"key, which broke tools like Redoc that strictly validate against the OAS 3.0 spec.Fix
Inverted the condition:
definitionsis now only used fordraft-04anddraft-07, while everything else (includingopenapi-3.0anddraft-2020-12) uses$defs. This is forward-compatible since OpenAPI 3.1 adopted JSON Schema 2020-12's$defsconvention.Changed in three places:
to-json-schema.tsline 250 —makeURIref path segmentto-json-schema.tslines 486-491 —finalizedefs key placementjson-schema-processors.tsline 653 — registrytoJSONSchemadefs segmentTests
Added a new test
"recursive schema openapi-3.0 uses $defs"that verifies:definitionskey$defskey#/$defs/prefixAll 3577 existing tests pass.
Related issue
Closes #5693