Skip to content

Generating strict servers using 2.1.0 breaks casting of imported components when moving from 2.0.0. #2010

@MathewSharff

Description

@MathewSharff

Example code that breaks when generating using 2.1.0+:

package main

import (
	"fmt"

	"github.com/oapiexample/api"
	"github.com/oapiexample/otherapi"
)

func main() {
	var a api.GetExampleResponseObject

	var b otherapi.GetOtherExample400JSONResponse
	switch a.(type) {
	case api.GetExample400JSONResponse:
		b = otherapi.GetOtherExample400JSONResponse(a.(api.GetExample400JSONResponse)) // <- this line no longer compiles
	}

	fmt.Println(b.Message)
}

Setup:

open_api.yaml

openapi: 3.0.1
info:
  description: An example schema
  title: ExampleAPI
  version: 1.0.0
paths:
  /example:
    get:
      operationId: getExample
      responses:
        200:
          description: OK
        400:
          $ref: "#/components/responses/400"
components:
  responses:
    400:
      content:
        application/json:
          schema:
            properties:
              message:
                type: string
            required:
              - message
            type: object
      description: Bad Request

open_api.cfg.yaml

package: api
generate:
  chi-server: true
  models: true
  strict-server: true
output: ./api/api.go
output-options:
  skip-prune: true

open_api_other.yaml

openapi: 3.0.1
info:
  description: Another example schema
  title: OtherAPI
  version: 1.0.0
paths:
  /example:
    get:
      operationId: getOtherExample
      responses:
        200:
          description: OK
        400:
          $ref: "./open_api.yaml#/components/responses/400"

open_api_other.cfg.yaml

package: otherapi
generate:
  chi-server: true
  models: true
  strict-server: true
import-mapping:
  ./open_api.yaml: github.com/oapiexample/api/api
output: ./otherapi/api.go

generate.go

package oapiexample

//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@v2.0.0 --config open_api.cfg.yaml ./open_api.yaml
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@v2.0.0 --config open_api_other.cfg.yaml ./open_api_other.yaml

When I look at the generated code, this line changes from

type GetOtherExample400JSONResponse struct{ externalRef0.N400JSONResponse }

to

type GetOtherExample400JSONResponse struct{ externalRef0.N400 }

The GetExample400JSONResponse type that references the same response component continues to embed the N400JSONResponse type.

type GetExample400JSONResponse struct{ N400JSONResponse }

I believe pull request #1387 introduced this change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions