Title: Unusable code for schema response with oneOf · Issue #1900 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: Unusable code for schema response with oneOf · Issue #1900 · oapi-codegen/oapi-codegen
X Title: Unusable code for schema response with oneOf · Issue #1900 · oapi-codegen/oapi-codegen
Description: Hello, I've looked for duplicate, but could not find exactly the same bug (as far as I searched), here are related ones: #626 #1496 #1872 #1882 #1665 And a related PR: #648 Steps to reproduce: I've created minimal reproduction code here ...
Open Graph Description: Hello, I've looked for duplicate, but could not find exactly the same bug (as far as I searched), here are related ones: #626 #1496 #1872 #1882 #1665 And a related PR: #648 Steps to reproduce: I've...
X Description: Hello, I've looked for duplicate, but could not find exactly the same bug (as far as I searched), here are related ones: #626 #1496 #1872 #1882 #1665 And a related PR: #648 Steps to reproduce: ...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/1900
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unusable code for schema response with oneOf","articleBody":"Hello, I've looked for duplicate, but could not find exactly the same bug (as far as I searched), here are related ones:\n- https://github.com/oapi-codegen/oapi-codegen/issues/626\n- https://github.com/oapi-codegen/oapi-codegen/issues/1496\n- https://github.com/oapi-codegen/oapi-codegen/issues/1872\n- https://github.com/oapi-codegen/oapi-codegen/issues/1882\n- https://github.com/oapi-codegen/oapi-codegen/issues/1665\n\nAnd a related PR: \n- https://github.com/oapi-codegen/oapi-codegen/pull/648\n\n## Steps to reproduce:\n\nI've created minimal reproduction code [here](https://github.com/aBMania/oapi-codegen/tree/bug/1900/internal/test/issues/issue-1900)\n\n### 1. OpenAPI Spec\n\n```yaml\nopenapi: 3.0.2\ninfo:\n version: 1.0.0\n title: Issue 1900\npaths:\n /ping:\n get:\n responses:\n '200':\n content:\n application/json:\n schema:\n oneOf:\n - $ref: '#/components/schemas/Pong1'\n - $ref: '#/components/schemas/Pong2'\n - $ref: '#/components/schemas/Pong3'\n```\n\n### 2. Oapi-codegen config\n\n```yaml\npackage: issue1900\noutput: ping.gen.go\ngenerate:\n models: true\n client: true\n```\n\n### 3. Generated code\n\n```go\ntype GetPingResponse struct {\n\tBody []byte\n\tHTTPResponse *http.Response\n\tJSON200 *struct {\n\t\tunion json.RawMessage\n\t}\n\tJSON404 *Error\n}\n```\n\nHere the `JSON200` type is unusable\n\n## Workaround\n\nAs a workaround, I've manually modified the OpenAPI spec of the API we are using before generating code:\n\n```yaml\n# Changed:\n'/Ping':\n get:\n # ...\n responses:\n '200':\n content:\n application/json:\n schema:\n $ref: \"#/components/schemas/PongResponse\"\n\n# Added: \n schemas:\n # ...\n PongResponse:\n oneOf:\n - $ref: \"#/components/schemas/Pong1\"\n - $ref: \"#/components/schemas/Pong2\"\n - $ref: \"#/components/schemas/Pong3\"\n # ...\n```\n\nAnd it's now generating a usable type: \n```go\ntype GetPingResponse struct {\n\tBody []byte\n\tHTTPResponse *http.Response\n\tJSON200 *PongResponse // generated type here\n\tJSON404 *Error\n}\nfunc (t PongResponse) AsPong1() (Pong1, error) { ... }\nfunc (t PongResponse) AsPong2() (Pong2, error) { ... }\n// ...\n```","author":{"url":"https://github.com/aBMania","@type":"Person","name":"aBMania"},"datePublished":"2025-02-12T09:20:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/1900/oapi-codegen/issues/1900"}
| 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:5aef844f-0758-53ee-149b-3eb45c1de147 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF10:16C3D3:B6B70B:F83740:6A4ED364 |
| html-safe-nonce | 562e56f7da6f22f3f18f3c2a7c6642acfbf57f9ef0987b3edab76549d7039396 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjEwOjE2QzNEMzpCNkI3MEI6RjgzNzQwOjZBNEVEMzY0IiwidmlzaXRvcl9pZCI6IjQ2NjU0Nzk4MTk4NzM0MDc4NDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b3d84bf5c91646e69366981ac59f23d3e5d158fc48ca92b80e6ccc32c506cb88 |
| hovercard-subject-tag | issue:2847638259 |
| 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/1900/issue_layout |
| twitter:image | https://opengraph.githubassets.com/806d8c44f54a5dc1b7e25accfa465dfd30867befc49dc2e521ecf0f02edd1009/oapi-codegen/oapi-codegen/issues/1900 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/806d8c44f54a5dc1b7e25accfa465dfd30867befc49dc2e521ecf0f02edd1009/oapi-codegen/oapi-codegen/issues/1900 |
| og:image:alt | Hello, I've looked for duplicate, but could not find exactly the same bug (as far as I searched), here are related ones: #626 #1496 #1872 #1882 #1665 And a related PR: #648 Steps to reproduce: I've... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aBMania |
| hostname | github.com |
| expected-hostname | github.com |
| None | 41b6ab3ba6d20a71766ac245b5a4a94c6fc672a9cd4da7d44c1b33ab8bf6a21c |
| 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 | e6a744804e8e70f97b4d5a18a94dcc63db22f97a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width