Skip to content

v2.8.0: identical schemas with the same x-go-type-name now fail with "duplicate typename" #2599

Closed

Description

@castonagentsync

What happens

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.

Repro

spec.yaml:

openapi: 3.0.3
info:
  title: repro
  version: 1.0.0
paths:
  /clients:
    post:
      operationId: createClient
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateClientRequest'
      responses:
        '200':
          description: ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Client'
components:
  schemas:
    Client:
      type: object
      properties:
        product:
          type: string
          enum: [MANAGE, API]
          x-go-type-name: Product
    CreateClientRequest:
      type: object
      properties:
        product:
          type: string
          enum: [MANAGE, API]
          x-go-type-name: Product

cfg.yaml:

package: repro
generate:
  models: true
  client: true
output: out.go
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)
  • Works: v2.7.2
  • Go 1.26.2

Activity

  1. eyoshioka commented on Oct 8, 2026

    @eyoshioka
    Contributor

    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?

    Specs (models only, skip-prune: true)
    # nested: fails on v2.8.0
    openapi: 3.0.3
    info: {title: t, version: "1"}
    paths: {}
    components:
      schemas:
        Client:
          type: object
          properties:
            address: {type: object, x-go-type-name: Address, properties: {city: {type: string}}}
        CreateClientRequest:
          type: object
          properties:
            address: {type: object, x-go-type-name: Address, properties: {city: {type: string}}}
    # recursive: fails on v2.8.0
    openapi: 3.0.3
    info: {title: t, version: "1"}
    paths: {}
    components:
      schemas:
        Node: {type: object, properties: {child: {$ref: '#/components/schemas/Node'}}}
        Client:
          type: object
          properties:
            product: {type: object, x-go-type-name: Product, properties: {node: {$ref: '#/components/schemas/Node'}}}
        CreateClientRequest:
          type: object
          properties:
            product: {type: object, x-go-type-name: Product, properties: {node: {$ref: '#/components/schemas/Node'}}}
    # 3.1 sibling: really different, so the duplicate error is correct (v2.7.2 reports it)
    openapi: 3.1.0
    info: {title: t, version: "1"}
    paths: {}
    components:
      schemas:
        Base: {type: object, properties: {id: {type: string}}}
        Client:
          type: object
          properties:
            product: {type: object, x-go-type-name: Product, properties: {base: {$ref: '#/components/schemas/Base', type: [object, "null"]}}}
        CreateClientRequest:
          type: object
          properties:
            product: {type: object, x-go-type-name: Product, properties: {base: {$ref: '#/components/schemas/Base'}}}

    I used Claude to help with the investigation, and I re-ran all of the results above myself.

  2. added a commit that references this issue on Nov 9, 2026
    b4fdfe1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions