.NET 9 OpenAPI produces lots of duplicate schemas for the same object · Issue #58968 · dotnet/aspnetcore (original) (raw)

A very simple circular/recursive data model produces a ton of duplicate schema definitions like Object, Object2, Object3, Object4, and so on.

Each class in C# is represented in the OpenAPI schema only once, as was the case with Swashbuckle.

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    [HttpGet("GetParent")]
    public ParentObject GetParent() => new();

    [HttpGet("GetChild")]
    public ChildObject GetChild() => new();
}

public class ParentObject
{
    public int Id { get; set; }
    public List<ChildObject> Children { get; set; } = [];
}

public class ChildObject
{
    public int Id { get; set; }
    public ParentObject? Parent { get; set; }
}
{
  "openapi": "3.0.1",
  "info": {
    "title": "aspnet9api | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://localhost:7217"
    },
    {
      "url": "http://localhost:5067"
    }
  ],
  "paths": {
    "/WeatherForecast/GetParent": {
      "get": {
        "tags": [
          "WeatherForecast"
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ParentObject"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ParentObject"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ParentObject"
                }
              }
            }
          }
        }
      }
    },
    "/WeatherForecast/GetChild": {
      "get": {
        "tags": [
          "WeatherForecast"
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ChildObject2"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ChildObject2"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ChildObject2"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ChildObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int32"
          },
          "parent": {
            "$ref": "#/components/schemas/ParentObject2"
          }
        }
      },
      "ChildObject2": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int32"
          },
          "parent": {
            "$ref": "#/components/schemas/ParentObject3"
          }
        }
      },
      "ParentObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int32"
          },
          "children": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ChildObject"
            }
          }
        }
      },
      "ParentObject2": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int32"
          },
          "children": {
            "$ref": "#/components/schemas/#/properties/children"
          }
        },
        "nullable": true
      },
      "ParentObject3": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int32"
          },
          "children": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ChildObject"
            }
          }
        },
        "nullable": true
      }
    }
  },
  "tags": [
    {
      "name": "WeatherForecast"
    }
  ]
}

In my real-world application, the worst offending model has been duplicated 39 times:
Image