Skip to content

Commit

Permalink
fix(php): no need to serialize collections, Guzzle does that, fix #2292
Browse files Browse the repository at this point in the history
… (#3984)

* fix(php): only serialize collections if !explode, Guzzle handles the rest, fix #2292

* fix(php): update petstore samples

Co-authored-by: Mahdi Dibaiee <[email protected]>
  • Loading branch information
copypasta-g and mdibaiee authored Mar 4, 2020
1 parent 33129ca commit 3e5fb67
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,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)) {
// 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}}
{{^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

0 comments on commit 3e5fb67

Please sign in to comment.