domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.26k stars 1.32k forks source link

[Bug] Discriminated polymorphism does not populate anyOf and oneOf, in contrast with Swagger documentation #3166

Open ldeluigi opened 3 days ago

ldeluigi commented 3 days ago

Problem

The problematic line is this: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/e727e8d53cd9f7edebe5faa6b8bf183e19afb0c6/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs#L416

This line properly generates subtype definitions but doesn't populate any of the anyOf/oneOf properties, resulting in a "broken" swagger document.

Quoting the Swagger doc:

The discriminator is used with anyOf or oneOf keywords only.

Side note

This also breaks openapi based code generators that properly support polymorphism honoring discriminators

Solution

Use the result of the invocation of GenerateConcreteSchema, filling the anyOf/oneOf field. More specifically, we think that oneOf better suits this use case.