OpenApi generator inheritance
We have to create a new API with inheritance in the model. To continue with the contract first way, we will see how to write it in OAS3.
The requirement
We will create an API that can (surprise) manage ponies. We will work with two kinds of ponies, pegasi and unicorn.
The solution
We must use allOf for inheritance:
api.yml
|
|
With this, we will have Pegasi and Unicorn that extend Pony. But if we generate the controller, it will not work well because it can’t say when to build a Pegasi or a Unicorn.
To specify this we need to use a discriminator.
api.yml
|
|
The discriminator is added to the model because it will be in. It must not be treated as a hidden property because it will mess clients.
If you want to fix it, it can be specified with mapping property.
api.yml
|
|
OneOf?
Our first try was with oneOf, but it’s not for inheritance it’s a composition, it’s create an object that can be both a Pegasi and a Unicorn, but in a way that says Pegasi and Unicorn are two disjoint objects with nothing common.