https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
Where can I find a full example of swagger.json about discriminator?
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.
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.
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, 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 ?
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
}
Most helpful comment
An example in Java would be very useful.
It would also be very nice if the
discriminator.propertyNamecould 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.