- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Inheritance + Swagger UI + Model Example help #1581
Comments
I am thinking either a ISchemaProcessor or IDocumentProcessor is needed but I could not find clear examples or help on how to achieve this. |
I've taken a look at this. Even with the example given on the page; I don't get my expected outcome. Give me a couple minutes and I'll show you. |
As you see, the example value only contains the properties for the base class:
Startup.cs
|
I've updated my code example. The parameter on the controller is now set to Container, and I've made sure that Animal is now an array. I would expect to see the result in my previous image about anyOf. swagger.json:
|
Related PR: RicoSuter/NJsonSchema#733 |
Edit: I think I can make something work after looking into NJsonSchema.Tests.Generation.InheritanceTests specifically with the FlattenInheritanceHierarchy set to true. What I believe I need to achieve is use a processor to read the EntityType property if it exists, resolve the type by the value of the string, then generate a schema based off that type that contain the flatten properties from the inherited object. Finally, the schema then needs to be replaced. Does all this seem achievable? What processor should I use - IDocumentProcessor or ISchemaProcessor? |
Just FYI: AnyOf inheritance is not (yet) supported by the generator: Also the Swagger spec only describes allOf inheritance. You shouldnt use JsonSchema4.FromTypeAsync in a schema processor but context.SchemaGenerator, so that schemas are correctly added to definitions etc. but then you can't just change the settings (e.g. FlattenInheritanceHierarchy). It seems that you want to completely customize inheritance handling, maybe you need an own implementation of JsonSchemaGenerator with some overrides... |
So, after a bit of toying around, I was able to achieve one thing: Having JsonSchema validate correctly based off my complex request. See code below:
Now, how do I proceed with possibly making SwaggerUI generate the proper examples? Would this custom JsonSchemaGenerator be the correct approach? |
I think you shouldnt use GenerateWithReferenceAndNullabilityAsync when you add the schema to definitions (in definitions they need to be inline, not nullable or referenced) |
We're just about there!
However, I'm still missing the AnyOf or OneOf implementation in the Model example view: What needs to be implemented for this? |
I dont know if Swagger UI even supports anyOf or oneOf. Is that the case? |
Why are you modifying
Try to use
And ensure that the AnyOf in the JSON contains only $refs |
Well, I feel a bit silly. I never changed my SchemaType to OpenApi 3.0 - Only this supports the anyOf keyword. With this code:
and the settings:
Thanks for support! 👍 |
Hi,
When using Swagger UI I am unable to get the Example Value to properly generate an example that contains the derived classes in the example.
This BaseEntityCustomJsonConverter is used on a custom ModelBinding for my incoming request to determine (from the EntityType property we ask in our request) which Object to case the jObject too.
Image of partial Model Example:
Swagger UI see's my inherited class in the Model's according:
What I would actually like to see in Swagger UI is this: (This image was taken from a YAML file created by me for another API that we will create, hence the different properties - What I like achieve does not change though)
Notice how "nodes" property contains an AnyOf with a list of models. How can I achieve this through code?
The text was updated successfully, but these errors were encountered: