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

Full example of Discriminator

Open raderio opened this issue 7 years ago • 8 comments

https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

Where can I find a full example of swagger.json about discriminator?

raderio avatar Apr 05 '18 09:04 raderio

The canonical example is the Cat/Dog/Lizard one under this section of the OAS. As discriminator only has effect within schema objects, the rest of the OAS document boilerplate is not included.

MikeRalphson avatar Jun 25 '18 11:06 MikeRalphson

I find the Cat/Dog/Lizard example to be weak, incomplete and difficult to follow and would be interested in finding other examples. As of today, a Google search of "openapi discriminator example" yields a smattering of partial examples, plus one attempt at an example, last commented on 10 weeks ago which, alas, ends up just showing a frustrated user "struggling" (unsuccessfully) to get that example to work.

liquidaty avatar Feb 12 '20 19:02 liquidaty

An example in Java would be very useful.

It would also be very nice if the discriminator.propertyName could resolve to a property that's at the same level as the polymorphic object rather than forcing it to be a property inside the polymorphic object.

nkavian avatar Apr 27 '20 18:04 nkavian

@nkavian, would this make it difficult to reuse the polymorphic instances independently of a referring object? If an object needed two polymorphic references that included the same concrete types, would you need a discriminator discriminator ?

jheinnic avatar Aug 09 '20 23:08 jheinnic

In my case, the parent knows what type of child it's holding. I could have easily gotten the class name of the concrete child and put it into a property of the parent when serializing.

In comparison, the child is required to define a property with a fixed value that assigns a static name. It shouldn't be the child's job to create that property.

class ChildA {

   public String discriminatorprop = "ChildA";

   ... other fields

}

nkavian avatar Aug 09 '20 23:08 nkavian

@liquidaty Did you ever find a good example, because I feel the same. The way I understand the Lizard, Cat, Dot example is that these object only inherit one property which is 'petType' , but what in the case that I want to inherit multiple properties?

I am trying to have a working example of

    class Animal{
        DateTime birthDate;
        Function doSomething;
        String name;
    }
    class Person extends Animal{
        String uid;
    }
    class Cat extends Animal{
        int lives;
    }

In my example, I want Person and Cat to get all of Animal, which includes two fields not just one like in alot of these examples. In fact, if Animal could just be an abstract class that would be great, but discriminator can only map to one field?

pcelis19 avatar Aug 16 '21 10:08 pcelis19

I found this to be informative: https://redoc.ly/docs/resources/discriminator/ Full example and good explanation

AFlowOfCode avatar Sep 12 '21 21:09 AFlowOfCode

@AFlowOfCode after many hours of searching, your hint saved my day

mafentakis avatar Sep 01 '22 15:09 mafentakis

@AFlowOfCode, do you happen to know how I can use the approach presented in the link's content to adjust the example below where I can define one of the schemas highlighted in oneOf in the schema CategoryDtoPagination?

CategoryDtoPagination:
  description: Uses the PaginationData base object.
  allOf:
    - $ref: "#/components/schemas/PaginationData"
PaginationData:
  type: object
  properties:
    pageNumber:
      type: integer
      format: int32
      description: Current page number.
    pageSize:
      type: integer
      format: int32
      description: Represents the maximum size of the page being returned.
    totalPages:
      type: integer
      format: int32
      description: Total pages
    totalElements:
      type: integer
      format: int64
      description: Total elements
    elements:
      type: array
      items:
        oneOf:
          - $ref: "#/components/schemas/CategoryDto"
          - $ref: "#/components/schemas/TransactionDto"

javadev-jef avatar Feb 17 '23 14:02 javadev-jef

This issue is mostly about the code side of things, and belongs in the tooling vendor's repo. Its also quite old.

If there is still a need for more documentation of discriminator, please file an issue for our Learn OpenAPI site based on the current spec versions.

handrews avatar May 19 '24 19:05 handrews