FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix v2.6.0 omitempty regression by mromaszewicz · Pull Request #2505 · oapi-codegen/oapi-codegen · GitHub

Fix v2.6.0 omitempty regression - #2505

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2503
Jul 31, 2026
Merged

Fix v2.6.0 omitempty regression#2505
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2503

Conversation

Copy link
Copy Markdown
Member

Fixes #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 (#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 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.

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 <noreply@anthropic.com>
mromaszewicz requested a review from a team as a code owner July 31, 2026 20:50
mromaszewicz added the bug Something isn't working label Jul 31, 2026

greptile-apps Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Restores explicit-null serialization for nullable fields generated without nullable-type.

  • Removes omitempty from default nullable JSON, YAML, and form tags while retaining it for nullable.Nullable fields where appropriate.
  • Regenerates nullable fixtures and updates OpenAPI 3.0/3.1 serialization tests.
  • Corrects the README’s description of default nullable-field output.

Confidence Score: 4/5

This 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

Important Files Changed

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

Comment thread pkg/codegen/schema.go
mromaszewicz merged commit 73e9acf into oapi-codegen:main Jul 31, 2026
16 checks passed
mromaszewicz added the notable changes Used for release notes to highlight these more highly label Jul 31, 2026
mromaszewicz deleted the fix/issue-2503 branch July 31, 2026 22:38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

omitempty added to nullable struct fields since v2.6.0

1 participant


Back | FazBrowse Home | New Git URL