OpenAPI-Specification
OpenAPI-Specification copied to clipboard
Full example of Discriminator
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
}
@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?
I found this to be informative: https://redoc.ly/docs/resources/discriminator/ Full example and good explanation
@AFlowOfCode after many hours of searching, your hint saved my day
@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"
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.