Skip to content

$ref in responses is not adding the imported reference properly #2113

Closed

Description

@angelcervera

Using this model definition as an example:

openapi: 3.0.4

components:
  schemas:
    ProblemDetails:
      type: object
      description: RFC 7807 Problem Details
      properties:
        type:
          type: string
          format: uri
          description: A URI reference that identifies the problem type
        title:
          type: string
          description: A short, human-readable summary of the problem type
        status:
          type: integer
          description: The HTTP status code
      required: [title, status]
  responses:
    StandardError:
      description: Error response using RFC 7807 Problem Details
      content:
        application/problem+json:
          schema:
            $ref: "#/components/schemas/ProblemDetails"

Next definition is working fine:

        default:
          description: Error (RFC 7807)
          content:
            application/problem+json:
              schema:
                $ref: "./common/problemdetails.yaml#/components/schemas/ProblemDetails"

generating this code:

import externalRef3 "github.com/XXXXXX/YYYYYY/generated/go/common/problemdetails"

type UsersListdefaultApplicationProblemPlusJSONResponse struct {
	Body       externalRef3.ProblemDetails
	StatusCode int
}

But this definition is not generating the right code :

        default:
          $ref: "./common/problemdetails.yaml#/components/responses/StandardError"

generating this code, where is not using the reference to access to the model:

import externalRef3 "github.com/XXXXXX/YYYYYY/generated/go/common/problemdetails"

type UsersListdefaultApplicationProblemPlusJSONResponse struct {
	Body       ProblemDetails // FAILING HERE. Should be externalRef3.ProblemDetails
	StatusCode int
}

Next is the config file:

package: users
output: ../../../../generated/go/users/handlers.chi.gen.go
generate:
  models: true
  embedded-spec: true
  strict-server: true
  chi-server: true
output-options:
  skip-prune: true
import-mapping:
  ./common/problemdetails.yaml: "github.com/XXXXXX/YYYYYY/generated/go/common/problemdetails"

Activity

  1. tebruno99 commented on Dec 7, 2025

    @tebruno99

    Same issue here, I tried placing all of my request and response bodies in a common package. The generated code only references that package for schemas, not requests or responses:

    externalRef0 "server-openapi/pkg/apigen/common"

    type PostAuthLoginRequestObject struct {
    	Body *PostAuthLoginJSONRequestBody
    }
    

    should be:

    type PostAuthLoginRequestObject struct {
    	Body *externalRef0.PostAuthLoginJSONRequestBody
    }
    
  2. tebruno99 commented on Dec 23, 2025

    @tebruno99

    This only happens when the response in the import-mapping file references schema that is defined in the imported file.

  3. descensus commented on Dec 30, 2025

    @descensus

    I think I'm running into a similar problem.
    I can't generate types (models) for anything that is referenced in another file, no matter if its responses or parameters etc.
    Generating types if I have everything in the same file works fine.

    It builds without errors though.
    is import-mapping completely broken or is there some syntax wizardry that is needed?

    Can't even get https://github.com/oapi-codegen/oapi-codegen/tree/main/examples/import-mapping to work though
    Running v2.5.1 on MacOS.

    Tried earlier versions as well, no diff.

  4. mromaszewicz commented on Feb 24, 2026

    @mromaszewicz
    Member

    Interesting. This works as you expect for numerical HTTP result codes, however, the strict server does something different for default, which is why it breaks.

  5. added a commit that references this issue on Feb 24, 2026
    71fc215
  6. added a commit that references this issue on Feb 27, 2026
    d04991e
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions