Repository navigation
Closed
Description
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: stringapi/service/types.yaml (codegen config)
package: service
generate:
models: true
output: types.gen.go
import-mapping:
'../common/openapi.yaml': common github.com/example/api/commonExpected 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: stringThis correctly generates Roles *[]common.Role.
Version
oapi-codegen v2.6.0
Reacted by Kean Pedersen and Christophe de Vienne
Metadata
Metadata
Assignees
Labels
No labels
Activity
svip commentedon Mar 13, 2026
I think it may be due to the fact that
Roleis anenum; I had a more basic scenario, whererolewas defined inline, e.g.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
allOfin a path context); so in that case, the workaround is to separate out theenum-type as a separate definition (e.g. theRoletype as above), and then reference it as the issue's workaround suggests.Propagate external refs into allOf
Propagate external refs into allOf
Propagate external refs through allOf/anyOf/oneOf/not sub-schemas
Propagate external refs through allOf/anyOf/oneOf/not sub-schemas
Propagate external refs through allOf/anyOf/oneOf/not sub-schemas (#2299
feat(v3): allOf can only annotate external and x-go-type schemas (#2568)