oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

allOf: types are generated not properly

Open lnshkv opened this issue 5 months ago • 1 comments

  • Go version: go1.24.7 linux/amd64
  • oapi-codegen version: v2.5.0
  • Configuration: default
  • OpenAPI spec: 3.0.0
  • Problem:

Consider a simple api.yaml:

openapi: 3.0.0
info:
  title: api
  version: 1.0.0
paths:
  /foo:
    get:
      summary: foo
      responses:
        "200":
          description: foo
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
  /bar:
    get:
      summary: bar
      responses:
        "200":
          description: bar
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Bar'
components:
  schemas:
    Foo:
      type: object
      required:
        - foo
      properties:
        foo:
          type: string
    Bar:
      type: object
      properties:
        bar:
          type: string
      allOf:
        - $ref: '#/components/schemas/Foo'
        - required:
            - bar

Invoking oapi-codegen -generate types api.yaml, one ends up with:

// Package api provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.0 DO NOT EDIT.
package api

// Bar defines model for Bar.
type Bar struct {
	Foo string `json:"foo"`
}

// Foo defines model for Foo.
type Foo struct {
	Foo string `json:"foo"`
}

This output is not what is expected.

  • Expected:
// Package api provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.0 DO NOT EDIT.
package api

// Bar defines model for Bar.
type Bar struct {
	Bar string `json:"bar"`
	Foo string `json:"foo"`
}

// Foo defines model for Foo.
type Foo struct {
	Foo string `json:"foo"`
}

The above output could be achieved by supplying the following api.yaml:

openapi: 3.0.0
info:
  title: api
  version: 1.0.0
paths:
  /foo:
    get:
      summary: foo
      responses:
        "200":
          description: foo
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
  /bar:
    get:
      summary: bar
      responses:
        "200":
          description: bar
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Bar'
components:
  schemas:
    Foo:
      type: object
      required:
        - foo
      properties:
        foo:
          type: string
    Bar:
      allOf:
        - $ref: '#/components/schemas/Foo'
        - type: object
          required:
            - bar
          properties:
            bar:
              type: string
  • Additional info: the first api.yaml example without paths is rendered by cue export --out openapi+yaml api.cue
// api.cue
#Foo: {
	foo: string
	...
}

#Bar: #Foo & {
	bar: string
}

I'm not sure which behavior is the most valid. For comparison, openapi-generator-cli gives even weirder output:

// Bar struct for Bar
type Bar struct {
	Bar *string `json:"bar,omitempty"`
	Foo string `json:"foo"`
}

lnshkv avatar Sep 26 '25 19:09 lnshkv

I also have the same issue with a schema i got and have to use. I tried a fix but was not working completely because then referenced types with included allOf or oneOff where also not working. When I also can fix that, I would check and open a pull request.

kruegge avatar Nov 17 '25 16:11 kruegge