Title: Request body with oneOf schema not generated correctly · Issue #2018 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: Request body with oneOf schema not generated correctly · Issue #2018 · oapi-codegen/oapi-codegen
X Title: Request body with oneOf schema not generated correctly · Issue #2018 · oapi-codegen/oapi-codegen
Description: When using a schema with oneOf in the requestBody the code is not generated accoring to the documentation. Version v2.4.1 openapi.yaml openapi: 3.0.1 info: title: openapi test version: v0.0.1 paths: /change: post: operationId: change req...
Open Graph Description: When using a schema with oneOf in the requestBody the code is not generated accoring to the documentation. Version v2.4.1 openapi.yaml openapi: 3.0.1 info: title: openapi test version: v0.0.1 paths...
X Description: When using a schema with oneOf in the requestBody the code is not generated accoring to the documentation. Version v2.4.1 openapi.yaml openapi: 3.0.1 info: title: openapi test version: v0.0.1 paths...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2018
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Request body with oneOf schema not generated correctly","articleBody":"When using a schema with oneOf in the requestBody the code is not generated accoring to the documentation.\n\n**Version v2.4.1**\n\nopenapi.yaml\n```yaml\nopenapi: 3.0.1\ninfo:\n title: openapi test\n version: v0.0.1\n\npaths:\n /change:\n post:\n operationId: change\n requestBody:\n description: the request\n required: true\n content:\n application/json:\n schema:\n oneOf:\n - $ref: '#/components/schemas/NameChange'\n - $ref: '#/components/schemas/AddressChange'\n discriminator:\n propertyName: changeType\n mapping:\n name: '#/components/schemas/NameChange'\n address: '#/components/schemas/AddressChange'\n responses:\n 200:\n description: OK\n content:\n application/json:\n schema:\n type: object\n properties:\n response:\n type: object\n description: Object containing the response.\n properties:\n changeType:\n type: string\n description: the type of change made\n\ncomponents:\n schemas:\n Change:\n type: object\n required:\n - changeType\n properties:\n changeType:\n type: string\n NameChange:\n allOf:\n - $ref: '#/components/schemas/Change'\n - type: object\n required:\n - newName\n properties:\n newName:\n type: string\n AddressChange:\n allOf:\n - $ref: '#/components/schemas/Change'\n - type: object\n required:\n - newAddress\n properties:\n newAddress:\n type: string\n\n```\n\n**Expected**\nAccording to the documentation there should be helper methods generated like the following:\n```go\nfunc (t *ChangeJSONRequestBody) FromNameChange(v NameChange) error {\n\tb, err := json.Marshal(v)\n\tt.union = b\n\treturn err\n}\n```\n\n**Actual**\nThe types are generated but without helper methods as you can see in this [example](https://github.com/m-kay/go-openapi-oneof-codegen/blob/main/oneof.gen.go)","author":{"url":"https://github.com/m-kay","@type":"Person","name":"m-kay"},"datePublished":"2025-07-10T08:43:06.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2018/oapi-codegen/issues/2018"}
| 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:68d2e9df-c083-f695-993e-73c89814411a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C186:6F3CF:89B09F6:C1D8C81:6A50D9FA |
| html-safe-nonce | cf3e18d4bf57900ad2b687ed198a1bb2430d972e9238d5feee9fad590b47a75a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTg2OjZGM0NGOjg5QjA5RjY6QzFEOEM4MTo2QTUwRDlGQSIsInZpc2l0b3JfaWQiOiI0MjUxMDI0ODYwMzEzMDgyMzYyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 773a6c5c67e07b9620d570c6e397931a555d5ffb3c3a7568c431ddc1ef5120fb |
| hovercard-subject-tag | issue:3218444581 |
| 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/2018/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8dd073e33fdc39b51c6bfafb7ee9cb922c4ae5578291f19acf6db6051c73536e/oapi-codegen/oapi-codegen/issues/2018 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8dd073e33fdc39b51c6bfafb7ee9cb922c4ae5578291f19acf6db6051c73536e/oapi-codegen/oapi-codegen/issues/2018 |
| og:image:alt | When using a schema with oneOf in the requestBody the code is not generated accoring to the documentation. Version v2.4.1 openapi.yaml openapi: 3.0.1 info: title: openapi test version: v0.0.1 paths... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | m-kay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 72b13e0e8d0319acdd27a145cfe73f4f06c791713d0ac4690e41fb68ad28cac0 |
| 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 | 73ad5c4eaffed7bb3637024653eabdbca84a855e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width