Repository navigation
Fix v2.6.0 omitempty regression - #2505
Conversation
Fixes oapi-codegen#2503 Since v2.6.0, nullable struct fields received an `omitempty` JSON tag, changing the wire format for unchanged specs: required+nullable fields lost their always-present key (a JSON Schema `required` violation when the pointer is nil), and optional+nullable fields could no longer emit an explicit `null`, collapsing the null-vs-absent distinction that `nullable: true` exists to express. The guard was removed by d567e49 (oapi-codegen#2221), which aligned the code with an inaccurate README example that issue oapi-codegen#2091 had taken as the documented behavior. Restore the pre-2.6.0 rule in GenFieldsFromProperties: nullable fields never get `omitempty`, except under the `nullable-type` output option, where nullable.Nullable[T] models absent-vs-null itself and relies on `omitempty` for the absent case. The rule applies uniformly to OpenAPI 3.0 `nullable: true` and the 3.1 `type: [..., "null"]` / null-branch idioms, since Property.Nullable covers both. Also revert the test expectations flipped by d567e49, update the two nullable round-trip tests that had baked in the regressed behavior (nil nullable fields now marshal as explicit nulls, identically across spec versions), regenerate the affected fixtures, and correct the README example that showed `omitempty` as the default for nullable fields. Users who prefer omission for nullable fields can opt in explicitly via `x-omitempty: true` or the `nullable-type` output option. Co-Authored-By: Claude Fable 5 <[email protected]>
Greptile SummaryRestores explicit-null serialization for nullable fields generated without nullable-type.
Confidence Score: 4/5This needs a fix before merging because required nullable readOnly and writeOnly fields can now be serialized on the schema direction where they must be absent. The nullable guard discards the existing omitempty decision for directional fields, causing nil readOnly fields to appear in requests and nil writeOnly fields to appear in responses as explicit nulls. Files Needing Attention: pkg/codegen/schema.go
|
| Filename | Overview |
|---|---|
| pkg/codegen/schema.go | Restores non-omitempty nullable tags, but unintentionally overrides directional omission for required nullable readOnly/writeOnly properties. |
| internal/test/schemas/nullable/nullable_test.go | Updates cross-version round-trip expectations to assert explicit null output for nil nullable fields. |
| pkg/codegen/codegen_test.go | Updates generated-tag assertions for nullable fields using optional-pointer overrides. |
| README.md | Documents that default nullable pointers serialize nil as explicit null rather than omitting the key. |
Prompt To Fix All With AI
### Issue 1
pkg/codegen/schema.go:1445
**Preserve directional field omission**
When a required nullable property is marked `readOnly` or `writeOnly`, this condition overrides the existing directional `shouldOmitEmpty` decision, causing nil read-only fields to be emitted as `null` in requests and nil write-only fields to be emitted as `null` in responses where the schema requires them to be absent.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Reviews (1): Last reviewed commit: "Fix v2.6.0 omitempty regression" | Re-trigger Greptile
|
Hey @mromaszewicz - I can see that this commit is on |
|
I am working through about a dozen PR's that I want to merge before making a gigantic v2.9.0 release. I'd like to get it out within a week or so, but I volunteer my time on this and do my best. can you pin to the top of the |
|
Looking forward to the release 🥳
|
Fixes #2503
Since v2.6.0, nullable struct fields received an
omitemptyJSON tag, changing the wire format for unchanged specs: required+nullable fields lost their always-present key (a JSON Schemarequiredviolation when the pointer is nil), and optional+nullable fields could no longer emit an explicitnull, collapsing the null-vs-absent distinction thatnullable: trueexists to express. The guard was removed by d567e49 (#2221), which aligned the code with an inaccurate README example that issue #2091 had taken as the documented behavior.Restore the pre-2.6.0 rule in GenFieldsFromProperties: nullable fields never get
omitempty, except under thenullable-typeoutput option, where nullable.Nullable[T] models absent-vs-null itself and relies onomitemptyfor the absent case. The rule applies uniformly to OpenAPI 3.0nullable: trueand the 3.1type: [..., "null"]/ null-branch idioms, since Property.Nullable covers both.Also revert the test expectations flipped by d567e49, update the two nullable round-trip tests that had baked in the regressed behavior (nil nullable fields now marshal as explicit nulls, identically across spec versions), regenerate the affected fixtures, and correct the README example that showed
omitemptyas the default for nullable fields.Users who prefer omission for nullable fields can opt in explicitly via
x-omitempty: trueor thenullable-typeoutput option.