Skip to content

feat(extensions): add support for x-omitzero #1944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2025

Conversation

lzap
Copy link
Contributor

@lzap lzap commented Apr 1, 2025

SSIA

I do not want to add this by default, not sure what consequences would be, but I want to be able to start playing around with it.

I can do add documentation as well to the README if you like it.

Comment on lines 23 to 25
x-omitempty: true
x-omitzero: true
Copy link
Member

Choose a reason for hiding this comment

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

Do both of these need to be set, here? Or should we only have x-omitzero?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, probably a leftover. Rebased.

@lzap lzap force-pushed the omitzero1 branch 2 times, most recently from e0142e6 to 48dc3d2 Compare May 12, 2025 07:37
@lzap
Copy link
Contributor Author

lzap commented Jun 3, 2025

Any other comments? This aligns with prior art and does not break anything existing.

Comment on lines +751 to +753
fieldTags["json"] = p.JsonFieldName +
stringOrEmpty(omitEmpty, ",omitempty") +
stringOrEmpty(omitZero, ",omitzero")
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if we should have both omitempty and omitzero 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it is a valid Go struct syntax and while I cannot come up with a valid usecase now, it does not mean it does not exist. Let me put it this way: why would be a problem to support both at the same time?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, thought I'd replied to this! It turns out there is likely no issue (via):

If both "omitempty" and "omitzero" are specified, the field will be omitted if the value is either empty or zero (or both).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I was not sure, I saw some articled explaining that having both would make sense but I could not google them back.

@jamietanna jamietanna self-assigned this Jun 15, 2025

This comment was marked as resolved.

Copy link

Kusari PR Analysis rerun based on - 2e18604 performed at: 2025-07-14T20:21:54Z - link to updated analysis

@jamietanna
Copy link
Member

jamietanna commented Jul 14, 2025

(Ignore that check, it's fine!)

I'm still working on this one - trying to get a good example of exactly how omitzero works and what differences in behaviour we'll see - largely out of my own learning!

Copy link

Kusari PR Analysis rerun based on - 9cfaf20 performed at: 2025-07-14T20:31:16Z - link to updated analysis

Copy link

Kusari PR Analysis rerun based on - e5d75ff performed at: 2025-07-15T08:10:07Z - link to updated analysis

@jamietanna
Copy link
Member

I'll follow-up with a PR to allow tuning only one or the other of omitempty and omitzero in case folks want more control

lzap and others added 2 commits July 15, 2025 09:36
One of the big improvements added in Go 1.24 is `omitzero`[0][1][2],
which makes it possible to omit the "zero value" of a given Go type when
marshaling to JSON, or if the `IsZero() bool` function on the type
returns `true`.

To make it possible for `oapi-codegen` to use this, we can wire in a new
extension, `x-omitzero` which allows per-field tuning of this behaviour.

This is something that is safe to generate alongside the `omitempty`
tags[3]:

> If both "omitempty" and "omitzero" are specified, the field will be
omitted if the value is either empty or zero (or both).

To validate this, we can add a new example for the extension, which
provides testing of the expected behaviour.

As this requires Go 1.24, we need to set up the boilerplate for a new
module that only runs Go 1.24.

Closes oapi-codegen#1899.

[0]: https://www.jvt.me/posts/2025/02/12/go-omitzero-124/
[1]: https://www.bytesizego.com/blog/go-124-omitzero
[2]: https://antonz.org/go-1-24/#omit-zero-values-in-json
[3]: https://pkg.go.dev/encoding/json
Copy link

Kusari PR Analysis rerun based on - 83987ce performed at: 2025-07-15T08:36:58Z - link to updated analysis

@jamietanna jamietanna changed the title feat: add support for x-omitzero eat(extensions): add support for x-omitzero Jul 15, 2025
@jamietanna jamietanna changed the title eat(extensions): add support for x-omitzero feat(extensions): add support for x-omitzero Jul 15, 2025
@jamietanna jamietanna added the notable changes Used for release notes to highlight these more highly label Jul 15, 2025
@jamietanna jamietanna merged commit 739f502 into oapi-codegen:main Jul 15, 2025
20 checks passed
@lzap lzap deleted the omitzero1 branch July 16, 2025 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:extensions notable changes Used for release notes to highlight these more highly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants