Skip to content

fix: add omitempty to optional nullable fields#2221

Merged
mromaszewicz merged 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/nullable-omitempty-2091
Feb 26, 2026
Merged

fix: add omitempty to optional nullable fields#2221
mromaszewicz merged 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/nullable-omitempty-2091

Conversation

@mromaszewicz
Copy link
Copy Markdown
Member

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.

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>
@mromaszewicz mromaszewicz requested a review from a team as a code owner February 14, 2026 04:41
@jamietanna jamietanna added this to the v2.6.0 milestone Feb 15, 2026
@mromaszewicz
Copy link
Copy Markdown
Member Author

@greptile

@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Feb 22, 2026

Greptile Summary

Fixed a bug where optional nullable fields were not receiving the omitempty JSON tag. The previous logic explicitly prevented nullable fields from getting omitempty by checking !p.Nullable && shouldOmitEmpty, which contradicted the expected behavior for optional fields.

Key changes:

  • Simplified omitempty logic in schema.go by removing the !p.Nullable guard and the special case for NullableType output option
  • Optional nullable fields now correctly receive omitempty tags, matching the behavior of optional non-nullable fields
  • Updated test expectations to reflect the correct behavior
  • Generated code properly includes omitempty on nullable optional fields like *string with json:"field,omitempty"

The fix ensures consistency: whether a field is nullable or not, if it's optional (not in the required array), it gets omitempty in its JSON tag.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is a simple, targeted logic change that removes an incorrect guard condition. The change is well-tested with multiple test cases covering different scenarios (nullable optional fields, nullable required fields, nullable with skip-optional-pointer). All generated test files have been updated to reflect the correct behavior, and the fix addresses a clear bug where optional nullable fields were incorrectly missing omitempty tags.
  • No files require special attention

Important Files Changed

Filename Overview
pkg/codegen/schema.go Simplified omitEmpty logic by removing the !p.Nullable guard that incorrectly prevented nullable optional fields from getting omitempty tags
pkg/codegen/codegen_test.go Updated test assertions to expect omitempty on nullable optional fields, correctly reflecting the new behavior
internal/test/schemas/schemas.gen.go Generated code now correctly includes omitempty on OptionalAndNullable field (nullable + optional)
internal/test/issues/issue-1039/defaultbehaviour/types.gen.go Generated code now correctly includes omitempty on all optional nullable fields (ComplexOptionalNullable, SimpleOptionalNullable, and nested AliasName)

Last reviewed commit: 33101e8

Copy link
Copy Markdown
Member

@jamietanna jamietanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the existing behaviour (potentially as early as #1033) wasn't quite right, thanks for spotting it!

@mromaszewicz mromaszewicz merged commit d567e49 into oapi-codegen:main Feb 26, 2026
27 checks passed
@jamietanna jamietanna added the bug Something isn't working label Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optional nullable field does not get omitempty json tag as mention in Readme

2 participants