fix: hoist nested definitions to top-level components/schemas#921
Closed
Upadhyay-Om wants to merge 1 commit intofastify:mainfrom
Closed
fix: hoist nested definitions to top-level components/schemas#921Upadhyay-Om wants to merge 1 commit intofastify:mainfrom
Upadhyay-Om wants to merge 1 commit intofastify:mainfrom
Conversation
2 tasks
|
Not had a close look at the code yet but wanted to make you aware of #676. I don't see you having the same problems but could be worth while taking a quick read of the comments. |
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.
Fixes #639
Problem
When using
addSchema()with nesteddefinitions, fastify-swagger silently deletes thedefinitionsblock during OpenAPI conversion. Any$refpointing into those definitions ends up dangling — Swagger UI throws a "could not resolve pointer" error even though Fastify's runtime handles it perfectly fine.Fix
Two things needed fixing:
definitions, each nested entry now gets hoisted up to top-levelcomponents/schemasunder the key{parentName}-{defName}. The deletion then becomes safe since nothing is lost.$refrewrite regex was too strict — it couldn't handle schema IDs that contain slashes (like URLs). Switched the capture group to a greedy match so it handles the full ID correctly.Test
Added a test in
test/spec/openapi/refs.test.jsusing the exact reproduction case from the issue — registers a schema with nesteddefinitions, references it via$refin a route, and asserts both that the hoisted schema appears incomponents/schemasand that the$refpath is rewritten correctly. All existing tests still pass at 100% coverage.Related: #676
Checklist
npm run test && npm run benchmark --if-present