Skip to content

OpenApi - Polymorphism information missing when the base class is empty #62378

Open
@DvdKhl

Description

@DvdKhl

Is there an existing issue for this?

  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions