Support for polymorphic types is unclear in the spec
While trying to work through apigee-127/swagger-tools#241 it became clear that the spec is not clear on how polymorphic types are intended to work. @webron will add more details.
I'm looking for docs tooling that displays polymorphism either as defined in version 2 spec or version 3 spec.
In my research the only docs tooling that handles and displays polymorphism is Swagger UI using the v3 spec.
The other docs tools that I've come across only support version 2 of the spec but I can't seem to find any that handle polymorphism as defined in the v2 spec.
Any help is appreciated!
In addition to swagger-ui, Rebilly ReDoc supports discriminator based polymorphism, and recent alphas support OpenAPI 3.0.x
https://github.com/Rebilly/ReDoc/blob/master/docs/images/discriminator-demo.gif
Related to this ambiguity, the spec is not very clear about how to represent lists of polymorphic types. For example, suppose the "Pet Store" API can return a list of pets:
store:
properties:
pets:
description: The collection of pets in this store
type: array
items:
- $ref: '#/components/schemas/Pet'
The intent of the API designer could be one of:
- Return a non-polymorphic list of pets, i.e. just the pets, not dogs and cats
- Return a polymorphic list of pets, where each item in the array is a "subclass" of pet, i.e. list of dogs, cats, possibly "pet"
How do you capture this intent in a OpenAPI spec? How do you specify any "subtype of"? For 2), the expectations are not very clear. The API designer may have the following approach in their mind:
- The "pets" property is a list of '#/components/schemas/Pet',
- The Pet schema specifies "petType" as a discriminator,
- The API server returns a heterogeneous list of pets, where each "petType" in the list is the value of the specialized type.
With this approach, the API designer might expect the client to parse the value of the discrimanator, resolve the type from the OpenAPI spec, and instantiate the proper implementation type, for each item in the array.
Alternatively, "oneOf" (as shown below) is one way to explicitly list all possible types, but it gets unwieldy when there are many subtypes. In addition, it seems the tooling designer would not be able to leverage polymorphic features from programming languages, such as interfaces and classes.
store:
properties:
pets:
description: The collection of pets in this store
type: array
items:
oneOf:
- $ref: '#/components/schemas/Pet'
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Cat'
Can I use a defined enum property as discriminator?
"PetType": {
"type": "string",
"enum": [
"CAT",
"DOG"
]
}
This was fixed by a PR in 2017, so I'm closing it. To answer the three subsequent questions:
- our only additional docs are on learn.openapis.org (to which you can contribute; we don't keep track of other documentation/advice sites
- as the point of
discriminatoris to tell tools how to instantiate the correct subclass, that is what it does regardless of whether it is in a collection or not, for each instance to which that schema object applies - yes, you can definitely use
type: string, enum: [...]for your discriminating property