Skip to content

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

Merged
jamietanna merged 2 commits into
oapi-codegen:mainfrom
lzap:omitzero1
Jul 15, 2025
Merged

jamietanna merged 2 commits into
oapi-codegen:mainfrom
lzap:omitzero1

Conversation

@lzap

@lzap lzap commented Apr 1, 2025

Copy link
Copy Markdown
Contributor

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 thread pkg/codegen/extension.go Resolved
Comment thread examples/extensions/xomitzero/api.yaml Outdated
Comment on lines +23 to +25
x-omitempty: true
x-omitzero: true

Copy link
Copy Markdown
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
Copy Markdown
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

lzap commented Jun 3, 2025

Copy link
Copy Markdown
Contributor Author

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

Comment thread pkg/codegen/schema.go
Comment on lines +751 to +753
fieldTags["json"] = p.JsonFieldName +
stringOrEmpty(omitEmpty, ",omitempty") +
stringOrEmpty(omitZero, ",omitzero")

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@kusari-inspector

This comment was marked as resolved.

@kusari-inspector

Copy link
Copy Markdown

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

@jamietanna

jamietanna commented Jul 14, 2025 •

Copy link
Copy Markdown
Member

(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!

@kusari-inspector

Copy link
Copy Markdown

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

@kusari-inspector

Copy link
Copy Markdown

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

@jamietanna

Copy link
Copy Markdown
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
@kusari-inspector

Copy link
Copy Markdown

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
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