Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LloydNicholson opened this issue Nov 6, 2023 · 6 comments · Fixed by #2980
Labels
help-wanted A change up for grabs for contributions from the community

Comments

@LloydNicholson
Copy link

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.

@Havunen
Copy link

Havunen commented Feb 17, 2024

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

@Liandrel
Copy link

Liandrel commented Apr 15, 2024

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

@martincostello
Copy link
Collaborator

More information would be good if you can.

@Liandrel
Copy link

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

@martincostello martincostello added help-wanted A change up for grabs for contributions from the community and removed needs investigation labels Apr 15, 2024
@martincostello
Copy link
Collaborator

Thanks for the repro.

@Havunen
Copy link

Havunen commented Apr 16, 2024

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 added a commit to Havunen/DotSwashbuckle that referenced this issue Apr 16, 2024
…Type or apiParameter.Type. Fixes an issue where AsParameter Enums get incorrectly generated as strings domaindrivendev#2733
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants