You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
v2.8.0: identical schemas with the same x-go-type-name now fail with "duplicate typename" #2599
In v2.8.0, two properties with the same inline enum and the same x-go-type-name fail to generate:
error generating code: error generating code for type definitions: duplicate typename 'Product' detected, can't auto-rename, please use x-go-name to specify your own name for one of them
v2.7.2 generates one Product type for both, as expected.
go run github.com/oapi-codegen/oapi-codegen/v2/cmd/[email protected] --config=cfg.yaml spec.yaml # ok
go run github.com/oapi-codegen/oapi-codegen/v2/cmd/[email protected] --config=cfg.yaml spec.yaml # duplicate typename 'Product'
Cause
v2.8.0 sets loader.IncludeOrigin = true in pkg/util/loader.go. Each loaded schema now carries an Origin with its file line and column.
TypeDefinitionsEquivalent in pkg/codegen/utils.go compares schemas with reflect.DeepEqual(t1.Schema.OAPISchema, t2.Schema.OAPISchema). Two schemas that are the same but sit on different lines now have different Origin values, so they never match, and GenerateTypes returns the duplicate error instead of merging them.
When we dumped the two colliding schemas, their JSON was the same. Only Origin was different.
Possible fix
Ignore Origin when comparing, for example by comparing copies with Origin set to nil, or by comparing the marshaled JSON.
Versions
Broken: v2.8.0, and main as of 2026-10-06 (same TypeDefinitionsEquivalent)
Reproduced on v2.8.0 and main (43281d1). v2.7.2 works.
Some findings before choosing a fix:
Nested and recursive schemas fail too. Clearing only the top-level Origin doesn't fix them, because nested schemas and SchemaRefs have their own Origin.
Comparing json.Marshal output fixes those cases, but it treats $refs that resolve to different targets as equal. For example, the same relative $ref from two directories, or a 3.1 $ref with a type: [object, "null"] sibling. Those schemas would be merged silently instead of reported.
Library users hit this before v2.8.0 too. codegen.Generate from v2.7.2 fails the same way when the spec is loaded with IncludeOrigin = true.
My proposal is to fix TypeDefinitionsEquivalent with a reflect.DeepEqual-style comparison that skips *openapi3.Origin fields and detects cycles. That keeps the v2.7.2 behaviour without a new dependency. go-cmp with IgnoreFields also passed all of these cases, if you'd rather add the dependency.
I'd like to send a PR with tests for these cases. Does this approach work for you?
Activity
eyoshioka commentedon Oct 8, 2026
Reproduced on v2.8.0 and
main(43281d1). v2.7.2 works.Some findings before choosing a fix:
Origindoesn't fix them, because nested schemas andSchemaRefs have their ownOrigin.json.Marshaloutput fixes those cases, but it treats$refs that resolve to different targets as equal. For example, the same relative$reffrom two directories, or a 3.1$refwith atype: [object, "null"]sibling. Those schemas would be merged silently instead of reported.codegen.Generatefrom v2.7.2 fails the same way when the spec is loaded withIncludeOrigin = true.My proposal is to fix
TypeDefinitionsEquivalentwith areflect.DeepEqual-style comparison that skips*openapi3.Originfields and detects cycles. That keeps the v2.7.2 behaviour without a new dependency. go-cmp withIgnoreFieldsalso passed all of these cases, if you'd rather add the dependency.I'd like to send a PR with tests for these cases. Does this approach work for you?
Specs (models only,
skip-prune: true)I used Claude to help with the investigation, and I re-ran all of the results above myself.
fix: ignore kin-openapi source locations when comparing type definiti…