Skip to content

Fix omitzero handling - #28

Merged
satvik007 merged 1 commit into
mainfrom
himanshu/fix-omitzero-handling
Sep 25, 2026
Merged

satvik007 merged 1 commit into
mainfrom
himanshu/fix-omitzero-handling

Conversation

@hi-rai

@hi-rai hi-rai commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What changed

  • isOptional now marks struct fields tagged omitzero as optional. This includes time.Time and inline structs. Struct fields tagged only omitempty stay required.
  • A new helper, hasJSONOption, reads the options of a json tag. Before this change, the code used strings.Contains on the whole tag. isOptional and isNullable both use the new helper.
  • getValidateKeys now uses strings.Cut instead of SplitN. This is a small cleanup with no change in behavior.
  • Tests and golden files now cover omitzero on structs, time.Time, *time.Time, arrays, slices, maps, pointer-to-slice/map, and fields named omitzero/omitempty.

Why

  • encoding/json omits a struct field tagged omitzero when the value is zero or when its IsZero() method returns true. The old code never marked struct fields as optional, so the schema required fields that Go may leave out of the JSON.
  • The old substring check also treated a field name as an option. For example, json:"omitzero" sets the field name to omitzero but has no options, and it was still treated as optional.

Impact

  • The generated schemas now match what encoding/json produces for these fields. Struct and time.Time fields tagged omitzero now get .optional().
  • A field whose JSON name contains omitempty or omitzero is no longer marked optional or non-nullable by mistake.
  • The generated output changes for affected types. Consumers should regenerate their schemas. Fields tagged omitzero with validate:"required" stay required.

@hi-rai
hi-rai requested a review from satvik007 September 24, 2026 07:09
@hi-rai hi-rai self-assigned this Sep 24, 2026
@github-actions github-actions Bot deleted a comment from claude Bot Sep 24, 2026
@satvik007
satvik007 merged commit 194dcf4 into main Sep 25, 2026
2 checks passed
@satvik007
satvik007 deleted the himanshu/fix-omitzero-handling branch September 25, 2026 08:48
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.

2 participants