Repository navigation
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 component schema, generation fails with "duplicate typename", and the suggested x-go-name doesn't apply, since both names are generated.
"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.
Reproduction
openapi.yaml:
openapi: 3.0.3
info: {title: t, version: "1"}
paths:
/items:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
roles:
oneOf:
- {type: string}
- {type: array, items: {type: string, enum: [admin, viewer]}}
responses:
"204": {description: ok}$ oapi-codegen -generate types -package api -o api.gen.go openapi.yaml
$ go build ./...
./api.gen.go:21:6: invalid recursive type: PostItemsJSONBodyRoles1 refers to itself
./api.gen.go:24:6: PostItemsJSONBodyRoles1 redeclared in this block
./api.gen.go:21:6: other declaration of PostItemsJSONBodyRoles1No configuration file; the module requires github.com/oapi-codegen/runtime v1.8.0. The generated code:
// PostItemsJSONBodyRoles1 defines parameters for PostItems.
type PostItemsJSONBodyRoles1 = []PostItemsJSONBodyRoles1
// PostItemsJSONBodyRoles1 defines parameters for PostItems.
type PostItemsJSONBodyRoles1 stringOn main the first one is type PostItemsJSONBodyRoles1 []PostItemsJSONBodyRoles1 and only the "redeclared" error remains.
The same roles property as a query parameter's schema gives GetItemsParamsRoles1 redeclared. Under components.schemas.Item (referenced from a response) generation fails instead:
error 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
It 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.
Expected
Two distinct types, for example PostItemsJSONBodyRoles1 []PostItemsJSONBodyRoles1Item and PostItemsJSONBodyRoles1Item string, and code that compiles.
Versions
v2.8.1 and main (15f5714, v2.8.1-0.20261028140811-15f571472ff8), Go 1.27.3, linux/amd64.
Likely cause
An 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.
Activity
fix: give inline-enum items of a oneOf/anyOf array branch their own t…