From 7b7c15ce6c642de72150be27fcd62d82c7bb05c6 Mon Sep 17 00:00:00 2001 From: Guillaume Turri Date: Fri, 9 May 2025 21:02:30 +0200 Subject: [PATCH 1/3] [php-symfony] Never return 406 when user accepts */* When a query has header "Accept" set to "*/*" it means it accepts everything. It is hence weird to return a 406. This patch ensures it does not occur: when the query accepts everything then we take any produced type. This fixes #13334. This also partly makes the open PR #15560 obsolete (or at least, it provides a workaround) --- .../src/main/resources/php-symfony/Controller.mustache | 4 ++++ .../php-symfony/SymfonyBundle-php/Controller/Controller.php | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache b/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache index 023b66c90f7d..1c3288d813bb 100644 --- a/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache +++ b/modules/openapi-generator/src/main/resources/php-symfony/Controller.mustache @@ -204,6 +204,10 @@ class Controller extends AbstractController return 'application/xml'; } + if (in_array('*/*', $accept)) { + return $produced[0]; + } + // If we reach this point, we don't have a common ground between server and client return null; } diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php index e21a2f138131..7d2600dbb6e7 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/Controller.php @@ -214,6 +214,10 @@ protected function getOutputFormat(string $accept, array $produced): ?string return 'application/xml'; } + if (in_array('*/*', $accept)) { + return $produced[0]; + } + // If we reach this point, we don't have a common ground between server and client return null; } From 36217ad7dc6adbace7294bd834224221e67e4833 Mon Sep 17 00:00:00 2001 From: Guillaume Turri Date: Fri, 9 May 2025 21:23:30 +0200 Subject: [PATCH 2/3] [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 9dfabc9733a3..6c93e947f425 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 5abbc6de3fb9..234056cf41d5 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 @@ public function __construct() */ 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); + } } /** From 945ef2379a5a8190d7d37196fecb564407008f58 Mon Sep 17 00:00:00 2001 From: Guillaume Turri Date: Fri, 9 May 2025 23:46:48 +0200 Subject: [PATCH 3/3] [php Symfony] fix return type for non json/xml api This fixes the generated returned type of controller methods for endpoint with a response declared like content: text/plain: schema: type: or for content: image/png: schema: type: string format: binary Without this commit the generated method *had to* return a value that matched "array|object|null", which does not work in this case. This commit makes it possible to return the proper type. --- .../languages/PhpSymfonyServerCodegen.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSymfonyServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSymfonyServerCodegen.java index 2a3c2ffef01d..db559d3c6685 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSymfonyServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSymfonyServerCodegen.java @@ -417,10 +417,16 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List produce : op.produces) { + String mediaType = produce.get("mediaType"); + if (isJsonMimeType(mediaType) || isXmlMimeType(mediaType)) { + return true; + } + } + } + return false; + } + @Override public ModelsMap postProcessModels(ModelsMap objs) { objs = super.postProcessModels(objs);