Skip to content

fix: add omitempty to optional nullable fields#2221

Open
mromaszewicz wants to merge 1 commit intooapi-codegen:mainfrom
mromaszewicz:fix/nullable-omitempty-2091
Open

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

Conversation

@mromaszewicz
Copy link
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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