-
Notifications
You must be signed in to change notification settings - Fork 9.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper use of polymorphism / inheritance in Open API 3 #2116
Comments
Could you take a swing at a PR to visualize what you think is correct? Then we can shape that PR into a useful wording improvement. |
@philsturgeon I am a bit confused by your suggestion, as I still don't know what is both correct towards Open Api3 and functional in the swagger editor and generators. It's seems to me that this very simple situation is a dead-end. Let met put it another way : Let's consider the general case where
I would eliminate 1 (impossible to generate accurately), 3 (useful information lost for editors) and 4 (incorrect constraints). So... maybe the correct approach is the second one, as it is the neatest description possible. Maybe it's only a concern for generators. They should implement a bit of logic : get rid of |
What I mean is that from the words being used I am not sure I'm following, but a PR could make the discussion more complete in my head. That said, with a discussion about potentially deprecating discriminator, I wonder if if the examples and discussion in here can take care of your use case? #2143 |
Consider in the case of "allOf" , i don't want to use one of the properties from my reference object , how should I achieve that? example : here i dont want property "addressCategory" in my object recipientAddress. |
|
Hello! Is it possible to have a structure like the following, or any other structure that could allow us to have "required" or "not required" structures per
|
Any news here? |
oneOf confuses me. Lets say the above was a property in an object rather than the return value from a web API call. How would a code generator know what base class to use for the type of the property? It would have to check the base classes for each of the listed subclasses and find the most top-level one? Personally, I don't think oneOf makes any sense for this. Why not, rather than enumerating the subclasses, just list the base class name and the discriminator field? |
@jemiller0 because all of this is trying to leverage JSON Schema, which was not built with code generation in mind at all. It's a constraint system, and things like OAS 3.1 allows using JSON Schema extension vocabularies, and the hope was for improved code generation support through that mechanism. So far that has not happened, but some major tooling only recently caught up to OAS 3.1 so despite being several years since its release, it's still early in terms of people working with it. We'll see what develops - if nothing does, OAS 4 (Moonwalk) might have to take a different approach (IMHO). |
This issue kind-of turned into a grab-bag of questions, most of which aren't addressable by spec changes so I am going to close this. Here are answers to the questions that don't already seem to be answered here:
|
I just want to make a correct specification with basic inheritance anywhere in the schemas, be able to show it properly in the swagger editor and generate some code with it, but I can't. I am clearly missing something in Open API 3.
In the specification document (3.0.2), an example uses
oneOf
, and optionally theDiscriminator
block :In this scenario, the only constraint that
Cat
,Dog
andLizard
must satisfy is the mandatory fieldpetType
, and a predetermined value.Then, one can read :
I find it nice since it offers a proper use of the natural inheritance of the 3 entities :
By the way, the spec is a bit ambiguous with
Pet
does not contains any of these keywords !However, this second example does not show the consequences on the
MyResponseType
block.MyResponseType
and aPet
classes ? What should theMyResponseType
class look like ?MyResponseType
be removed from the file, andPet
used instead ? If so, the swagger editor is not capable of tracing the subtypes anymore...oneOf
to thePet
schema, so that the swagger editor can display the subtypes ? I've tried, it works, and validators say it's Open API 3 compliant, but warn that a cycle gets in the way. It seems to me that this would be formally wrong : an http body with aCat
(petType Cat) with all theCat
andDog
properties would validate because of theoneOf
Cat/Dog inherited fromPet
. Am I right to interprete it this way ?It may be correlated with [#403]
The text was updated successfully, but these errors were encountered: