Swashbuckle.AspNetCore
Swashbuckle.AspNetCore copied to clipboard
[AsParameter] attribute generates a string query parameter when it should generate a decimal
Issue
The [AsParameters]
attribute is not generating the correct type of decimal
when using Swashbuckle to generate a WebApi. It generates a string
.
When a decimal type is used on the object that is annotated with the [AsParameters]
attribute the type generated is always a string
rather than a decimal.
Reproduction
I have attached a zip file with a net7.0 solution with a basic replication of the problem. It contains a net7 minimal web API and you need to run it to see the type generated on the /swagger
URL page is a string, not a decimal as expected.
Expected Behaviour Generate a decimal when the client runs the build step.
I tested this using DotSwashbuckle and .NET 8 and it seems to work. https://github.com/Havunen/DotSwashbuckle https://www.nuget.org/packages/DotSwashbuckle.AspNetCore
I've run into similar issue with enum type
endpoint registered like that:
As parameters object:
swagger endpoint:
swagger json:
I can provide some more context if it will be needed
tho if I provide valid value for that enum (0, 1, 2) or proper string value of that enum names it parses fine into model
More information would be good if you can.
To make sure it's not any external or domain dependency I've created sample project that shows the issue
https://github.com/Liandrel/Swashbuckle-AsParameters-sample
.NET 8 minimal api with no additional nugget packages except of pattern ones
one endpoint:
RequestModel:
Swagger Page:
Swagger.json
Thanks for the repro.
It seems the ApiExplorer provides the type for the enum as string... using metadata driven approach works tho...
// Works
endpoints.MapGet("/asparameter/openapi/enum", ([FromQuery] ConditionState? ConditionStateFromQuery,
[AsParameters] RequestModel model) =>
{
return $"{ConditionStateFromQuery}, {model.ConditionStateAsParameters}";
}).WithOpenApi();
// Does not work
endpoints.MapGet("/asparameter/openapi/enum2", ([FromQuery] ConditionState? ConditionStateFromQuery,
[AsParameters] RequestModel model) =>
{
return $"{ConditionStateFromQuery}, {model.ConditionStateAsParameters}";
});