aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

OpenApi - Polymorphism information missing when the base class is empty

Open DvdKhl opened this issue 8 months ago • 0 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the bug

If the base class is empty no polymorphism information is included in the document.

Expected Behavior

Include polymorphism information even if the base class is empty.

Steps To Reproduce

Minimal Repo:

using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();

var app = builder.Build();
app.MapOpenApi();

app.MapGet("/Pet", () => Enumerable.Empty<Pet>).WithName("Pet").Produces<Pet[]>();
app.MapGet("/Shape", () => Enumerable.Empty<Shape>).WithName("Shape").Produces<Shape[]>();

app.Run();

[JsonPolymorphic]
[JsonDerivedType(typeof(Cat), "cat")]
[JsonDerivedType(typeof(Dog), "dog")]
public record Pet(string Name);
public record Dog(string Name, string? Breed) : Pet(Name);
public record Cat(string Name, int? Lives) : Pet(Name);

[JsonPolymorphic]
[JsonDerivedType(typeof(Rectangle), "Rectangle")]
[JsonDerivedType(typeof(Circle), "Circle")]
public record Shape();
public record Rectangle(int Width, int Height) : Shape();
public record Circle(int Radius) : Shape();

Generated document:

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenApiPolyEmptyBase | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://localhost:5129/"
    }
  ],
  "paths": {
    "/Pet": {
      "get": {
        "tags": [
          "OpenApiPolyEmptyBase"
        ],
        "operationId": "Pet",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Pet"
                  }
                }
              }
            }
          }
        }
      }
    },
    "/Shape": {
      "get": {
        "tags": [
          "OpenApiPolyEmptyBase"
        ],
        "operationId": "Shape",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Shape"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "anyOf": [
          {
            "$ref": "#/components/schemas/PetCat"
          },
          {
            "$ref": "#/components/schemas/PetDog"
          },
          {
            "$ref": "#/components/schemas/PetBase"
          }
        ]
      },
      "PetBase": {
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          }
        }
      },
      "PetCat": {
        "required": [
          "$type",
          "lives",
          "name"
        ],
        "properties": {
          "$type": {
            "enum": [
              "cat"
            ],
            "type": "string"
          },
          "lives": {
            "type": "integer",
            "format": "int32",
            "nullable": true
          },
          "name": {
            "type": "string"
          }
        }
      },
      "PetDog": {
        "required": [
          "$type",
          "breed",
          "name"
        ],
        "properties": {
          "$type": {
            "enum": [
              "dog"
            ],
            "type": "string"
          },
          "breed": {
            "type": "string",
            "nullable": true
          },
          "name": {
            "type": "string"
          }
        }
      },
      "Shape": {
        "type": "object"
      }
    }
  },
  "tags": [
    {
      "name": "OpenApiPolyEmptyBase"
    }
  ]
}

Exceptions (if any)

No response

.NET Version

9.0.300

Anything else?

Microsoft.AspNetCore.OpenApi: 9.0.5

DvdKhl avatar Jun 17 '25 14:06 DvdKhl