kuzzle
kuzzle copied to clipboard
Fixed getArray function not working with only one element
What does this PR do ?
Fixes an error that is thrown when a single parameter is input and an array is requested. This will allow to avoid having to fill at least 2 elements.
How should this be manually tested?
- Step 1 : Start Kuzzle instance
- Step 2 : Send a request with one array parameter.
Example:
https://{baseUrl}:{port}/_openapi?{arrayparameter}=one //NOT WORKING
https://{baseUrl}:{port}/_openapi?{arrayparameter}=one&{arrayparameter}=two //WORKING
Kudos, SonarCloud Quality Gate passed!
0 Bugs
0 Vulnerabilities
0 Security Hotspots
0 Code Smells
No Coverage information
0.0% Duplication
The goal of getArray is to return an array, if the type is not an array the method should throw.
There is a special case when a request is made using the HTTP protocol parameters can come from the query string, since in a query string every arguments is a string we need to try to parse the value to be sure it can be parsed to a JSON Array otherwise we should throw since the given string is not an array nor a valid JSON Array that has been stringified.
In your example:
https://{baseUrl}:{port}/_openapi?{arrayparameter}=one //NOT WORKING
https://{baseUrl}:{port}/_openapi?{arrayparameter}=one&{arrayparameter}=two //WORKING
The second line is working only because when two or more parameters with the same name are sent in a query string, they are batched as one array containing the N values, but when there is only one value it's considered to be a string.
But, I find this behaviour really inconsistent, because you can't be sure that this one parameter was not meant to be a string instead of an array, giving a stringified json array as a string would be more consistent since it would work all the times with not ambiguity but it would also be a bit less easy to pass an array since you would have to write it like so:
https://{baseUrl}:{port}/_openapi?{arrayparameter}=["one"]
https://{baseUrl}:{port}/_openapi?{arrayparameter}=["one","two"]
https://{baseUrl}:{port}/_openapi?{arrayparameter}=one&{arrayparameter}=two
Closed due to inactivity