Skip to content

Commit 36217ad

Browse files
committed
[php-symfony] Don't crash at runtime on null convertFormat
$this->convertFormat may return "null". When it's the case we end up calling ...->serialize($data, null); but this crashes at runtime because that serialize method declares that the 2nd parameter is of type "string" (so null is not accepted). With this patch we avoid having an error 500. Instead we return something that makes perfect sense when the OpenApi specification declares a content of type "text/plain" and that the returned value is for instance a string, an int, or a boolean.
1 parent 7b7c15c commit 36217ad

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ class JmsSerializer implements SerializerInterface
2929
*/
3030
public function serialize($data, string $format): string
3131
{
32-
return SerializerBuilder::create()->build()->serialize($data, $this->convertFormat($format));
32+
$convertFormat = $this->convertFormat($format);
33+
if ($convertFormat !== null) {
34+
return SerializerBuilder::create()->build()->serialize($data, $convertFormat);
35+
} else {
36+
// don't use var_export if $data is already a string: it may corrupt binary strings
37+
return is_string($data) ? $data : var_export($data, true);
38+
}
3339
}
3440
3541
/**

samples/server/petstore/php-symfony/SymfonyBundle-php/Service/JmsSerializer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public function __construct()
2929
*/
3030
public function serialize($data, string $format): string
3131
{
32-
return SerializerBuilder::create()->build()->serialize($data, $this->convertFormat($format));
32+
$convertFormat = $this->convertFormat($format);
33+
if ($convertFormat !== null) {
34+
return SerializerBuilder::create()->build()->serialize($data, $convertFormat);
35+
} else {
36+
// don't use var_export if $data is already a string: it may corrupt binary strings
37+
return is_string($data) ? $data : var_export($data, true);
38+
}
3339
}
3440

3541
/**

0 commit comments

Comments
 (0)