domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.26k stars 1.31k forks source link

[Bug]: Property casing policy is ignored when using minimal APIs, camel casing always applied #3097

Open antoinebj opened 1 month ago

antoinebj commented 1 month ago

Describe the bug

No matter which naming policy is set in general JSON serializer options when using minimal APIs, Swashbuckle applies camel casing. It therefore does not comply with the actual output of the endpoint.

Expected behavior

With the null naming policy, the property name should have been taken without modification, i.e. it should have been HelloWorld.

Actual behavior

The property name was modified to camel case.

This is what I get in the document:

  "components": {
    "schemas": {
      "MyDto": {
        "type": "object",
        "properties": {
          "helloWorld": {
            "type": "string",
            "nullable": true,
            "readOnly": true
          }
        },
        "additionalProperties": false
      }
    }
  }

Steps to reproduce

Using this code in Program.cs:

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.PropertyNamingPolicy = null;
});

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// This needs to be added because of some other open issue
builder.Services.Configure<RouteOptions>(
    options => options.SetParameterPolicy<RegexInlineRouteConstraint>("regex"));

builder.WebHost.UseKestrelHttpsConfiguration();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapGet("/hello", () => new MyDto());

app.Run();

internal sealed class MyDto
{
    public string HelloWorld => "hello";
}

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

6.8.1

.NET Version

8.0.400

Anything else?

NSwag correctly applies the configured property naming policy, using the same code (aside from the 3 Swashbuckle calls replaced with NSwag ones).

jgarciadelanoceda commented 3 weeks ago

@antoinebj can you see if this WA works for you? I beleive that is the same issue: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2293#issuecomment-993419245