Skip to content

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

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

Upadhyay-Om wants to merge 1 commit into
fastify: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

@Upadhyay-Om Upadhyay-Om mentioned this pull request Apr 18, 2026
2 tasks done
@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.

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON Schema Definitions not work

2 participants