Skip to content

allOf with cross-file does not qualify nested type references from source schema #2288

Closed

Description

@xiongzubiao

Description

When using allOf to extend a schema from another file, oapi-codegen flattens the fields correctly but does not qualify nested type references that originate from the source schema's file. This produces uncompilable Go code.

Reproduction

Given two OpenAPI spec files with an import-mapping configured:

api/common/openapi.yaml

openapi: 3.0.0
info:
  title: Common
  version: 0.1.0
paths: {}
components:
  schemas:
    Role:
      type: string
      enum: [admin, user]
    User:
      type: object
      required: [id, username]
      properties:
        id:
          type: string
        username:
          type: string
        roles:
          type: array
          items:
            $ref: '#/components/schemas/Role'

api/service/openapi.yaml

openapi: 3.0.0
info:
  title: Service
  version: 0.1.0
paths: {}
components:
  schemas:
    EnrichedUser:
      allOf:
      - $ref: '../common/openapi.yaml#/components/schemas/User'
      - type: object
        properties:
          extra_field:
            type: string

api/service/types.yaml (codegen config)

package: service
generate:
  models: true
output: types.gen.go
import-mapping:
  '../common/openapi.yaml': common github.com/example/api/common

Expected behavior

Generated EnrichedUser should qualify the Role reference via the import mapping:

type EnrichedUser struct {
    ExtraField *string        `json:"extra_field,omitempty"`
    ID         string         `json:"id"`
    Roles      *[]common.Role `json:"roles,omitempty"`
    Username   string         `json:"username"`
}

Actual behavior

Generated code uses an unqualified Role reference:

type EnrichedUser struct {
    ExtraField *string    `json:"extra_field,omitempty"`
    ID         string     `json:"id"`
    Roles      *[]Role    `json:"roles,omitempty"`  // <-- unqualified
    Username   string     `json:"username"`
}

This fails to compile: undefined: Role

Workaround

Override the field in the allOf extension with a direct cross-file $ref:

    EnrichedUser:
      allOf:
      - $ref: '../common/openapi.yaml#/components/schemas/User'
      - type: object
        properties:
          roles:
            type: array
            items:
              $ref: '../common/openapi.yaml#/components/schemas/Role'
          extra_field:
            type: string

This correctly generates Roles *[]common.Role.

Version

oapi-codegen v2.6.0

Activity

  1. svip commented on Mar 13, 2026

    @svip

    I think it may be due to the fact that Role is an enum; I had a more basic scenario, where role was defined inline, e.g.

    roles:
      type: array
      items:
        type: string
        enum: [admin, user]
    

    The above would create a similar issue; but then the workaround doesn't work, since it then doesn't create a new correct type (at least when you do your allOf in a path context); so in that case, the workaround is to separate out the enum-type as a separate definition (e.g. the Role type as above), and then reference it as the issue's workaround suggests.

  2. added 3 commits that reference this issue on Mar 23, 2026
    0ecc287
    ee50af8
    f673c70
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