Accept arrays as arguments to collection parameters in PHP client.#1499
Accept arrays as arguments to collection parameters in PHP client.#1499wing328 merged 2 commits intoswagger-api:masterfrom arnested:collectionFormat
Conversation
|
Tests running fine: |
There was a problem hiding this comment.
I don't think this handles the "multi" collectionFormat correctly. Assuming a parameter "foo" has an array value of 2 elements, "bar" and "baz", in the $collection here, then this is expected to be included in the URL: foo=bar&foo=baz.
However, the current implementation of serializeCollection seems to return string 0=bar&1=baz, which will be included into the URL via ApiClient.php and the URL would contains:
foo=0%3Dbar%261%3Dbaz
(see here in swagger spec for the above example of "multi" collectionFormat)
There was a problem hiding this comment.
Yes, you're right, @xhh, I haven't taken account for multidimensional arrays. That's a bug.
I'll look into -- either I have to do query string building myself or I'll postprocess the result of http_build_query() and weed out the encoded [].
There was a problem hiding this comment.
FYI I just submitted PR #1509 to support collectionFormat in Ruby client, which is using Typhoeus to make HTTP requests.
Typhoeus handles array parameters in the same way with the multi collectionFormat. So the build_collection_param method returns the (array) parameter directly.
I've no idea how PHP handles it, but hope this helps.
|
I changed the pull request to handle @xhh comments about multidimensionale arrays. The pull request has been rebased on HEAD of master and the petstore sample has been regenerated. |
|
@wing328 please merge if this looks good to you... |
|
The change definitely looks good. CI test result: |
Accept arrays as arguments to collection parameters in PHP client.
This patch makes it possible to use arrays as arguments for parameters defined as arrays of strings -- and respects the
collectionFormat.Until now you had to implode the string to an array yourself before calling the API.
I've been thinking about also adding type hints to array for those parameters in method argument list. But since that would break backwards compatibility it should probably be hidden behind a command line option or something.
NB: This needs swagger-parser 1.0.12.