Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

Commit d567e49

Browse files
mromaszewiczclaude
andauthored
fix: add omitempty to optional nullable fields (oapi-codegen#2221)
Fixes oapi-codegen#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 99eadbe commit d567e49

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
@@ -117,8 +117,8 @@ func TestExtPropGoTypeSkipOptionalPointer(t *testing.T) {
117117
assert.NoError(t, err)
118118

119119
// Check that optional pointer fields are skipped if requested
120-
assert.Contains(t, code, "NullableFieldSkipFalse *string `json:\"nullableFieldSkipFalse\"`")
121-
assert.Contains(t, code, "NullableFieldSkipTrue string `json:\"nullableFieldSkipTrue\"`")
120+
assert.Contains(t, code, "NullableFieldSkipFalse *string `json:\"nullableFieldSkipFalse,omitempty\"`")
121+
assert.Contains(t, code, "NullableFieldSkipTrue string `json:\"nullableFieldSkipTrue,omitempty\"`")
122122
assert.Contains(t, code, "OptionalField *string `json:\"optionalField,omitempty\"`")
123123
assert.Contains(t, code, "OptionalFieldSkipFalse *string `json:\"optionalFieldSkipFalse,omitempty\"`")
124124
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
@@ -734,11 +734,7 @@ func GenFieldsFromProperties(props []Property) []string {
734734
shouldOmitEmpty := (!p.Required || p.ReadOnly || p.WriteOnly) &&
735735
(!p.Required || !p.ReadOnly || !globalState.options.Compatibility.DisableRequiredReadOnlyAsPointer)
736736

737-
omitEmpty := !p.Nullable && shouldOmitEmpty
738-
739-
if p.Nullable && globalState.options.OutputOptions.NullableType {
740-
omitEmpty = shouldOmitEmpty
741-
}
737+
omitEmpty := shouldOmitEmpty
742738

743739
omitZero := false
744740

0 commit comments

Comments
 (0)