Skip to content

Commit 33101e8

Browse files
mromaszewiczclaude
andcommitted
fix: add omitempty to optional nullable fields
Fixes #2091 The `omitempty` JSON tag was not being added to optional nullable fields. The condition `!p.Nullable && shouldOmitEmpty` explicitly prevented any nullable field from receiving `omitempty`, even when the field was optional (not required). This contradicted the documented behavior. The fix removes the `!p.Nullable` guard so nullable fields follow the same `omitempty` rules as non-nullable fields. The special-case exception for the `nullable-type` output option is no longer needed since the logic is now uniform. Note: `x-go-type-skip-optional-pointer: true` on a nullable field suppresses the pointer (generating `string` instead of `*string`) but still correctly receives `omitempty` when the field is optional. Whether skip-optional-pointer should be allowed to suppress the pointer on nullable fields is a separate concern. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 01d4fc0 commit 33101e8

4 files changed

Lines changed: 7 additions & 11 deletions

File tree

internal/test/issues/issue-1039/defaultbehaviour/types.gen.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/schemas/schemas.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/codegen/codegen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func TestExtPropGoTypeSkipOptionalPointer(t *testing.T) {
110110
assert.NoError(t, err)
111111

112112
// Check that optional pointer fields are skipped if requested
113-
assert.Contains(t, code, "NullableFieldSkipFalse *string `json:\"nullableFieldSkipFalse\"`")
114-
assert.Contains(t, code, "NullableFieldSkipTrue string `json:\"nullableFieldSkipTrue\"`")
113+
assert.Contains(t, code, "NullableFieldSkipFalse *string `json:\"nullableFieldSkipFalse,omitempty\"`")
114+
assert.Contains(t, code, "NullableFieldSkipTrue string `json:\"nullableFieldSkipTrue,omitempty\"`")
115115
assert.Contains(t, code, "OptionalField *string `json:\"optionalField,omitempty\"`")
116116
assert.Contains(t, code, "OptionalFieldSkipFalse *string `json:\"optionalFieldSkipFalse,omitempty\"`")
117117
assert.Contains(t, code, "OptionalFieldSkipTrue string `json:\"optionalFieldSkipTrue,omitempty\"`")

pkg/codegen/schema.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,7 @@ func GenFieldsFromProperties(props []Property) []string {
759759
shouldOmitEmpty := (!p.Required || p.ReadOnly || p.WriteOnly) &&
760760
(!p.Required || !p.ReadOnly || !globalState.options.Compatibility.DisableRequiredReadOnlyAsPointer)
761761

762-
omitEmpty := !p.Nullable && shouldOmitEmpty
763-
764-
if p.Nullable && globalState.options.OutputOptions.NullableType {
765-
omitEmpty = shouldOmitEmpty
766-
}
762+
omitEmpty := shouldOmitEmpty
767763

768764
omitZero := false
769765

0 commit comments

Comments
 (0)