fix(go): collapse nullable single-member oneOf union to pointer type#2567
Open
siemendev wants to merge 1 commit into
Open
fix(go): collapse nullable single-member oneOf union to pointer type#2567siemendev wants to merge 1 commit into
siemendev wants to merge 1 commit into
Conversation
When a JSON Schema uses `oneOf: [{type: T}, {type: null}]`, the conversion
layer correctly sets `isNullable: true` on the resulting ConstrainedUnionModel
and strips the null member, leaving a single-member union. However, the Go
generator still rendered this as a named struct type (e.g. `type Foo struct
{ string }`), producing dead anonymous wrapper types instead of idiomatic
Go pointer types.
Two-part fix:
- GoConstrainer Union handler: when isNullable=true and union has exactly
one member, return `*T` (pointer to the member type) instead of the union
type name. Multi-member nullable unions are unaffected.
- UnionRenderer defaultSelf: return empty string for the same condition so
no standalone Go type definition is emitted for the collapsed union.
Added two new test cases in GoConstrainer.spec.ts:
- nullable single-member union → `*string`
- nullable multi-member union → still uses union type name (no regression)
Related: fixes the same class of nullable-type issues as
asyncapi#2566
✅ Deploy Preview for modelina canceled.
|
|
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.



Summary
When a JSON Schema uses
oneOf: [{type: T}, {type: null}], the conversion layer already does the right thing: it setsisNullable: trueon the resultingConstrainedUnionModeland strips the null member, leaving a single-item union. But the Go generator ignored this and still rendered the union as a named struct type:The wrapper type polluted generated output and was functionally useless; consumers had to manually rewrite it to
*string/*int/etc.Changes
src/generators/go/GoConstrainer.ts— Union handler: detect the collapsed-nullable case and return*T:src/generators/go/renderers/UnionRenderer.ts—defaultSelf: return empty string for the same condition so no standalone Go type definition is emitted:Multi-member nullable unions (e.g.
oneOf: [{type: string}, {type: integer}, {type: null}]) are unaffected — they still render as named union structs.test/generators/go/GoConstrainer.spec.ts— two new Union test cases:'nullable single-member union renders as pointer to member type'→*string'nullable multi-member union still renders as union type name'→NullableMulti(no regression)Test plan
npx jest test/generators/go/GoConstrainer.spec.ts— 2 new tests passnpx jest test/generators/go/— 69 tests pass, no snapshot changesRelated
Companion to #2566 (fix nullable+required fields emitting value types instead of pointer types). Together these two PRs make Go nullable field generation correct for all combinations.