Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

[AsParameter] attribute generates a string query parameter when it should generate a decimal

Open LloydNicholson opened this issue 1 year ago • 6 comments

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.

TestWebApp.zip

Expected Behaviour Generate a decimal when the client runs the build step.

LloydNicholson avatar Nov 06 '23 09:11 LloydNicholson

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

image

Havunen avatar Feb 17 '24 16:02 Havunen

I've run into similar issue with enum type

endpoint registered like that: image

As parameters object: image

swagger endpoint: image

swagger json: image

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

Liandrel avatar Apr 15 '24 17:04 Liandrel

More information would be good if you can.

martincostello avatar Apr 15 '24 17:04 martincostello

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: image

RequestModel: image

Swagger Page: image

Swagger.json image

Liandrel avatar Apr 15 '24 18:04 Liandrel

Thanks for the repro.

martincostello avatar Apr 15 '24 18:04 martincostello

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}";
                });

Havunen avatar Apr 16 '24 17:04 Havunen