Skip to content

allOf: types are generated not properly #2102

@lnshkv

Description

@lnshkv
  • 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"`
}

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