Skip to content

feat: support for omitzero added in Go 1.24#512

Open
perher wants to merge 1 commit intoomissis:mainfrom
perher:feature/support-omitzero
Open

feat: support for omitzero added in Go 1.24#512
perher wants to merge 1 commit intoomissis:mainfrom
perher:feature/support-omitzero

Conversation

@perher
Copy link
Contributor

@perher perher commented Jan 25, 2026

This PR adds optional support for emitting omitzero tags (instead of omitempty). omitzero is a new feature that was added in Go 1.24 for json marshaling,

fixes #471

Summary by CodeRabbit

  • New Features
    • Added a PreferOmitzero option so generated structs can use omitzero (Go 1.24+) instead of omitempty for JSON/YAML tags.
  • Tests
    • Added tests and an example schema demonstrating omitzero generation and enforcing nullability checks during JSON/YAML deserialization.

@codecov
Copy link

codecov bot commented Jan 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@9f6eb96). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #512   +/-   ##
=======================================
  Coverage        ?   41.44%           
=======================================
  Files           ?       67           
  Lines           ?     5801           
  Branches        ?        0           
=======================================
  Hits            ?     2404           
  Misses          ?     3118           
  Partials        ?      279           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai
Copy link

coderabbitai bot commented Feb 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b201caf and f15ccbd.

📒 Files selected for processing (5)
  • pkg/generator/config.go
  • pkg/generator/schema_generator.go
  • tests/data/preferOmitzero/preferOmitzero.go
  • tests/data/preferOmitzero/preferOmitzero.json
  • tests/generation_test.go
🚧 Files skipped from review as they are similar to previous changes (5)
  • tests/generation_test.go
  • tests/data/preferOmitzero/preferOmitzero.go
  • tests/data/preferOmitzero/preferOmitzero.json
  • pkg/generator/config.go
  • pkg/generator/schema_generator.go

📝 Walkthrough

Walkthrough

Adds a PreferOmitzero boolean to generator config and updates the schema generator to emit ,omitzero instead of ,omitempty when set. Includes new test schema, generated types, and a test exercising the new flag.

Changes

Cohort / File(s) Summary
Config & Generator
pkg/generator/config.go, pkg/generator/schema_generator.go
Added PreferOmitzero bool to Config. Generator now uses ,omitzero for JSON tags when the flag is true; existing required/omitempty disabling logic preserved.
Test data & generated types
tests/data/preferOmitzero/preferOmitzero.json, tests/data/preferOmitzero/preferOmitzero.go
Added JSON schema and generated Go file demonstrating omitzero tags, with custom UnmarshalJSON/UnmarshalYAML that validate null constraints for specific fields.
Tests
tests/generation_test.go
Added TestPreferOmitzero which enables PreferOmitzero on a config copy and runs generation tests against the new dataset.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 I wiggle a flag and say hello,
omitzero hops where omitempty used to go.
Slices stay put, not vanished from view,
Tests crunch carrots to prove that's true.
A tiny rabbit smiles — code fresh and new.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main feature: adding support for omitzero, a Go 1.24 feature.
Linked Issues check ✅ Passed Changes implement omitzero support across config, schema generation, and test fixtures, meeting the core objective of supporting Go 1.24's omitzero tag for JSON marshaling.
Out of Scope Changes check ✅ Passed All changes directly support the omitzero feature: configuration field, generation logic, test schema, and test case. No unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@pkg/generator/config.go`:
- Around line 38-39: Fix the typo in the comment for the PreferOmitzero field:
change the phrase "will use omit omitzero" to "will use omitzero" in the comment
above the PreferOmitzero bool declaration so the comment reads that
PreferOmitzero will use omitzero instead of omitempty (requires Go 1.24+);
update the comment attached to PreferOmitzero accordingly.
🧹 Nitpick comments (2)
tests/data/preferOmitzero/preferOmitzero.go (2)

46-66: Note: Unused raw variable in generated code.

The raw map is unmarshalled but never used (lines 47-50). This appears to be a pre-existing pattern in the generator templates, likely used for other validation scenarios. Consider cleaning up the generator template to omit this when not needed, but this is outside the scope of this PR.


68-89: Same observation for UnmarshalYAML.

The raw map decoding (lines 70-73) follows the same unused pattern as UnmarshalJSON. Same suggestion applies regarding generator template cleanup as a future improvement.

@perher perher force-pushed the feature/support-omitzero branch from 2acdf26 to b201caf Compare February 16, 2026 09:28
@omissis omissis force-pushed the feature/support-omitzero branch from b201caf to f15ccbd Compare February 28, 2026 15:46
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.

Support for omitzero tag added in Go 1.24

2 participants