Skip to content

fix: hoist nested definitions to top-level components/schemas#921

Closed
Upadhyay-Om wants to merge 1 commit intofastify:mainfrom
Upadhyay-Om:fix/nested-definitions-support
Closed

fix: hoist nested definitions to top-level components/schemas#921
Upadhyay-Om wants to merge 1 commit intofastify:mainfrom
Upadhyay-Om:fix/nested-definitions-support

Conversation

@Upadhyay-Om
Copy link
Copy Markdown

Fixes #639

Problem

When using addSchema() with nested definitions, fastify-swagger silently deletes the definitions block during OpenAPI conversion. Any $ref pointing 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:

  • Before deleting definitions, each nested entry now gets hoisted up to top-level components/schemas under the key {parentName}-{defName}. The deletion then becomes safe since nothing is lost.
  • The $ref rewrite 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.js using the exact reproduction case from the issue — registers a schema with nested definitions, references it via $ref in a route, and asserts both that the hoisted schema appears in components/schemas and that the $ref path is rewritten correctly. All existing tests still pass at 100% coverage.

Related: #676

Checklist

@mikaelkaron
Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON Schema Definitions not work

2 participants