Skip to content

Separate packages for models and server part aren't resolved #2078

Open

Description

@Yuno-obsessed

I am trying to place my server generated part in one package and models that it has to use in another package. But when my api has a query parameter, server part's code is not importing model's package, thus can't resolve it, giving an error.

oapi-codegen version: v2.5.0
go version: 1.24.5
os: windows

Config for models:

package: dto
generate:
  models: true
output: internal/application/dto/gen.go

Config for server:

package: rest
generate:
  fiber-server: true
output: internal/presentation/rest/gen.go
import-mapping:
  "#/components/schemas": .../internal/application/dto

Api's definition:

/payment/status:
    get:
      summary: Gets a payment checkout session info
      description: Returns a payment checkout session info
      operationId: getPaymentSession
      parameters:
        - name: session_id
          in: query
          description: Session id to query
          required: true
          schema:
            type: string

Code generated (without import and reference to package "dto"):

	GetPaymentSession(c *fiber.Ctx, params GetPaymentSessionParams) error

Activity

  1. mromaszewicz commented on Jul 13, 2026

    @mromaszewicz
    Member

    This is working as designed, though the config needed is a bit different from what you have:

    1. import-mapping keys are external document paths, not JSON pointers — "#/components/schemas": ... never matches anything, because internal $refs always resolve within the current package. To put schemas in a separate Go package, split the spec into two files: move shared schemas into e.g. models.yaml and reference them from your API spec as $ref: "./models.yaml#/components/schemas/Foo". Then in the server config: import-mapping: {"./models.yaml": "yourmodule/internal/application/dto"}. See examples/import-mapping/multiplepackages/ for a complete working setup.

    2. The specific error you're seeing is different, though: GetPaymentSessionParams is generated from your inline query parameter — it's an operation-scoped type, not a component schema, so it always belongs to the server's package. Add models: true to your server config (alongside fiber-server: true) and it will be declared there. Component schemas you've $ref'd into models.yaml will still come from the dto package via the import mapping; only operation-derived types (Params structs, request/response bodies) stay with the server.

    If you don't actually need separate Go packages — just separate files — the simpler pattern is generating both outputs into the same package (see examples/import-mapping/samepackage/, which uses the special mapping value "-").

    Closing as working-as-intended — happy to reopen if the suggested config doesn't resolve it.

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

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions