Microsoft.AspNetCore.OpenApi generates a class that does not exist but is like a other class with a number added to the end of the name · Issue #59677 · dotnet/aspnetcore (original) (raw)

Is there an existing issue for this?

Describe the bug

I have asp dotnet 9.0 project.

I use Microsoft.AspNetCore.OpenApi to generate OpenAPI and use Scalar and SwaggerUI to show it.
my project file is

net9.0 enable enable true true ./

and there are Monitoring api and class like this:

using System.Net;

namespace WebApiAotTest;

public class MonitoringItem { public string[] Name { get; set; } = null!; public int value { get; set; } }

public class MonitoringRoot { public MonitoringItem Item1 { get; set; } = null!; public MonitoringItem Item2 { get; set; } = null!; }

public static class MonitoringExtension { public static void MonitoringApi(this WebApplication app) { var monitoringApi = app.MapGroup("/monitoring");

    monitoringApi.MapGet("/", () => new MonitoringRoot() )
        .Produces<MonitoringRoot>((int)HttpStatusCode.OK)
        .WithTags("Monitoring")
        .WithSummary("Get Monitoring")
        .WithDescription("This endpoint returns Monitoring data.");
}

}

The API return MonitoringRoot that has multi MonitoringItem.

and program.cs is:

using Microsoft.AspNetCore.OpenApi; using Microsoft.OpenApi.Models; using Scalar.AspNetCore; using System.Text.Json.Serialization; using WebApiAotTest;

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.AddOpenApi(options => { options.AddDocumentTransformer((document, context, cancellationToken) => { document.Info.Contact = new OpenApiContact { Name = "Contoso Support", Email = "support@contoso.com" }; return Task.CompletedTask; });

options.CreateSchemaReferenceId = (type) => type.Type.IsEnum ? null : OpenApiOptions.CreateDefaultSchemaReferenceId(type);

});

builder.Services.ConfigureHttpJsonOptions(options => { options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); });

var app = builder.Build();

app.MapOpenApi("/openapi/v1/my.json"); app.UseSwaggerUI(opt => { opt.SwaggerEndpoint("/openapi/v1/my.json", "MyApi"); }); app.MapScalarApiReference(opt => { opt .WithTitle("MyApi") .WithOpenApiRoutePattern("/openapi/v1/{documentName}.json") .WithEndpointPrefix("/openapi/{documentName}") ; });

app.MonitoringApi(); app.Run();

[JsonSerializable(typeof(MonitoringRoot))] internal partial class AppJsonSerializerContext : JsonSerializerContext {

}

API works OK. but when I see swagger, there is wrong data!

Image

and (??!!!!):

Image

also original json has wrong:

{ "openapi": "3.0.1", "info": { "title": "WebApiAotTest | v1", "contact": { "name": "Contoso Support", "email": "support@contoso.com" }, "version": "1.0.0" }, "servers": [ { "url": "http://localhost:5090" } ], "paths": { "/monitoring": { "get": { "tags": [ "Monitoring" ], "summary": "Get Monitoring", "description": "This endpoint returns Monitoring data.", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/MonitoringRoot" } } } } } } } }, "components": { "schemas": { "MonitoringItem": { "type": "object", "properties": { "name": { "type": "array", "items": { "type": "string" } }, "value": { "type": "integer", "format": "int32" } } }, "MonitoringItem2": { "type": "object", "properties": { "name": { "$ref": "#/components/schemas/#/properties/item1/properties/name" }, "value": { "type": "integer", "format": "int32" } } }, "MonitoringRoot": { "type": "object", "properties": { "item1": { "$ref": "#/components/schemas/MonitoringItem" }, "item2": { "$ref": "#/components/schemas/MonitoringItem2" } } } } }, "tags": [ { "name": "Monitoring" } ] }

There is MonitoringItem2 is json (generated by OpenAPI) but there is not in my code.

NOTE: MonitoringItem2 is exactly like MonitoringItem.
NOTE: If add Item3, Item4 , ... to MonitoringRoot, generate MonitoringItem3, MonitoringItem3, .... .
NOTE: I checked and found that the below code was causing this problem:

// in MonitoringItem public string[] Name { get; set; }

so I remove array, everything is OK! like this:

public class MonitoringItem { public string Name { get; set; } = null!; public int value { get; set; } }

Expected Behavior

No generate extra data and I see:

Image

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.100

Anything else?

I use Microsoft Visual Studio Community 2022 (64-bit) Version 17.12.2 in windows 10 22H2.