Skip to content
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

[PHP] no need to serialize collections, Guzzle does that #3984

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,26 @@ class ObjectSerializer
*
* @return string
*/
public static function serializeCollection(array $collection, $collectionFormat, $allowCollectionFormatMulti = false)
public static function serializeCollection(array $collection, $style, $allowCollectionFormatMulti = false)
{
if ($allowCollectionFormatMulti && ('multi' === $collectionFormat)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($allowCollectionFormatMulti && ('multi' === $style))

($collectionFormat doesn't exist anymore ;) )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix it in a separate PR.

If no further feedback, I'll merge it on the coming Tuesday.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #5517

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, William!
Do you want me to create a PR for my other comment (regarding "form" support)? I've been using the suggested fix in my own fork, and it works pretty fine! :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomasphansen yes please 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! #5519

// http_build_query() almost does the job for us. We just
// need to fix the result of multidimensional arrays.
return preg_replace('/%5B[0-9]+%5D=/', '=', http_build_query($collection, '', '&'));
}
switch ($collectionFormat) {
switch ($style) {
case 'pipeDelimited':
case 'pipes':
return implode('|', $collection);

case 'tsv':
return implode("\t", $collection);

case 'spaceDelimited':
case 'ssv':
return implode(' ', $collection);

case 'simple':
case 'csv':
// Deliberate fall through. CSV is default format.
default:
Expand Down
16 changes: 12 additions & 4 deletions modules/openapi-generator/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -472,16 +472,24 @@ use {{invokerPackage}}\ObjectSerializer;
$multipart = false;

{{#queryParams}}

// query params
{{#collectionFormat}}
{{#isExplode}}
if (${{paramName}} !== null) {
$queryParams['{{baseName}}'] = ${{paramName}};
}
{{/isExplode}}
Comment on lines +477 to +481
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't properly support the 'form' style. If we look at other implementations (such as the Javascript one), when explode is true and style equals to 'form', the parameter should be expanded so that each key inside the parameter (which should then be an array) would result in a new query parameter. There is a very nice example of this in the accepted answer for this thread at stackoverflow.
So, to make this work, in my humble opinion this code might be changed to:

        if (${{paramName}} !== null) {
        {{#style}}
            if('form' === '{{style}}' && is_array(${{paramName}})) {
                foreach(${{paramName}} as $key => $value) {
                    $queryParams[$key] = $value;
                }
            }
            else {
                $queryParams['{{baseName}}'] = ${{paramName}};
            }
        {{/style}}
        {{^style}}
            $queryParams['{{baseName}}'] = ${{paramName}};
        {{/style}}
        }

The only problem here is: PR #4042 is unfortunately incomplete, and due to it, the style property is always empty. I've created a new PR #4640 to fix the issue...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: #4640 is merged now, which means the style property should work fine from now on! :)

{{^isExplode}}
if (is_array(${{paramName}})) {
${{paramName}} = ObjectSerializer::serializeCollection(${{paramName}}, '{{collectionFormat}}', true);
${{paramName}} = ObjectSerializer::serializeCollection(${{paramName}}, '{{#style}}{{style}}{{/style}}{{^style}}{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}{{/style}}', true);
}
{{/collectionFormat}}
if (${{paramName}} !== null) {
$queryParams['{{baseName}}'] = ObjectSerializer::toQueryValue(${{paramName}});
$queryParams['{{baseName}}'] = ${{paramName}};
}
{{/isExplode}}

{{/queryParams}}

{{#headerParams}}
// header params
{{#collectionFormat}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ protected function call123TestSpecialTagsRequest($body)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down
97 changes: 80 additions & 17 deletions samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ protected function createXmlItemRequest($xml_item)




// body params
$_tempBody = null;
if (isset($xml_item)) {
Expand Down Expand Up @@ -524,6 +525,7 @@ protected function fakeOuterBooleanSerializeRequest($body = null)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down Expand Up @@ -781,6 +783,7 @@ protected function fakeOuterCompositeSerializeRequest($body = null)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down Expand Up @@ -1038,6 +1041,7 @@ protected function fakeOuterNumberSerializeRequest($body = null)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down Expand Up @@ -1295,6 +1299,7 @@ protected function fakeOuterStringSerializeRequest($body = null)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down Expand Up @@ -1510,6 +1515,7 @@ protected function testBodyWithFileSchemaRequest($body)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down Expand Up @@ -1734,12 +1740,18 @@ protected function testBodyWithQueryParamsRequest($query, $body)
$httpBody = '';
$multipart = false;


// query params
if (is_array($query)) {
$query = ObjectSerializer::serializeCollection($query, '', true);
}
if ($query !== null) {
$queryParams['query'] = ObjectSerializer::toQueryValue($query);
$queryParams['query'] = $query;
}




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down Expand Up @@ -2007,6 +2019,7 @@ protected function testClientModelRequest($body)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down Expand Up @@ -2356,6 +2369,7 @@ protected function testEndpointParametersRequest($number, $double, $pattern_with




// form params
if ($integer !== null) {
$formParams['integer'] = ObjectSerializer::toFormValue($integer);
Expand Down Expand Up @@ -2660,25 +2674,43 @@ protected function testEnumParametersRequest($enum_header_string_array = null, $
$httpBody = '';
$multipart = false;


// query params
if (is_array($enum_query_string_array)) {
$enum_query_string_array = ObjectSerializer::serializeCollection($enum_query_string_array, 'csv', true);
}
if ($enum_query_string_array !== null) {
$queryParams['enum_query_string_array'] = ObjectSerializer::toQueryValue($enum_query_string_array);
$queryParams['enum_query_string_array'] = $enum_query_string_array;
}


// query params
if (is_array($enum_query_string)) {
$enum_query_string = ObjectSerializer::serializeCollection($enum_query_string, '', true);
}
if ($enum_query_string !== null) {
$queryParams['enum_query_string'] = ObjectSerializer::toQueryValue($enum_query_string);
$queryParams['enum_query_string'] = $enum_query_string;
}


// query params
if (is_array($enum_query_integer)) {
$enum_query_integer = ObjectSerializer::serializeCollection($enum_query_integer, '', true);
}
if ($enum_query_integer !== null) {
$queryParams['enum_query_integer'] = ObjectSerializer::toQueryValue($enum_query_integer);
$queryParams['enum_query_integer'] = $enum_query_integer;
}


// query params
if (is_array($enum_query_double)) {
$enum_query_double = ObjectSerializer::serializeCollection($enum_query_double, '', true);
}
if ($enum_query_double !== null) {
$queryParams['enum_query_double'] = ObjectSerializer::toQueryValue($enum_query_double);
$queryParams['enum_query_double'] = $enum_query_double;
}


// header params
if (is_array($enum_header_string_array)) {
$enum_header_string_array = ObjectSerializer::serializeCollection($enum_header_string_array, 'csv');
Expand Down Expand Up @@ -2969,22 +3001,43 @@ protected function testGroupParametersRequest($associative_array)
$httpBody = '';
$multipart = false;


// query params
if (is_array($required_string_group)) {
$required_string_group = ObjectSerializer::serializeCollection($required_string_group, '', true);
}
if ($required_string_group !== null) {
$queryParams['required_string_group'] = ObjectSerializer::toQueryValue($required_string_group);
$queryParams['required_string_group'] = $required_string_group;
}


// query params
if (is_array($required_int64_group)) {
$required_int64_group = ObjectSerializer::serializeCollection($required_int64_group, '', true);
}
if ($required_int64_group !== null) {
$queryParams['required_int64_group'] = ObjectSerializer::toQueryValue($required_int64_group);
$queryParams['required_int64_group'] = $required_int64_group;
}


// query params
if (is_array($string_group)) {
$string_group = ObjectSerializer::serializeCollection($string_group, '', true);
}
if ($string_group !== null) {
$queryParams['string_group'] = ObjectSerializer::toQueryValue($string_group);
$queryParams['string_group'] = $string_group;
}


// query params
if (is_array($int64_group)) {
$int64_group = ObjectSerializer::serializeCollection($int64_group, '', true);
}
if ($int64_group !== null) {
$queryParams['int64_group'] = ObjectSerializer::toQueryValue($int64_group);
$queryParams['int64_group'] = $int64_group;
}


// header params
if ($required_boolean_group !== null) {
$headerParams['required_boolean_group'] = ObjectSerializer::toHeaderValue($required_boolean_group);
Expand Down Expand Up @@ -3211,6 +3264,7 @@ protected function testInlineAdditionalPropertiesRequest($param)




// body params
$_tempBody = null;
if (isset($param)) {
Expand Down Expand Up @@ -3441,6 +3495,7 @@ protected function testJsonFormDataRequest($param, $param2)




// form params
if ($param !== null) {
$formParams['param'] = ObjectSerializer::toFormValue($param);
Expand Down Expand Up @@ -3703,43 +3758,51 @@ protected function testQueryParameterCollectionFormatRequest($pipe, $ioutil, $ht
$httpBody = '';
$multipart = false;


// query params
if (is_array($pipe)) {
$pipe = ObjectSerializer::serializeCollection($pipe, 'csv', true);
}
if ($pipe !== null) {
$queryParams['pipe'] = ObjectSerializer::toQueryValue($pipe);
$queryParams['pipe'] = $pipe;
}


// query params
if (is_array($ioutil)) {
$ioutil = ObjectSerializer::serializeCollection($ioutil, 'csv', true);
}
if ($ioutil !== null) {
$queryParams['ioutil'] = ObjectSerializer::toQueryValue($ioutil);
$queryParams['ioutil'] = $ioutil;
}


// query params
if (is_array($http)) {
$http = ObjectSerializer::serializeCollection($http, 'space', true);
}
if ($http !== null) {
$queryParams['http'] = ObjectSerializer::toQueryValue($http);
$queryParams['http'] = $http;
}


// query params
if (is_array($url)) {
$url = ObjectSerializer::serializeCollection($url, 'csv', true);
}
if ($url !== null) {
$queryParams['url'] = ObjectSerializer::toQueryValue($url);
$queryParams['url'] = $url;
}


// query params
if (is_array($context)) {
$context = ObjectSerializer::serializeCollection($context, 'multi', true);
}
if ($context !== null) {
$queryParams['context'] = ObjectSerializer::toQueryValue($context);
$queryParams['context'] = $context;
}




// body params
$_tempBody = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ protected function testClassnameRequest($body)




// body params
$_tempBody = null;
if (isset($body)) {
Expand Down
Loading