From d4e1529890f0aec6d98bbf3aa6bbb17dbdee1342 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Sun, 29 Sep 2019 12:00:02 +0330 Subject: [PATCH] fix(php): only serialize collections if !explode, Guzzle handles the rest, fix #2292 --- .../main/resources/php/ObjectSerializer.mustache | 7 +++++-- .../src/main/resources/php/api.mustache | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index f5f0e5119efc..a54f47422410 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -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)) { // 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: diff --git a/modules/openapi-generator/src/main/resources/php/api.mustache b/modules/openapi-generator/src/main/resources/php/api.mustache index 1debf0856965..9c975a76f323 100644 --- a/modules/openapi-generator/src/main/resources/php/api.mustache +++ b/modules/openapi-generator/src/main/resources/php/api.mustache @@ -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}}