fix: resolve OpenAPI generation crash for Optional/Union model outputs#5636
Open
abdogomaa201099 wants to merge 1 commit into
Open
fix: resolve OpenAPI generation crash for Optional/Union model outputs#5636abdogomaa201099 wants to merge 1 commit into
abdogomaa201099 wants to merge 1 commit into
Conversation
An endpoint whose return annotation is a single Pydantic model wrapped in
`Optional[...]`, `... | None`, or `Union[...]` made `/docs.json` raise
`KeyError: 'ents/schemas/...'`. `_flatten_field` collapses the resulting
top-level `anyOf` into a bare `$ref`, then sliced the ref with a hard-coded
`#/$defs/` prefix length and looked it up in an (empty) defs map.
Make the ref-name extraction robust (`rsplit('/', 1)[-1]`) and only inline
references that exist in the local `$defs`; references to registered
component schemas (`#/components/schemas/...`) are now left intact instead
of crashing. Also route the request/response schema construction through a
helper that maps a top-level `$ref` onto `Schema`'s renamed `ref` field.
Adds regression tests covering Optional/Union/List model outputs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
An endpoint whose return annotation is a single Pydantic model wrapped in
Optional[...],... | None, orUnion[...]made/docs.jsonraiseKeyError: 'ents/schemas/...'._flatten_fieldcollapses the resulting top-levelanyOfinto a bare$ref, then sliced the ref with a hard-coded#/$defs/prefix length and looked it up in an (empty) defs map.Make the ref-name extraction robust (
rsplit('/', 1)[-1]) and only inline references that exist in the local$defs; references to registered component schemas (#/components/schemas/...) are now left intact instead of crashing. Also route the request/response schema construction through a helper that maps a top-level$refontoSchema's renamedreffield.Adds regression tests covering Optional/Union/List model outputs.
What does this PR address?
No prior issue — bug discovered while testing Optional[Model] return types.
Before submitting:
guide on how to create a pull request.
pre-commit run -ascript has passed (instructions)?those accordingly? Here are documentation guidelines and tips on writting docs.