Title: Nested nullable unions silently discard JSON payloads · Issue #2565 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: Nested nullable unions silently discard JSON payloads · Issue #2565 · oapi-codegen/oapi-codegen
X Title: Nested nullable unions silently discard JSON payloads · Issue #2565 · oapi-codegen/oapi-codegen
Description: An OpenAPI 3.1 nullable wrapper around an inline oneOf or anyOf generates a union struct without its JSON methods. Generation succeeds, but unmarshalling and then marshalling silently drops the union payload. Reproduced on main at 833a96...
Open Graph Description: An OpenAPI 3.1 nullable wrapper around an inline oneOf or anyOf generates a union struct without its JSON methods. Generation succeeds, but unmarshalling and then marshalling silently drops the uni...
X Description: An OpenAPI 3.1 nullable wrapper around an inline oneOf or anyOf generates a union struct without its JSON methods. Generation succeeds, but unmarshalling and then marshalling silently drops the uni...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2565
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Nested nullable unions silently discard JSON payloads","articleBody":"An OpenAPI 3.1 nullable wrapper around an inline `oneOf` or `anyOf` generates a union struct without its JSON methods. Generation succeeds, but unmarshalling and then marshalling silently drops the union payload.\n\nReproduced on `main` at `833a968f84b4b0ec7c2f3fb5bb119e3c449f7a4f`, using Go 1.27.1 on darwin/arm64, with `compatibility.schema-merging-behavior` set to `v1`, `v2`, or `v3`.\n\n### Reproduction\n\nSave these three files in a new directory. The reproduction is provided under Apache-2.0.\n\n`spec.yaml`:\n\n```yaml\nopenapi: 3.1.0\ninfo:\n title: Nested nullable union reproduction\n version: 1.0.0\npaths: {}\ncomponents:\n schemas:\n Envelope:\n type: object\n required: [pet]\n properties:\n pet:\n anyOf:\n - oneOf:\n - $ref: '#/components/schemas/Cat'\n - $ref: '#/components/schemas/Dog'\n - type: 'null'\n Cat:\n type: object\n required: [meow]\n properties:\n meow: {type: string}\n Dog:\n type: object\n required: [bark]\n properties:\n bark: {type: string}\n```\n\n`config.yaml`:\n\n```yaml\npackage: repro\noutput: types.gen.go\ngenerate:\n models: true\noutput-options:\n skip-prune: true\n```\n\n`repro_test.go`:\n\n```go\npackage repro\n\nimport (\n\t\"encoding/json\"\n\t\"testing\"\n)\n\nfunc TestNestedNullableUnionRoundTrip(t *testing.T) {\n\tconst input = `{\"pet\":{\"meow\":\"purr\"}}`\n\tvar value Envelope\n\tif err := json.Unmarshal([]byte(input), &value); err != nil {\n\t\tt.Fatal(err)\n\t}\n\toutput, err := json.Marshal(value)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tif string(output) != input {\n\t\tt.Fatalf(\"got %s, want %s\", output, input)\n\t}\n}\n```\n\nRun:\n\n```sh\ngo mod init example.com/nested-nullable-repro\ngo run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@833a968f84b4b0ec7c2f3fb5bb119e3c449f7a4f --config=config.yaml spec.yaml\ngo get github.com/oapi-codegen/runtime@v1.7.0\ngo test .\n```\n\nActual result:\n\n```text\ngot {\"pet\":{}}, want {\"pet\":{\"meow\":\"purr\"}}\n```\n\nExpected: the complete payload survives the JSON round trip.\n\n### Cause\n\nThe nullable-union collapse path drops `UnionElements` and `Discriminator`, so the union's JSON methods and accessors are not generated.\n\nRelated to #2218. This reproduces after the OpenAPI 3.1 support added in #2336.\n","author":{"url":"https://github.com/g-logunov","@type":"Person","name":"g-logunov"},"datePublished":"2026-09-24T13:26:28Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2565"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:fae81b97-7dcf-4cfd-3c5f-c79e6e7876ba |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D1B8:14BCA4:50A303:705705:6AF9BAB6 |
| html-safe-nonce | 2eaedd7a789a8e2cf8f00bc6de3abbfa725a520ffe425962211cb7e28a6d4e69 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMUI4OjE0QkNBNDo1MEEzMDM6NzA1NzA1OjZBRjlCQUI2IiwidmlzaXRvcl9pZCI6IjEwMDg4OTU5NDU3MDMyNzUxOTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f1f820c88310fab9d1e63b99ad462f67c86c77151a6c3756b41639d06609cbc9 |
| hovercard-subject-tag | issue:5569041852 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/oapi-codegen/oapi-codegen/2565/issue_layout |
| twitter:image | https://opengraph.githubassets.com/91e434211772337837930bb1b1f7aa7473c8597e42b32cd70435601097264e3a/oapi-codegen/oapi-codegen/issues/2565 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/91e434211772337837930bb1b1f7aa7473c8597e42b32cd70435601097264e3a/oapi-codegen/oapi-codegen/issues/2565 |
| og:image:alt | An OpenAPI 3.1 nullable wrapper around an inline oneOf or anyOf generates a union struct without its JSON methods. Generation succeeds, but unmarshalling and then marshalling silently drops the uni... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | g-logunov |
| hostname | github.com |
| expected-hostname | github.com |
| None | 612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7 |
| turbo-cache-control | no-preview |
| go-import | github.com/oapi-codegen/oapi-codegen git https://github.com/oapi-codegen/oapi-codegen.git |
| octolytics-dimension-user_id | 142752710 |
| octolytics-dimension-user_login | oapi-codegen |
| octolytics-dimension-repository_id | 173009358 |
| octolytics-dimension-repository_nwo | oapi-codegen/oapi-codegen |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 173009358 |
| octolytics-dimension-repository_network_root_nwo | oapi-codegen/oapi-codegen |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 072e34035c13c31543fba9f025cbbbda4210dae4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width