From 600ef008a348c0663c7b274fcfa3eccc01be589c Mon Sep 17 00:00:00 2001 From: Rafikooo Date: Thu, 13 Apr 2023 15:35:04 +0200 Subject: [PATCH] [API] Restore default API Platform exception response codes --- phpunit.xml.dist | 1 + .../Resources/config/app/config.yaml | 8 +++++ ...sonApiGenericRequestValidationTestCase.php | 36 +++++++++++++++++++ .../Expected/malformed_json_response.json | 6 ++++ 4 files changed, 51 insertions(+) create mode 100644 tests/Api/JsonApiGenericRequestValidationTestCase.php create mode 100644 tests/Api/Responses/Expected/malformed_json_response.json diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0f4f9a6134..f7c4b7da20 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,6 +17,7 @@ + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml b/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml index 2725e17fb9..918f69fdd7 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/app/config.yaml @@ -35,6 +35,14 @@ api_platform: name: "%sylius.api.authorization_header%" type: header exception_to_status: + # Default API Platform exception to status code mapping + ApiPlatform\Exception\FilterValidationException: 400 + ApiPlatform\Exception\InvalidArgumentException: 400 + ApiPlatform\Validator\Exception\ValidationException: 422 + Doctrine\ORM\OptimisticLockException: 409 + Symfony\Component\Serializer\Exception\ExceptionInterface: 400 + + # Sylius exception to status code mapping SM\SMException: 422 Sylius\Bundle\ApiBundle\Exception\CannotRemoveCurrentlyLoggedInUser: 422 Sylius\Bundle\ApiBundle\Exception\OrderItemNotFoundException: 422 diff --git a/tests/Api/JsonApiGenericRequestValidationTestCase.php b/tests/Api/JsonApiGenericRequestValidationTestCase.php new file mode 100644 index 0000000000..f422cb21e3 --- /dev/null +++ b/tests/Api/JsonApiGenericRequestValidationTestCase.php @@ -0,0 +1,36 @@ +client->request( + method: 'POST', + uri: '/api/v2/shop/orders', + server: self::CONTENT_TYPE_HEADER, + content: 'Malformed JSON: the provided JSON payload is not properly formatted.' + ); + + $this->assertResponse( + $this->client->getResponse(), + 'malformed_json_response', + Response::HTTP_BAD_REQUEST, + ); + } +} diff --git a/tests/Api/Responses/Expected/malformed_json_response.json b/tests/Api/Responses/Expected/malformed_json_response.json new file mode 100644 index 0000000000..d269c897e2 --- /dev/null +++ b/tests/Api/Responses/Expected/malformed_json_response.json @@ -0,0 +1,6 @@ +{ + "@context": "\/api\/v2\/contexts\/Error", + "@type": "hydra:Error", + "hydra:title": "An error occurred", + "hydra:description": "Syntax error" +}