From 36217ad7dc6adbace7294bd834224221e67e4833 Mon Sep 17 00:00:00 2001 From: Guillaume Turri Date: Fri, 9 May 2025 21:23:30 +0200 Subject: [PATCH] [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. --- .../php-symfony/serialization/JmsSerializer.mustache | 8 +++++++- .../SymfonyBundle-php/Service/JmsSerializer.php | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache b/modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache index 9dfabc9733a..6c93e947f42 100644 --- a/modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache @@ -29,7 +29,13 @@ class JmsSerializer implements SerializerInterface */ public function serialize($data, string $format): string { - return SerializerBuilder::create()->build()->serialize($data, $this->convertFormat($format)); + $convertFormat = $this->convertFormat($format); + if ($convertFormat !== null) { + return SerializerBuilder::create()->build()->serialize($data, $convertFormat); + } else { + // don't use var_export if $data is already a string: it may corrupt binary strings + return is_string($data) ? $data : var_export($data, true); + } } /** diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Service/JmsSerializer.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Service/JmsSerializer.php index 5abbc6de3fb..234056cf41d 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Service/JmsSerializer.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Service/JmsSerializer.php @@ -29,7 +29,13 @@ class JmsSerializer implements SerializerInterface */ public function serialize($data, string $format): string { - return SerializerBuilder::create()->build()->serialize($data, $this->convertFormat($format)); + $convertFormat = $this->convertFormat($format); + if ($convertFormat !== null) { + return SerializerBuilder::create()->build()->serialize($data, $convertFormat); + } else { + // don't use var_export if $data is already a string: it may corrupt binary strings + return is_string($data) ? $data : var_export($data, true); + } } /**