Skip to content

Commit abbbc2d

Browse files
committed
[PHP] Fix converting objects to formdata
1 parent c82355d commit abbbc2d

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,28 @@ class ObjectSerializer
321321
*
322322
* @param string|\SplFileObject $value the value of the form parameter
323323
*
324-
* @return string the form string
324+
* @return array
325325
*/
326-
public static function toFormValue($value)
326+
public static function toFormValue(string $key, mixed $value)
327327
{
328-
if ($value instanceof \SplFileObject) {
329-
return $value->getRealPath();
330-
} else {
331-
return self::toString($value);
328+
$stringable = self::toString($value);
329+
330+
if ($stringable !== null) {
331+
return [$key => $stringable];
332+
} elseif ($value instanceof \SplFileObject) {
333+
return [$key => $value->getRealPath()];
334+
}
335+
336+
$flattened = [];
337+
$result = [];
338+
339+
self::flatten_array(json_decode(json_encode($value), true), $flattened);
340+
341+
foreach ($flattened as $k => $v) {
342+
$result["{$key}{$k}"] = self::toString($v);
332343
}
344+
345+
return $result;
333346
}
334347
335348
/**
@@ -340,12 +353,14 @@ class ObjectSerializer
340353
*
341354
* @param float|int|bool|\DateTime $value the value of the parameter
342355
*
343-
* @return string the header string
356+
* @return string|null the header string
344357
*/
345358
public static function toString($value)
346359
{
347360
if ($value instanceof \DateTime) { // datetime in ISO8601 format
348361
return $value->format(self::$dateTimeFormat);
362+
} elseif (!is_scalar($value) && $value !== null) {
363+
return null;
349364
} elseif (is_bool($value)) {
350365
return $value ? 'true' : 'false';
351366
} else {

modules/openapi-generator/src/main/resources/php/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,13 @@ use {{invokerPackage}}\ObjectSerializer;
677677
$paramFiles = is_array(${{paramName}}) ? ${{paramName}} : [${{paramName}}];
678678
foreach ($paramFiles as $paramFile) {
679679
$formParams['{{baseName}}'][] = \GuzzleHttp\Psr7\Utils::tryFopen(
680-
ObjectSerializer::toFormValue($paramFile),
680+
ObjectSerializer::toFormValue('{{baseName}}', $paramFile)['{{baseName}}'],
681681
'rb'
682682
);
683683
}
684684
{{/isFile}}
685685
{{^isFile}}
686-
$formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
686+
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('{{baseName}}', ${{paramName}}));
687687
{{/isFile}}
688688
}
689689
{{/formParams}}

0 commit comments

Comments
 (0)