-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🐛 [Bug]: QueryParser is not parsing comma separated values to slices by default #2624
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
@rere61 what do you think about it ? |
@rere61 actually meant about the current behavior and the new behavior which is requested in the report |
Yes, the new behavior is more common in server-side development. |
@rere61 |
@ReneWerner87 So, my short answer for this request is "No". And most imporantly, split string by comma is very easy task, why should this feature needed to be built-in framework? |
Because that's what frameworks are for. So we don't have to do the "dirty work". Go Standard HTTP is a raw implementation, so it's understandable that we must do these transformations manually Comma-separated query parameters are by convention transformed into arrays/lists/slices of values around the web developer community. Again, in case of Or, another solution is decide based on param type, if that field was defined as |
Can you tell me which framework will automatically separate a user's single query value by comma, such as input containing a given name and first name(ex: Hemingway, Ernest Miller ), with commas without causing side effects? |
In this specific case the query parameters must be encoded and decoded on server side. But, this is an atypical case, in most cases the comma will be used as a separator and not as an integral part of the string. It is possible turn the method smarter as below if equalFieldType(out, reflect.Slice, k) {
values := strings.Split(v, ",")
for i := 0; i < len(values); i++ {
data[k] = append(data[k], values[i])
}
} else {
data[k] = append(data[k], v)
} It would be not needed to check if Another option is to enable/disable this behavior as a new parameter in the QueryParser method, instead of a global config. // enableSplitting is a second param in QueryParser method, or maybe could be exist a options{} second
// parameter containing some configs, one of them enableSplitting. And by default this value would be true
if equalFieldType(out, reflect.Slice, k) && enableSplitting {
values := strings.Split(v, ",")
for i := 0; i < len(values); i++ {
data[k] = append(data[k], values[i])
}
} else {
data[k] = append(data[k], v)
} this way It would not be a breaking change and will not change the behavior for entire application |
This isn't a bad idea actually but it would be nice to have it also take into account the global config |
And if it has two global configs? One for BodyParser and other for QueryParser? Where the QueryParser config would be true by default. And, besides that, could have this second param too, that would be initialized by the global config value, but that could be changed if user has passed as argument... |
Hmm or instead it would take the global variable if true by default, if its not set then a local variable takes preference. |
But then it would require changes to the body parser and also the cookie parser |
Bug Description
Bug inserted into #2560.
I agree that BodyParser shouldn't be splitting values by comma by default, but I don't agree that the same behavior should be extrapolated to QueryParser. By default query params separated by commas are arrays/slices and I believe that the framework must follow this rule by default and not by enabling a configuration
How to Reproduce
curl --location 'http://localhost:5000/api/alerts?level=warning%2Cerror'
Steps to reproduce the behavior:
Expected Behavior
Query params must be parsed to slices when they came separated with commas
Fiber Version
v2.49.1
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: