Closed JansthcirlU closed 1 year ago
@JansthcirlU thanks for contacting us.
I believe this is by design. Arrays are serialized in that fashion for historical reasons and backwards compatibility with older versions of MVC as well as the model binding system.
@javiercn Thank you for your reply. How could I support the case for users who want to call my API by manually typing out the URL using commas, or for other APIs that want to call the method by visiting a URL with comma-separated values?
There was a workaround in the Swashbuckle issue, but this doesn't seem to be the right solution for de-serializing query strings.
@JansthcirlU if I understood correctly, the issue you are having is that ?letters=a,b,c
is not binding correctly to an array with three elements, is that the case?
@javiercn correct, using ?letters=a,b,c
creates an array with a single element "a,b,c"
rather than one element for each letter. I understand that I could manually split the text inside the controller method, but it would be nice if it just worked.
@JansthcirlU You can author a custom model binder for the parameter and apply it with an attribute.
@javiercn okay, I'll investigate that approach!
@JansthcirlU Where in your OpenAPI definition do you define the format for the letters query value?
Hi @JansthcirlU. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
See our Issue Management Policies for more information.
Is there an existing issue for this?
Describe the bug
Arrays or enumerables of
string
are incorrectly serialized when using them as a query parameter inside a controller method. For example, the input arraystring letters = { "d", "g", "o" };
gets serialized to the query string"?letters=d&letters=g&letters=o"
.The reason why I'm making an issue here is because someone made a very similar issue on the Swashbuckle repository and they believe it is an upstream issue in the .NET implementation.
Expected Behavior
The query string should be simplified to
"?letters=d,g,o"
in situations where the OpenAPI style specifications allow it (such as this one).Steps To Reproduce
dotnet new webapi -f net7.0 -o MyApi
)CombinationsController
api/v1/combinations
route on theCombinations
controllerd
,g
ando
as the input characters and executeThe resulting request URL contains the query
?letters=d&letters=g&letters=o
instead of?letters=d,g,o
.Exceptions (if any)
No response
.NET Version
7.0.101
Anything else?
I'm using ASP.NET Core 7 with Visual Studio Code 1.75.0 as my editor