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

$ref in responses is not adding the imported reference properly

Open angelcervera opened this issue 4 months ago • 1 comments

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"

angelcervera avatar Oct 24 '25 05:10 angelcervera

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
}

tebruno99 avatar Dec 07 '25 23:12 tebruno99

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

tebruno99 avatar Dec 23 '25 21:12 tebruno99