dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.51k stars 25.31k forks source link

Swagger - Polymorphic model binding #26921

Open sannyjacobsson opened 1 year ago

sannyjacobsson commented 1 year ago

I have followed the instructions regarding polymorphic model binding here (https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-6.0#polymorphic-model-binding). Which works great.

However, I'm missing information on how to get this to work with Swagger. I have configured Swagger to support polymorphic model behaviour according to the Swagger documentation. Unfortunately I have not figured out any way to include parameters from derived classes. Is there any way to configure Swagger to display the correct input fields depending on the "kind" type? If not, is there any way to configure Swagger to only display an input field so that I can manually add the form data (application/x-www-form-urlencoded encoded) to the request?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Rick-Anderson commented 1 year ago

In backlog

rafikiassumani-msft commented 1 year ago

@Rick-Anderson I am not sure if this person is using Swashbuckle and we should point them to the docs on polymorphism and inheritance that supports allOf or anyOf to express the schema relationships. @captainsafia is this something that we should document? Did we investigate how inheritance works for Minimal APIs when it comes to openApi doc generation?

sannyjacobsson commented 1 year ago

Additional information.

Packages `

`

We enabled polymorphism and added explicit annotations. List Known Subtypes for Inheritance and Polymorphism and Enrich Polymorphic Base Classes with Discriminator Metadata

The rest Api Endpoints worked as intended fine using Postman. However, we could not get it to work with the Swagger UI. Since one of the requirements where that external developers where to be able to use Swagger UI we used a different approach.

I still think it would be interesting to know if it is possible to configure Swagger and Swagger UI to work with polymorphism and request parameters.