OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Support for polymorphic types is unclear in the spec

Open daveismith opened this issue 11 years ago • 26 comments

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.

daveismith avatar Jul 10 '15 16:07 daveismith

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!

debri174 avatar Mar 15 '18 20:03 debri174

In addition to swagger-ui, Rebilly ReDoc supports discriminator based polymorphism, and recent alphas support OpenAPI 3.0.x

MikeRalphson avatar Mar 15 '18 20:03 MikeRalphson

https://github.com/Rebilly/ReDoc/blob/master/docs/images/discriminator-demo.gif

raderio avatar Apr 03 '18 20:04 raderio

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:

  1. Return a non-polymorphic list of pets, i.e. just the pets, not dogs and cats
  2. 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:

  1. The "pets" property is a list of '#/components/schemas/Pet',
  2. The Pet schema specifies "petType" as a discriminator,
  3. 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'

sebastien-rosset avatar Oct 16 '19 14:10 sebastien-rosset

Can I use a defined enum property as discriminator? "PetType": { "type": "string", "enum": [ "CAT", "DOG" ] }

Yingsheng-eroad avatar Jul 05 '20 23:07 Yingsheng-eroad

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 discriminator is 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

handrews avatar May 19 '24 18:05 handrews