openapi3gen: clear nullable on exported component bodies#1164
Merged
Conversation
When a struct is reached via *T, generateSchemaRefFor sets
schema.Nullable=true to encode nullability of that one reference site.
With ExportComponentSchemas on, that same body becomes the canonical
component definition shared by every $ref site, so the nullable flag
leaks into the component itself. Codegen tools (Orval, openapi-typescript)
then emit broken types like `interface Foo {...} | null`.
Clear the flag right before registering the schema as a component.
fenollp
approved these changes
May 3, 2026
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.
Problem
When a struct is reached via
*T,generateSchemaRefForsetsschema.Nullable = trueto encode nullability of that one reference site:With
ExportComponentSchemas: true, that same*openapi3.Schemabody later becomes the canonical component definition shared by every\$refsite:So the nullable flag leaks into the component itself. Real-world example from a Fuego-based API:
Codegen tools (Orval, openapi-typescript) interpret
nullableon a component body as "the type itself can be null" and emit broken TypeScript:Fix
Clear
schema.Nullableright before registering the schema as a component. Nullability of the body is meaningless once the body is shared by every reference site; the field-level decision belongs at the\$refsite (and OpenAPI 3.0 has no clean way to mark a\$refas nullable, but that's a separate concern). Inline schemas (the fallback at the bottom of the function) keep the existing nullable behavior.Test
TestExportComponentSchemasNoNullableOnBodyregistersWrapper{ Data *Channel }, exports components, and assertsschemas["Channel"].Value.Nullable == false.The existing
ExampleThrowErrorOnCyclesnapshot is unaffected (it doesn't enableExportComponentSchemas).