Skip to content

Missing package name on type from common API spec when referencing a path operation from another spec in generated code #1378

@markuswustenberg

Description

@markuswustenberg

We're hitting a bug where generated code doesn't compile when using API specs referencing each other. This may or may not be related to #695, so I'm opening a separate issue just to be sure.

The setup is this (also available at https://github.com/maragudk/oapi-codegen-bug):

  • An api.yaml spec references a path in a common.yaml spec.
  • In common.yaml, a path /ping references a schema PingResponse in the same file.

When the spec is generated for api.yaml, PingResponse doesn't have the externalRef0 package name prefix, and so the generated code can't compile.

This is with v2 of oapi-codegen.

api.yaml

version: "3.0.0"

paths:
  /ping:
    $ref: "common.yaml#/paths/~1ping"

config.yaml

package: api
generate:
  gin-server: true
  embedded-spec: true
  strict-server: true
  models: true
import-mapping:
  common.yaml: github.com/maragudk/oapi-codegen-bug/oapi/common
output: api.gen.go

common.yaml

version: "3.0.0"

paths:
  /ping:
    get:
      responses:
        200:
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/PingResponse"

components:
  schemas:
    PingResponse:
      type: object
      properties:
        message:
          type: string

config.yaml

package: common
generate:
  gin-server: true
  embedded-spec: true
  strict-server: true
  models: true
output: common.gen.go

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