You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for Go 1.24's omitzero to replace unnecessary "optional pointers" #1899
Due to the way that Go's JSON marshalling works, the standard way to make a field not marshal, if it was not specified, was to use a pointer, and a JSON tag of omitempty:
This is the default behaviour within oapi-codegen, and the expectation within the Go community, but it can be unwieldy.
Over the years, we've had a number of requests to make this easier to work with, in particular for folks who don't want to be dealing with pointers to things, or where there are i.e. pointers to slices:
However, with the upcoming Go 1.24 release, this is something that can now handle with the new changes to encoding/json which introduces omitzero (proposal)
This would allow us to no longer provide a pointer for a type that is optional, when we can instead omitzero the zero value.
What about previous versions of Go?
This would only be intended for use with Go 1.24+ codebases.
Does this require everyone upgrade to Go 1.24?
No, the intent is that we either:
make this an opt-in flag, via output-options
auto-detect the go version that the parent go.mod - if present - is using, and if 1.24+, use omitzero, rather than optional pointers
Out of scope
Additionally - and not part of this proposal - is that there are performance implications where you may want to use a pointer for a type, or may not.
Reacted by Léonard Suslian, Bibin T, John Daniel Maguire, metinatakli, Kevin Falting, Brian Edelman, Jasmin, Ashish, Olivier Ligot, Krisztian Gacsal and 39 more
The default pointer behavior has non-Go-idiomatic behaviors like pointers to maps and slices (#1561). Go's JSON marshaling does not require pointers for such cases.
Client code needs to be written with wrappers around every read and write, which is error prone and cumbersome. Checking for nil pointers doesn't obviate needing to check the dereferenced values, so it doesn't add value in most cases.
We're trying x-go-type-skip-optional-pointer, but making it easier to produce that behavior seems like a good idea.
I think I've unfortunately closed this in a bit of a hasty nature - re-reading the intent here, it's not quite solved by adding the support for x-omitzero, sorry folks!
Hi @jamietanna I was under the impression that this would be geared towards allowing users to apply this feature to the entire spec instead of on a per field basis. This latest change still appears to require setting it on all fields individually unless I'm mistaken?
Activity
bgrant0607 commentedon Apr 4, 2025
The default pointer behavior has non-Go-idiomatic behaviors like pointers to maps and slices (#1561). Go's JSON marshaling does not require pointers for such cases.
Client code needs to be written with wrappers around every read and write, which is error prone and cumbersome. Checking for nil pointers doesn't obviate needing to check the dereferenced values, so it doesn't add value in most cases.
We're trying
x-go-type-skip-optional-pointer, but making it easier to produce that behavior seems like a good idea.jamietanna commentedon Apr 7, 2025
A couple of community contributions so far:
x-omitzero#1944jamietanna commentedon Apr 7, 2025
(given the number of hearts on the issue I can see this may be something we should look at prioritising in the near future)
lzap commentedon Apr 17, 2025
The other link does appear to be all about
omitemptynotomitzero, anyways, yes. That is a bunch of hearts :-)feat(extensions): add support for `x-omitzero`
jamietanna commentedon Jul 15, 2025
This will be going in later today - need to do some small tweaks to our README before I can finish off merging in @lzap's great contribution 🚀
feat(extensions): add support for `x-omitzero`
jamietanna commentedon Jul 15, 2025
I think I've unfortunately closed this in a bit of a hasty nature - re-reading the intent here, it's not quite solved by adding the support for
x-omitzero, sorry folks!jamietanna commentedon Jul 15, 2025
This looks like it'll be very similar to #1694, with the addition of "and make sure that
omitzerois added to the JSON tags"feat(output-options): allow using `omitzero` with `prefer-skip-option…
feat(output-options): allow using `omitzero` with `prefer-skip-option…
jamietanna commentedon Jul 15, 2025
This will be supported by #2023 - any feedback welcome, but I'll be looking at merging it today
feat(output-options): allow using `omitzero` with `prefer-skip-option…
mminklet commentedon Sep 10, 2025
Hi @jamietanna I was under the impression that this would be geared towards allowing users to apply this feature to the entire spec instead of on a per field basis. This latest change still appears to require setting it on all fields individually unless I'm mistaken?
jamietanna commentedon Sep 10, 2025
Have you taken a look at https://github.com/oapi-codegen/oapi-codegen?tab=readme-ov-file#globally-skipping-the-optional-pointer which indicates the global setting
prefer-skip-optional-pointer-with-omitzerowhich should do what you want?mminklet commentedon Sep 10, 2025
Amazing, thanks. I hadn't seen that addition, should have looked harder