Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub-types not generated from go discriminated type #1913

Open
zgramana opened this issue Mar 29, 2019 · 0 comments
Open

Sub-types not generated from go discriminated type #1913

zgramana opened this issue Mar 29, 2019 · 0 comments
Labels
generate spec Related to spec generation from code scanner

Comments

@zgramana
Copy link

zgramana commented Mar 29, 2019

Problem statement

Sub-types are not emitted for a discriminated type when using go annotations to generate an OpenAPI spec.

Annotated go

openapi.go

// From https://goswagger.io/use/spec/discriminated.html

// TeslaCar is a tesla car
//
// swagger:model
type TeslaCar interface {
	// The model of tesla car
	//
	// discriminator: true
	// swagger:name model
	Model() string

	// AutoPilot returns true when it supports autopilot
	// swagger:name autoPilot
	AutoPilot() bool
}

// The ModelS version of the tesla car
//
// swagger:model modelS
type ModelS struct {
	// swagger:allOf com.tesla.models.ModelS
	TeslaCar
	// The edition of this Model S
	Edition string `json:"edition"`
}

// The ModelX version of the tesla car
//
// swagger:model modelX
type ModelX struct {
	// swagger:allOf com.tesla.models.ModelX
	TeslaCar
	// The number of doors on this Model X
	Doors int32 `json:"doors"`
}

// Add a reference to tie TeslaCar into the type definitions hierarchy...

// swagger:response carResponse
type CarResponse struct {
	// in: body
	Body struct {
		Result  int
		Vehicle TeslaCar
	}
}

routes.go

...
	// swagger:route GET /car GetCar
	//
	// Gets a Tesla car
	//
	//   Produces:
	//   - application/json
	//
	//   Responses:
	//     200: carResponse
	router.Handle("/", makeCarHandler()).Methods("GET")
...

Generated Swagger Specification

basePath: /
consumes:
- application/json
definitions:
  TeslaCar:
    description: TeslaCar is a tesla car
    discriminator: model
    properties:
      autoPilot:
        description: AutoPilot returns true when it supports autopilot
        type: boolean
        x-go-name: AutoPilot
      model:
        description: The model of tesla car
        type: string
        x-go-name: Model
    type: object
    x-go-package: github.com/foo/bar
info:
  description: Package main Car API
  version: "1.0"
paths:
  /car:
    get:
      description: Gets a job document
      operationId: GetRoot
      produces:
      - application/json
      responses:
        "200":
          $ref: '#/responses/carResponse'
produces:
- application/json
responses:
  carResponse:
    schema:
      properties:
        Result:
          format: int64
          type: integer
        Vehicle:
          $ref: '#/definitions/TeslaCar'
      type: object
schemes:
- http
- https
swagger: "2.0"

Steps to reproduce

  1. Create new go project with the above code.
  2. Run swagger generate spec -o swagger.yaml

Note

Running the generate command with -m/--scan-models does force the subtypes to be emitted in the definitions array. However, this comes with the unfortunate side-effect of also generating a model for every swagger:response in definitions as well (see example below).

I say unfortunate since having to hand edit/cleanup the resulting spec breaks our CI/CD automation.

definitions:
  CarResponse:
    properties:
      Body:
        description: 'in: body'
        properties:
          Result:
            format: int64
            type: integer
          Vehicle:
            $ref: '#/definitions/TeslaCar'
        type: object
    type: object
    x-go-package: github.com/foo/bar
  TeslaCar:
    description: TeslaCar is a tesla car
    discriminator: model
    properties:
      autoPilot:
        description: AutoPilot returns true when it supports autopilot
        type: boolean
        x-go-name: AutoPilot
      model:
        description: The model of tesla car
        type: string
        x-go-name: Model
    type: object
    x-go-package: github.com/foo/bar
  modelS:
    allOf:
    - $ref: '#/definitions/TeslaCar'
    - properties:
        edition:
          description: The edition of this Model S
          type: string
          x-go-name: Edition
      type: object
    description: The ModelS version of the tesla car
    x-class: com.tesla.models.ModelS
    x-go-name: ModelS
    x-go-package: github.com/foo/bar
  modelX:
    allOf:
    - $ref: '#/definitions/TeslaCar'
    - properties:
        doors:
          description: The number of doors on this Model X
          format: int32
          type: integer
          x-go-name: Doors
      type: object
    description: The ModelX version of the tesla car
    x-class: com.tesla.models.ModelX
    x-go-name: ModelX
    x-go-package: github.com/foo/bar
info:
  description: Package main Car API
  version: "1.0"
paths: {}
produces:
- application/json
responses:
  carResponse:
    schema:
      properties:
        Result:
          format: int64
          type: integer
        Vehicle:
          $ref: '#/definitions/TeslaCar'
      type: object
schemes:
- http
- https
swagger: "2.0"

Environment

swagger version: dev
go version: go1.12 darwin/amd64
OS: macOS 10.14.3

@fredbi fredbi added the generate spec Related to spec generation from code label Mar 29, 2019
@fredbi fredbi added the scanner label Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generate spec Related to spec generation from code scanner
Projects
None yet
Development

No branches or pull requests

2 participants