Skip to content

[OpenAPI] Duplicated object definitions in openapi spec #62154

Closed

Description

@januszj

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using two classes as result from minimal api, that are using same class, openapi definition contains two copies of the object for example: WeatherForecast and WeatherForecast2.

"components": {
    "schemas": {
      "WeatherForecast": {
        "required": [
          "date",
          "temperatureC",
          "summary"
        ],
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "summary": {
            "type": "string",
            "nullable": true
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32"
          }
        },
        "nullable": true
      },
      "WeatherForecast2": {
        "required": [
          "date",
          "temperatureC",
          "summary"
        ],
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "summary": {
            "type": "string",
            "nullable": true
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32"
          }
        }
      },

I created reproduction sample code.

WebApplication3.zip

Expected Behavior

It should generated schema with one WeatherForecast.

Steps To Reproduce

Run repo project, open scalar and click Download OpenAPI Document

Exceptions (if any)

No response

.NET Version

9.0.5

Anything else?

No response

Activity

  1. added
    area-minimal
    on May 29, 2025
  2. captainsafia commented on Jul 31, 2025

    @captainsafia
    Contributor

    Fixed in .NET 10.

    #:sdk Microsoft.NET.Sdk.Web
    #:property TargetFramework=net10.0
    #:property PublishAoT=false
    #:package Microsoft.AspNetCore.OpenApi@10.0.0-preview.7.*
    #:package Scalar.AspNetCore@2.6.4
    
    using Scalar.AspNetCore;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
    builder.Services.AddOpenApi();
    
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    
      app.MapOpenApi();
      app.MapScalarApiReference(options =>
      {
        options.Servers = [];
        options.Theme = ScalarTheme.BluePlanet;
      });
    
    app.UseHttpsRedirection();
    
    var summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };
    var group = app
            .MapGroup("/serviceprograms")
            .WithTags("WeatherForecast")
            .WithOpenApi();
    
    group.MapGet("/weatherforecast", (HttpContext httpContext) =>
    {
      var forecast = Enumerable.Range(1, 5).Select(index =>
          new WeatherForecast
          (
              DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
              Random.Shared.Next(-20, 55),
              summaries[Random.Shared.Next(summaries.Length)]
          ))
          .ToArray();
    
      var wr = new WeatherResponse();
      wr.Payload = forecast[0];
    
      return wr;
    })
    .WithName("GetWeatherForecast")
    .Produces<WeatherResponse>(StatusCodes.Status200OK)
    .WithOpenApi();
    
    group.MapGet("/weatherforecasts", (HttpContext httpContext) =>
    {
      var forecast = Enumerable.Range(1, 5).Select(index =>
          new WeatherForecast
          (
              DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
              Random.Shared.Next(-20, 55),
              summaries[Random.Shared.Next(summaries.Length)]
          ))
          .ToArray();
      var wsr = new WeathersResponse();
      wsr.Payload = forecast.ToList();
    
      return wsr;
    })
    .WithName("GetWeatherForecasts")
    .Produces<WeathersResponse>(StatusCodes.Status200OK)
    .WithOpenApi();
    
    app.Run();
    
    public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
    {
      public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    }
    
    public class WeatherResponse
    {
      public WeatherForecast? Payload { get; set; }
    }
    
    public class WeathersResponse
    {
      public List<WeatherForecast>? Payload { get; set; }
    }
    
    {
      "openapi": "3.1.1",
      "info": {
        "title": "62345 | v1",
        "version": "1.0.0"
      },
      "servers": [
        {
          "url": "http://localhost:5000/"
        }
      ],
      "paths": {
        "/serviceprograms/weatherforecast": {
          "get": {
            "tags": [
              "WeatherForecast"
            ],
            "operationId": "GetWeatherForecast",
            "responses": {
              "200": {
                "description": "OK",
                "content": {
                  "application/json": {
                    "schema": {
                      "$ref": "#/components/schemas/WeatherResponse"
                    }
                  }
                }
              }
            }
          }
        },
        "/serviceprograms/weatherforecasts": {
          "get": {
            "tags": [
              "WeatherForecast"
            ],
            "operationId": "GetWeatherForecasts",
            "responses": {
              "200": {
                "description": "OK",
                "content": {
                  "application/json": {
                    "schema": {
                      "$ref": "#/components/schemas/WeathersResponse"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "components": {
        "schemas": {
          "WeatherForecast": {
            "required": [
              "date",
              "temperatureC",
              "summary"
            ],
            "type": "object",
            "properties": {
              "date": {
                "type": "string",
                "format": "date"
              },
              "temperatureC": {
                "pattern": "^-?(?:0|[1-9]\\d*)$",
                "type": [
                  "integer",
                  "string"
                ],
                "format": "int32"
              },
              "summary": {
                "type": [
                  "null",
                  "string"
                ]
              },
              "temperatureF": {
                "pattern": "^-?(?:0|[1-9]\\d*)$",
                "type": [
                  "integer",
                  "string"
                ],
                "format": "int32"
              }
            }
          },
          "WeatherResponse": {
            "type": "object",
            "properties": {
              "payload": {
                "$ref": "#/components/schemas/WeatherForecast"
              }
            }
          },
          "WeathersResponse": {
            "type": "object",
            "properties": {
              "payload": {
                "type": [
                  "null",
                  "array"
                ],
                "items": {
                  "$ref": "#/components/schemas/WeatherForecast"
                }
              }
            }
          }
        }
      },
      "tags": [
        {
          "name": "WeatherForecast"
        }
      ]
    }
    
  3. mikernet commented on Aug 18, 2025

    @mikernet

    Is this getting backported to .net9? I'm still getting duplicates,

  4. captainsafia commented on Aug 18, 2025

    @captainsafia
    Contributor

    @mikernet Unfortunately, these changes can't be backported since they relied on bug fixes in a new major version of the Microsoft.OpenApi package. :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalfeature-openapi

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions