mattfrear/Swashbuckle.AspNetCore.Filters

Minimal Api returns examples in Pascal case with no option to change

kimsagro1 opened this issue · 3 comments

If you run the MinimalApi8 test project, you can see that the example response is Pascal case.

[
  {
    "Date": "2024-01-08T00:00:00+11:00",
    "TemperatureC": 20,
    "Summary": "Sunny",
    "TemperatureF": 67
  }
]

image

I assume this is due to System.Text.Json using Pascal Case by default.

return System.Text.Json.JsonSerializer.Serialize(value);

This behaviour is the opposite of the default example generated by swagger

[
  {
    "date": "2024-01-08T05:49:44.552Z",
    "temperatureC": 0,
    "summary": "string",
    "temperatureF": 0
  }
]

Is there any way to configure this?

Hello. There's a note about camel case in my Readme: https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters#pascal-case-or-camel-case

The suggested workaround in the readme is:

builder.Services.Configure<Microsoft.AspNetCore.Mvc.MvcOptions>(options =>
{
	options.OutputFormatters.Clear();
	options.OutputFormatters.Add(
		new Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter(
			new JsonSerializerOptions
			{
				PropertyNamingPolicy = JsonNamingPolicy.CamelCase
			}));
});

The above worked fine with .NET 6, but it throws exceptions in .NET 8.

However, I don't think this should be necessary.

As you have pointed out, the default from Swagger is camelCase, and the default 200 response is also camel case - so the example should also default to camel case.

I will make these changes soon and push out a new release.

Unfortunately this is a breaking change for those few folks who want to use Pascal Case in their minimal API examples, which dare I say it means I need to bump the major version number. Hmm. I'm reluctant to make breaking changes so I need to think about this.

@mattfrear Thanks for your speedy response. I did try the work around as per the docs but hit the exception (I'm using .NET 8).