Skip to content

Package Imports Not Set on Response Models Containing External Refs #1087

Closed

Description

@Rmarken5

When generating server response models, external package imports are not being set on struct properties

Example of reproduce the issue:

Main spec

openapi: 3.0.3
info:
  title: test
  description: "The API for Test"
  version: 2.0.0
servers:
  - url: http://localhost:8000
tags:
  - name: Tag
    description: Foo Bar
paths:
  /api/my/path:
    get:
      tags:
        - Tag
      summary: list things
      description: my list of things
      operationId: getThings
      responses:
        200:
          $ref: "#/components/responses/ThingResponse"
        304:
          $ref: "#/components/responses/304"
        401:
          $ref: "./deps/my-deps.json#/components/responses/401"
        403:
          $ref: "./deps/my-deps.json#/components/responses/403"
        404:
          $ref: "#/components/responses/404"
        500:
          $ref: "./deps/my-deps.json#/components/responses/DefaultError"
components:
  securitySchemes:
    bearerAuthWebhook:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: The JWT access token.
  responses:
    Empty:
      description: No content
    ThingResponse:
      description: List of Things
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/ThingList"
    304:
      description: Response when client should use cached results
      headers:
        Cache-Control:
          description: defines TTL of resource in seconds, indicates that the response can be stored in caches, and indicates that the response can be stored only in a private cache
          example: "max-age: 86400, must-revalidate, private"
          schema:
            type: string
        ETag:
          description: "indicate the current version of the resource"
          schema:
            type: string
    404:
      description: Not Found. Resource could not be found
      content:
        application/json:
          schema:
            $ref: "./deps/my-deps.json#/components/schemas/Error"
          example:
            code: 404
            domain: "Foo"
            message: "Not Found. Subscription could not be found"
            reason: "Not_Found"
    409:
      description: Conflict. Resource already exists
      content:
        application/json:
          schema:
            $ref: "./deps/my-deps.json#/components/schemas/Error"
          example:
            code: 404
            domain: "Webhook"
            message: "Conflict. Resource already exists"
            reason: "Conflict"
    410:
      description: Gone. Record no longer available
      content:
        application/json:
          schema:
            $ref: "./deps/my-deps.json#/components/schemas/Error"
          example:
            code: 403
            domain: ""
            message: "Gone"
            reason: "Already deleted"
  schemas:
    Thing:
      type: object
      properties:
        name:
          type: string
          description: just a name
      required: [ name]
    ThingList:
      type: object
      description: Object containing list of Things
      properties:
        keys:
          type: array
          items:
            $ref: "#/components/schemas/Thing"
      required: [ keys ]

Config

package: external_refs_issue
output:
  api.gen.go
generate:
  chi-server: true
  models: true
  embedded-spec: false
  client: true
import-mapping:
  ./deps/my-deps.json: github.com/deepmap/oapi-codegen/internal/test/issues/external-refs-issue/deps

Referenced Spec

{
  "openapi": "3.0.3",
  "info": {
    "title": "Models",
    "description": "",
    "version": "2.0.0"
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {},
  "components": {
    "responses": {
      "DefaultError": {
        "description": "Default error response for any errors in the API",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "403": {
        "description": "Forbidden. Indicates that request cannot be authorized",
        "content": {
          "text/plain": {
            "schema": {
              "type": "string",
              "example": "Access Denied"
            }
          },
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "code": 403,
              "domain": "Auth",
              "message": "Access Denied",
              "reason": "Access_Denied"
            }
          }
        }
      },
      "401": {
        "description": "Unauthorized. Indicates that provided credentials is not valid",
        "content": {
          "text/plain": {
            "schema": {
              "type": "string",
              "example": "Jwt is expired"
            }
          },
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "code": 401,
              "domain": "Auth",
              "message": "Invalid authentication",
              "reason": "Invalid_Authentication"
            }
          }
        }
      },
      "410": {
        "description": "Record no longer available",
        "content": {
          "text/plain": {
            "schema": {
              "type": "string",
              "example": "Gone"
            }
          },
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "code": 410,
              "domain": "",
              "message": "Gone",
              "reason": "Gone"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "allOf": [
          {
            "$ref": "#/components/schemas/BaseError"
          }
        ],
        "required": [
          "code",
          "domain",
          "message",
          "reason"
        ],
        "type": "object",
        "properties": {
          "reason": {
            "type": "string",
            "description": "A reason code specific to the service and can be used to identify the exact issue. Should be unique within a domain",
            "example": "Reason_Code"
          }
        }
      },
      "BaseError": {
        "required": [
          "code",
          "domain",
          "message"
        ],
        "type": "object",
        "properties": {
          "code": {
            "type": "integer",
            "description": "The underlying http status code",
            "format": "int32",
            "example": 500
          },
          "message": {
            "type": "string",
            "description": "A simple message in english describing the error and can be returned to the consumer",
            "example": "Age cannot be less than 18"
          },
          "domain": {
            "type": "string",
            "description": "The domain where the error is originating from as defined by the service",
            "example": ""
          },
          "metadata": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            },
            "description": "Any additional details to be conveyed as determined by the service. If present, will return map of key value pairs",
            "example": {
              "propertyName": "propertyName is required"
            }
          }
        }
      }
    }
  }
}

Struct with Error

type GetThingsResponse struct {
	Body         []byte
	HTTPResponse *http.Response
	JSON200      *ThingList
	JSON401      *Error
	JSON403      *Error
	JSON404      *externalRef0.Error
	JSON500      *Error
}

JSON401, JSON403, JSON500 should have reference to externalRef0

Activity

  1. jamietanna commented on Jan 19, 2024

    @jamietanna
    Member

    @Rmarken5 should this now be closed, since your PR was merged?

  2. self-assigned this
    on Jan 19, 2024
  3. Rmarken5 commented on Jan 19, 2024

    @Rmarken5
    ContributorAuthor

    @Rmarken5 should this now be closed, since your PR was merged?

    Yes I believe this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions