kuzzle icon indicating copy to clipboard operation
kuzzle copied to clipboard

Fixed getArray function not working with only one element

Open CustomEntity opened this issue 2 years ago • 2 comments

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

CustomEntity avatar Jul 08 '22 13:07 CustomEntity

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

sonarqubecloud[bot] avatar Jul 08 '22 13:07 sonarqubecloud[bot]

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

Shiranuit avatar Jul 08 '22 16:07 Shiranuit

Closed due to inactivity

Aschen avatar Dec 29 '22 21:12 Aschen