From 2ee6abfae650bab5954944ea1518a7b91b5b7dbe Mon Sep 17 00:00:00 2001 From: Gocha Ossinkine Date: Wed, 6 Feb 2019 17:56:54 +0500 Subject: [PATCH] Catch serializer exception --- .../php-symfony/api_controller.mustache | 27 +++++---- .../Controller/PetController.php | 57 +++++++++++++++---- .../Controller/StoreController.php | 21 +++++-- .../Controller/UserController.php | 49 ++++++++++++---- 4 files changed, 116 insertions(+), 38 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php-symfony/api_controller.mustache b/modules/openapi-generator/src/main/resources/php-symfony/api_controller.mustache index b0f2efbc93fe..4063695da342 100644 --- a/modules/openapi-generator/src/main/resources/php-symfony/api_controller.mustache +++ b/modules/openapi-generator/src/main/resources/php-symfony/api_controller.mustache @@ -20,6 +20,7 @@ namespace {{controllerPackage}}; use \Exception; +use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -137,18 +138,24 @@ class {{controllerName}} extends Controller {{/isContainer}} {{/required}} + {{#hasParams}} // Deserialize the input values that needs it - {{#allParams}} - {{^isFile}} - {{#isBodyParam}} - ${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}{{#items}}array<{{dataType}}>{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', $inputFormat); - {{/isBodyParam}} - {{^isBodyParam}} - ${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}array<{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}{{^collectionFormat}}csv{{/collectionFormat}},{{dataType}}>{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', 'string'); - {{/isBodyParam}} - {{/isFile}} - {{/allParams}} + try { + {{#allParams}} + {{^isFile}} + {{#isBodyParam}} + ${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}{{#items}}array<{{dataType}}>{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', $inputFormat); + {{/isBodyParam}} + {{^isBodyParam}} + ${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}array<{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}{{^collectionFormat}}csv{{/collectionFormat}},{{dataType}}>{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', 'string'); + {{/isBodyParam}} + {{/isFile}} + {{/allParams}} + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } + {{/hasParams}} // Validate the input values {{>api_input_validation}} diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/PetController.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/PetController.php index 3eda55dfa00f..b3141f14b99c 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/PetController.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/PetController.php @@ -30,6 +30,7 @@ namespace OpenAPI\Server\Controller; use \Exception; +use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -87,7 +88,11 @@ public function addPetAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat); + try { + $body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -166,8 +171,12 @@ public function deletePetAction(Request $request, $petId) // Use the default value if no value was provided // Deserialize the input values that needs it - $petId = $this->deserialize($petId, 'int', 'string'); - $apiKey = $this->deserialize($apiKey, 'string', 'string'); + try { + $petId = $this->deserialize($petId, 'int', 'string'); + $apiKey = $this->deserialize($apiKey, 'string', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -252,7 +261,11 @@ public function findPetsByStatusAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $status = $this->deserialize($status, 'array', 'string'); + try { + $status = $this->deserialize($status, 'array', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -339,7 +352,11 @@ public function findPetsByTagsAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $tags = $this->deserialize($tags, 'array', 'string'); + try { + $tags = $this->deserialize($tags, 'array', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -422,7 +439,11 @@ public function getPetByIdAction(Request $request, $petId) // Use the default value if no value was provided // Deserialize the input values that needs it - $petId = $this->deserialize($petId, 'int', 'string'); + try { + $petId = $this->deserialize($petId, 'int', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -515,7 +536,11 @@ public function updatePetAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat); + try { + $body = $this->deserialize($body, 'OpenAPI\Server\Model\Pet', $inputFormat); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -601,9 +626,13 @@ public function updatePetWithFormAction(Request $request, $petId) // Use the default value if no value was provided // Deserialize the input values that needs it - $petId = $this->deserialize($petId, 'int', 'string'); - $name = $this->deserialize($name, 'string', 'string'); - $status = $this->deserialize($status, 'string', 'string'); + try { + $petId = $this->deserialize($petId, 'int', 'string'); + $name = $this->deserialize($name, 'string', 'string'); + $status = $this->deserialize($status, 'string', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -695,8 +724,12 @@ public function uploadFileAction(Request $request, $petId) // Use the default value if no value was provided // Deserialize the input values that needs it - $petId = $this->deserialize($petId, 'int', 'string'); - $additionalMetadata = $this->deserialize($additionalMetadata, 'string', 'string'); + try { + $petId = $this->deserialize($petId, 'int', 'string'); + $additionalMetadata = $this->deserialize($additionalMetadata, 'string', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/StoreController.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/StoreController.php index 96bf511cf181..dce68da494d5 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/StoreController.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/StoreController.php @@ -30,6 +30,7 @@ namespace OpenAPI\Server\Controller; use \Exception; +use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -74,7 +75,11 @@ public function deleteOrderAction(Request $request, $orderId) // Use the default value if no value was provided // Deserialize the input values that needs it - $orderId = $this->deserialize($orderId, 'string', 'string'); + try { + $orderId = $this->deserialize($orderId, 'string', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -152,8 +157,6 @@ public function getInventoryAction(Request $request) // Use the default value if no value was provided - // Deserialize the input values that needs it - // Validate the input values @@ -220,7 +223,11 @@ public function getOrderByIdAction(Request $request, $orderId) // Use the default value if no value was provided // Deserialize the input values that needs it - $orderId = $this->deserialize($orderId, 'int', 'string'); + try { + $orderId = $this->deserialize($orderId, 'int', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -310,7 +317,11 @@ public function placeOrderAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $body = $this->deserialize($body, 'OpenAPI\Server\Model\Order', $inputFormat); + try { + $body = $this->deserialize($body, 'OpenAPI\Server\Model\Order', $inputFormat); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/UserController.php b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/UserController.php index c2c454fc131a..d2b32eb4af18 100644 --- a/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/UserController.php +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/UserController.php @@ -30,6 +30,7 @@ namespace OpenAPI\Server\Controller; use \Exception; +use JMS\Serializer\Exception\RuntimeException as SerializerRuntimeException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -83,7 +84,11 @@ public function createUserAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat); + try { + $body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -165,7 +170,11 @@ public function createUsersWithArrayInputAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $body = $this->deserialize($body, 'array', $inputFormat); + try { + $body = $this->deserialize($body, 'array', $inputFormat); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -249,7 +258,11 @@ public function createUsersWithListInputAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $body = $this->deserialize($body, 'array', $inputFormat); + try { + $body = $this->deserialize($body, 'array', $inputFormat); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -324,7 +337,11 @@ public function deleteUserAction(Request $request, $username) // Use the default value if no value was provided // Deserialize the input values that needs it - $username = $this->deserialize($username, 'string', 'string'); + try { + $username = $this->deserialize($username, 'string', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -400,7 +417,11 @@ public function getUserByNameAction(Request $request, $username) // Use the default value if no value was provided // Deserialize the input values that needs it - $username = $this->deserialize($username, 'string', 'string'); + try { + $username = $this->deserialize($username, 'string', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -481,8 +502,12 @@ public function loginUserAction(Request $request) // Use the default value if no value was provided // Deserialize the input values that needs it - $username = $this->deserialize($username, 'string', 'string'); - $password = $this->deserialize($password, 'string', 'string'); + try { + $username = $this->deserialize($username, 'string', 'string'); + $password = $this->deserialize($password, 'string', 'string'); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = []; @@ -564,8 +589,6 @@ public function logoutUserAction(Request $request) // Use the default value if no value was provided - // Deserialize the input values that needs it - // Validate the input values @@ -639,8 +662,12 @@ public function updateUserAction(Request $request, $username) // Use the default value if no value was provided // Deserialize the input values that needs it - $username = $this->deserialize($username, 'string', 'string'); - $body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat); + try { + $username = $this->deserialize($username, 'string', 'string'); + $body = $this->deserialize($body, 'OpenAPI\Server\Model\User', $inputFormat); + } catch (SerializerRuntimeException $exception) { + return $this->createBadRequestResponse($exception->getMessage()); + } // Validate the input values $asserts = [];