Title: A oneOf/anyOf branch that is an array of an inline enum gets the same type name as its items · Issue #2640 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: A oneOf/anyOf branch that is an array of an inline enum gets the same type name as its items · Issue #2640 · oapi-codegen/oapi-codegen
X Title: A oneOf/anyOf branch that is an array of an inline enum gets the same type name as its items · Issue #2640 · oapi-codegen/oapi-codegen
Description: When a union branch is an array whose items are an inline enum, the branch's type and the items' enum type get the same name (...Roles1). In a request body or a parameter, generation succeeds and the output doesn't compile. In a componen...
Open Graph Description: When a union branch is an array whose items are an inline enum, the branch's type and the items' enum type get the same name (...Roles1). In a request body or a parameter, generation succeeds and t...
X Description: When a union branch is an array whose items are an inline enum, the branch's type and the items' enum type get the same name (...Roles1). In a request body or a parameter, generation succee...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2640
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A oneOf/anyOf branch that is an array of an inline enum gets the same type name as its items","articleBody":"When a union branch is an array whose items are an inline enum, the branch's type and the items' enum type get the same name (`...Roles1`). In a request body or a parameter, generation succeeds and the output doesn't compile. In a component schema, generation fails with \"duplicate typename\", and the suggested `x-go-name` doesn't apply, since both names are generated.\n\n\"A string or a list of strings\" (`oneOf: [string, array of string]`) is a common shape; adding an `enum` to the items is enough to trigger this.\n\n## Reproduction\n\n`openapi.yaml`:\n\n```yaml\nopenapi: 3.0.3\ninfo: {title: t, version: \"1\"}\npaths:\n /items:\n post:\n requestBody:\n content:\n application/json:\n schema:\n type: object\n properties:\n roles:\n oneOf:\n - {type: string}\n - {type: array, items: {type: string, enum: [admin, viewer]}}\n responses:\n \"204\": {description: ok}\n```\n\n```console\n$ oapi-codegen -generate types -package api -o api.gen.go openapi.yaml\n$ go build ./...\n./api.gen.go:21:6: invalid recursive type: PostItemsJSONBodyRoles1 refers to itself\n./api.gen.go:24:6: PostItemsJSONBodyRoles1 redeclared in this block\n\t./api.gen.go:21:6: other declaration of PostItemsJSONBodyRoles1\n```\n\nNo configuration file; the module requires `github.com/oapi-codegen/runtime` v1.8.0. The generated code:\n\n```go\n// PostItemsJSONBodyRoles1 defines parameters for PostItems.\ntype PostItemsJSONBodyRoles1 = []PostItemsJSONBodyRoles1\n\n// PostItemsJSONBodyRoles1 defines parameters for PostItems.\ntype PostItemsJSONBodyRoles1 string\n```\n\nOn `main` the first one is `type PostItemsJSONBodyRoles1 []PostItemsJSONBodyRoles1` and only the \"redeclared\" error remains.\n\nThe same `roles` property as a query parameter's schema gives `GetItemsParamsRoles1 redeclared`. Under `components.schemas.Item` (referenced from a response) generation fails instead:\n\n```text\nerror generating code: error generating code for type definitions: duplicate typename 'ItemRoles1' detected, can't auto-rename, please use x-go-name to specify your own name for one of them\n```\n\nIt also happens with a single-branch `oneOf`, with `anyOf`, with an enum on the string branch too, and with `resolve-type-name-collisions: true`. It reproduces on v2.4.1 as well. Setting `x-go-type-name` on the items (`items: {type: string, enum: [admin, viewer], x-go-type-name: Role}`) works around it.\n\n## Expected\n\nTwo distinct types, for example `PostItemsJSONBodyRoles1 []PostItemsJSONBodyRoles1Item` and `PostItemsJSONBodyRoles1Item string`, and code that compiles.\n\n## Versions\n\nv2.8.1 and `main` (15f5714, `v2.8.1-0.20261028140811-15f571472ff8`), Go 1.27.3, linux/amd64.\n\n## Likely cause\n\nAn array's items are generated at the array's own path (`generateGoSchema(ctx.at(itemHint), schema.Items, path)`, `pkg/codegen/schema.go:1729` on main), and an inline enum is named from its path (`schema.go:1693`), so the items' enum takes the array's name. A plain array property never gets a type of its own, so this doesn't show there. But `generateUnionElement` (`pkg/codegen/union.go:89`) names the array branch from that same path. #1878 had a similar shape: an array with an `enum` of its own and an enum on its items, both called `ApprovalConfigurationExclude`.\n","author":{"url":"https://github.com/BogdanYarotsky","@type":"Person","name":"BogdanYarotsky"},"datePublished":"2026-10-29T06:29:10Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2640"}
| 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:4730a58c-606b-f234-7292-e5dcf2811c75 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B826:7CC9A:14D2682:1B6C903:6AF9BAB7 |
| html-safe-nonce | 518d56c1b8b2dc02fd3e63a5185015697f0fd279674d1f4e391851690e461110 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODI2OjdDQzlBOjE0RDI2ODI6MUI2QzkwMzo2QUY5QkFCNyIsInZpc2l0b3JfaWQiOiI4OTIxMzIwNDQwNTU5NDE4MDM5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1efad75c94beb89ea138beafea7bbe84fbde8e09cfcc7f2c2b995e0b53e58f3e |
| hovercard-subject-tag | issue:6094276579 |
| 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/2640/issue_layout |
| twitter:image | https://opengraph.githubassets.com/67a9bbfe7618618876918e88af3c909728fb112f4c25e60a5873d8ca2b801ca1/oapi-codegen/oapi-codegen/issues/2640 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/67a9bbfe7618618876918e88af3c909728fb112f4c25e60a5873d8ca2b801ca1/oapi-codegen/oapi-codegen/issues/2640 |
| og:image:alt | When a union branch is an array whose items are an inline enum, the branch's type and the items' enum type get the same name (...Roles1). In a request body or a parameter, generation succeeds and t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | BogdanYarotsky |
| 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