Skip to content

OpenAPI: Populate discriminator defaultMapping for non-abstract base types that include self JsonDerivedType #65724

Description

@baywet

Is there an existing issue for this?

  • I have searched the existing issues

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)

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions