You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:modeltypeTeslaCarinterface {
// The model of tesla car//// discriminator: true// swagger:name modelModel() string// AutoPilot returns true when it supports autopilot// swagger:name autoPilotAutoPilot() bool
}
// The ModelS version of the tesla car//// swagger:model modelStypeModelSstruct {
// swagger:allOf com.tesla.models.ModelSTeslaCar// The edition of this Model SEditionstring`json:"edition"`
}
// The ModelX version of the tesla car//// swagger:model modelXtypeModelXstruct {
// swagger:allOf com.tesla.models.ModelXTeslaCar// The number of doors on this Model XDoorsint32`json:"doors"`
}
// Add a reference to tie TeslaCar into the type definitions hierarchy...// swagger:response carResponsetypeCarResponsestruct {
// in: bodyBodystruct {
ResultintVehicleTeslaCar
}
}
routes.go
...// swagger:route GET /car GetCar//// Gets a Tesla car//// Produces:// - application/json//// Responses:// 200: carResponserouter.Handle("/", makeCarHandler()).Methods("GET")
...
Generated Swagger Specification
basePath: /consumes:
- application/jsondefinitions:
TeslaCar:
description: TeslaCar is a tesla cardiscriminator: modelproperties:
autoPilot:
description: AutoPilot returns true when it supports autopilottype: booleanx-go-name: AutoPilotmodel:
description: The model of tesla cartype: stringx-go-name: Modeltype: objectx-go-package: github.com/foo/barinfo:
description: Package main Car APIversion: "1.0"paths:
/car:
get:
description: Gets a job documentoperationId: GetRootproduces:
- application/jsonresponses:
"200":
$ref: '#/responses/carResponse'produces:
- application/jsonresponses:
carResponse:
schema:
properties:
Result:
format: int64type: integerVehicle:
$ref: '#/definitions/TeslaCar'type: objectschemes:
- http
- httpsswagger: "2.0"
Steps to reproduce
Create new go project with the above code.
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: int64type: integerVehicle:
$ref: '#/definitions/TeslaCar'type: objecttype: objectx-go-package: github.com/foo/barTeslaCar:
description: TeslaCar is a tesla cardiscriminator: modelproperties:
autoPilot:
description: AutoPilot returns true when it supports autopilottype: booleanx-go-name: AutoPilotmodel:
description: The model of tesla cartype: stringx-go-name: Modeltype: objectx-go-package: github.com/foo/barmodelS:
allOf:
- $ref: '#/definitions/TeslaCar'
- properties:
edition:
description: The edition of this Model Stype: stringx-go-name: Editiontype: objectdescription: The ModelS version of the tesla carx-class: com.tesla.models.ModelSx-go-name: ModelSx-go-package: github.com/foo/barmodelX:
allOf:
- $ref: '#/definitions/TeslaCar'
- properties:
doors:
description: The number of doors on this Model Xformat: int32type: integerx-go-name: Doorstype: objectdescription: The ModelX version of the tesla carx-class: com.tesla.models.ModelXx-go-name: ModelXx-go-package: github.com/foo/barinfo:
description: Package main Car APIversion: "1.0"paths: {}produces:
- application/jsonresponses:
carResponse:
schema:
properties:
Result:
format: int64type: integerVehicle:
$ref: '#/definitions/TeslaCar'type: objectschemes:
- http
- httpsswagger: "2.0"
Environment
swagger version: dev
go version: go1.12 darwin/amd64
OS: macOS 10.14.3
The text was updated successfully, but these errors were encountered:
Problem statement
Sub-types are not emitted for a discriminated type when using go annotations to generate an OpenAPI spec.
Annotated go
openapi.go
routes.go
Generated Swagger Specification
Steps to reproduce
swagger generate spec -o swagger.yaml
Note
Running the generate command with
-m/--scan-models
does force the subtypes to be emitted in thedefinitions
array. However, this comes with the unfortunate side-effect of also generating a model for everyswagger:response
indefinitions
as well (see example below).I say unfortunate since having to hand edit/cleanup the resulting spec breaks our CI/CD automation.
Environment
swagger version:
dev
go version:
go1.12 darwin/amd64
OS: macOS 10.14.3
The text was updated successfully, but these errors were encountered: