Is there an existing issue for this?
Describe the feature request
When generating OpenAPI for polymorphic schemas, if the base type is non-abstract and explicitly includes itself as a derived type (for example [JsonDerivedType(typeof(Pet), "pet")]),
please also populate the discriminator defaultMapping (OpenAPI 3.2) to the base schema reference.
Today, discriminator mapping is produced, but there is no defaultMapping in this scenario.
Why this matters
For non-abstract base types, there is a natural fallback/base shape. Emitting defaultMapping improves schema clarity and better aligns with OpenAPI 3.2 discriminator semantics.
Spec reference:
https://spec.openapis.org/oas/v3.2.0.html#discriminator-object
Updated example
using System.Text.Json.Serialization;
[JsonDerivedType(typeof(Cat), typeDiscriminator: "cat")]
[JsonDerivedType(typeof(Dog), typeDiscriminator: "dog")]
[JsonDerivedType(typeof(Pet), typeDiscriminator: "pet")] // self mapping, non-abstract base
public class Pet
{
public string Name { get; set; } = "";
}
public class Cat : Pet
{
public bool IsKitten { get; set; }
}
public class Dog : Pet
{
public string Breed { get; set; } = "";
}
app.MapPost("/pets", (Pet pet) => Results.Ok());
Expected OpenAPI behavior (conceptually)
- discriminator.propertyName is present (e.g. "$type").
- discriminator.mapping includes cat, dog, pet.
- discriminator.defaultMapping points to the base schema (e.g. #/components/schemas/Pet).
Unit tests to update (permalinks)
Is there an existing issue for this?
Describe the feature request
When generating OpenAPI for polymorphic schemas, if the base type is non-abstract and explicitly includes itself as a derived type (for example [JsonDerivedType(typeof(Pet), "pet")]),
please also populate the discriminator
defaultMapping(OpenAPI 3.2) to the base schema reference.Today, discriminator mapping is produced, but there is no defaultMapping in this scenario.
Why this matters
For non-abstract base types, there is a natural fallback/base shape. Emitting defaultMapping improves schema clarity and better aligns with OpenAPI 3.2 discriminator semantics.
Spec reference:
https://spec.openapis.org/oas/v3.2.0.html#discriminator-object
Updated example
Expected OpenAPI behavior (conceptually)
Unit tests to update (permalinks)
https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaSe
rvice.PolymorphicSchemas.cs#L170-L220
https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Servi
ces/OpenApiSchemaService/OpenApiSchemaService.PolymorphicSchemas.cs#L131-L167
https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Shared/SharedTypes.Polymorphism.cs#L85-L99