From bb18191411d7488dbc5db9f0e53d643bb82d6c35 Mon Sep 17 00:00:00 2001 From: Guillaume Turri Date: Fri, 9 May 2025 23:46:48 +0200 Subject: [PATCH] [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. --- .../codegen/languages/PhpSymfonyServerCodegen.java | 12 +++++++++++- .../SymfonyBundle-php/Api/StoreApiInterface.php | 4 ++-- .../SymfonyBundle-php/Api/UserApiInterface.php | 4 ++-- .../SymfonyBundle-php/docs/Api/StoreApiInterface.md | 2 +- .../SymfonyBundle-php/docs/Api/UserApiInterface.md | 2 +- 5 files changed, 17 insertions(+), 7 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 2a3c2ffef01..fede4f2a34a 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,7 +417,17 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg op.vendorExtensions.put("x-comment-type", "void"); } // Create a variable to add typing for return value of interface - if (op.returnType != null) { + if ("bool".equals(op.returnType)) { + op.vendorExtensions.put("x-return-type", "bool"); + } else if ("UploadedFile".equals(op.returnType)) { + op.vendorExtensions.put("x-return-type", "string"); + } else if ("int".equals(op.returnType)) { + op.vendorExtensions.put("x-return-type", "int"); + } else if ("float".equals(op.returnType)) { + op.vendorExtensions.put("x-return-type", "float"); + } else if ("string".equals(op.returnType)) { + op.vendorExtensions.put("x-return-type", "string"); + } else if (op.returnType != null) { op.vendorExtensions.put("x-return-type", "array|object|null"); } else { op.vendorExtensions.put("x-return-type", "void"); diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/StoreApiInterface.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/StoreApiInterface.php index 097275dc0a4..4ed7e707986 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/StoreApiInterface.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/StoreApiInterface.php @@ -77,12 +77,12 @@ interface StoreApiInterface * @param int &$responseCode The HTTP Response Code * @param array $responseHeaders Additional HTTP headers to return with the response () * - * @return array|object|null + * @return int */ public function getInventory( int &$responseCode, array &$responseHeaders - ): array|object|null; + ): int; /** * Operation getOrderById diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/UserApiInterface.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/UserApiInterface.php index f98deb9fd07..0689aa1f710 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/UserApiInterface.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Api/UserApiInterface.php @@ -147,14 +147,14 @@ interface UserApiInterface * @param int &$responseCode The HTTP Response Code * @param array $responseHeaders Additional HTTP headers to return with the response () * - * @return array|object|null + * @return string */ public function loginUser( string $username, string $password, int &$responseCode, array &$responseHeaders - ): array|object|null; + ): string; /** * Operation logoutUser diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/StoreApiInterface.md b/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/StoreApiInterface.md index b6ed0824d5c..111a791612e 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/StoreApiInterface.md +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/StoreApiInterface.md @@ -107,7 +107,7 @@ class StoreApi implements StoreApiInterface /** * Implementation of StoreApiInterface#getInventory */ - public function getInventory(int &$responseCode, array &$responseHeaders): array|object|null + public function getInventory(int &$responseCode, array &$responseHeaders): int { // Implement the operation ... } diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/UserApiInterface.md b/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/UserApiInterface.md index 2c7e3ba2626..827393e38c5 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/UserApiInterface.md +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/docs/Api/UserApiInterface.md @@ -351,7 +351,7 @@ class UserApi implements UserApiInterface /** * Implementation of UserApiInterface#loginUser */ - public function loginUser(string $username, string $password, int &$responseCode, array &$responseHeaders): array|object|null + public function loginUser(string $username, string $password, int &$responseCode, array &$responseHeaders): string { // Implement the operation ... }