From 81a658cc71f164fb9b8af83713f056152de410c6 Mon Sep 17 00:00:00 2001 From: De Cramer Oliver Date: Wed, 16 Oct 2024 20:03:33 +0200 Subject: [PATCH 1/5] doc: Adding a tip about api's preventing methods beside GET to load non cart orders by default --- docs/customization/api/adding_and_removing_endpoints.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/customization/api/adding_and_removing_endpoints.rst b/docs/customization/api/adding_and_removing_endpoints.rst index 36c0fd239d..baa9890d4a 100644 --- a/docs/customization/api/adding_and_removing_endpoints.rst +++ b/docs/customization/api/adding_and_removing_endpoints.rst @@ -23,6 +23,13 @@ And that's all, now you have a new endpoint with your custom logic. Read more about API Platform endpoint configuration `here `_ +Good to Know +~~~~~~~~~~~~ + +.. tip:: + Api Platform is configured to prevent modifications to orders not in the ``cart`` state. There is only a few specific actions allowed. This is done by preventing orders not in the state ``cart`` from loading if the api's method is not ``GET``. So if you need to add an endpoint to an api that needs to edit orders that are not in the state ``cart`` you will need to whitelist your api. This can be done by adding your api's route to the ``sylius.api.doctrine_extension.order_shop_user_item.filter_cart.allowed_non_get_operations`` parameter. + + How to remove an endpoint? -------------------------- From b146c955a1553d48cd493d9d870937677981fe59 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Mon, 28 Oct 2024 14:03:18 +0100 Subject: [PATCH 2/5] [API] Refactor statistics constraints --- .../Controller/GetStatisticsAction.php | 39 ++++++++++++------- tests/Api/Admin/StatisticsTest.php | 8 ++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php b/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php index 2933485e58..4f4d1ac9e1 100644 --- a/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php +++ b/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php @@ -15,7 +15,7 @@ namespace Sylius\Bundle\ApiBundle\Controller; use ApiPlatform\Symfony\Validator\Exception\ValidationException; use Sylius\Bundle\ApiBundle\Query\GetStatistics; -use Sylius\Bundle\ApiBundle\Validator\Constraints; +use Sylius\Bundle\ApiBundle\Validator\Constraints\Code; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -25,6 +25,7 @@ use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints as SymfonyConstraints; +use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; final class GetStatisticsAction @@ -90,24 +91,32 @@ final class GetStatisticsAction private function createInputDataConstraints(): array { return [ - new SymfonyConstraints\Sequentially([ - new SymfonyConstraints\Collection([ - 'channelCode' => new Constraints\Code(), - 'startDate' => [ - new SymfonyConstraints\NotBlank(), - new SymfonyConstraints\DateTime('Y-m-d\TH:i:s', message: 'sylius.date_time.invalid'), - ], - 'interval' => new SymfonyConstraints\Choice(choices: array_keys($this->intervalsMap), multiple: false), - 'endDate' => [ - new SymfonyConstraints\NotBlank(), - new SymfonyConstraints\DateTime('Y-m-d\TH:i:s', message: 'sylius.date_time.invalid'), - ], - ]), - new SymfonyConstraints\Expression(expression: 'value["startDate"] < value["endDate"]', message: 'sylius.statistics.end_date.invalid'), + new SymfonyConstraints\Collection([ + 'channelCode' => new Code(), + 'startDate' => [ + new SymfonyConstraints\NotBlank(), + new SymfonyConstraints\DateTime('Y-m-d\TH:i:s', message: 'sylius.date_time.invalid'), + ], + 'interval' => new SymfonyConstraints\Choice(choices: array_keys($this->intervalsMap), multiple: false), + 'endDate' => [ + new SymfonyConstraints\NotBlank(), + new SymfonyConstraints\DateTime('Y-m-d\TH:i:s', message: 'sylius.date_time.invalid'), + ], ]), + new SymfonyConstraints\Callback(function (array $data, ExecutionContextInterface $context) { + $this->validateDateRange($data, $context); + }), ]; } + /** @param array $data */ + private function validateDateRange(array $data, ExecutionContextInterface $context): void + { + if (isset($data['startDate'], $data['endDate']) && $data['startDate'] >= $data['endDate']) { + $context->buildViolation('sylius.statistics.end_date.invalid')->addViolation(); + } + } + /** * @param array $intervalsMap * diff --git a/tests/Api/Admin/StatisticsTest.php b/tests/Api/Admin/StatisticsTest.php index 6600efaa59..156a88a8ee 100644 --- a/tests/Api/Admin/StatisticsTest.php +++ b/tests/Api/Admin/StatisticsTest.php @@ -347,6 +347,10 @@ final class StatisticsTest extends JsonApiTestCase 'propertyPath' => '[endDate]', 'message' => 'This value should not be blank.', ], + [ + 'propertyPath' => '', + 'message' => 'The start date must be earlier than the end date.', + ], ], ]; } @@ -380,6 +384,10 @@ final class StatisticsTest extends JsonApiTestCase 'propertyPath' => '[startDate]', 'message' => 'The date time is not valid ISO 8601 date time in Y-m-d\TH:i:s format.', ], + [ + 'propertyPath' => '', + 'message' => 'The start date must be earlier than the end date.', + ], ], ]; From 7dd327e6cf491be7f61f6511c8f481b306d757c0 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Tue, 29 Oct 2024 07:23:12 +0100 Subject: [PATCH 3/5] Apply suggestions from code review --- docs/customization/api/adding_and_removing_endpoints.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/customization/api/adding_and_removing_endpoints.rst b/docs/customization/api/adding_and_removing_endpoints.rst index baa9890d4a..3003d489ff 100644 --- a/docs/customization/api/adding_and_removing_endpoints.rst +++ b/docs/customization/api/adding_and_removing_endpoints.rst @@ -27,9 +27,11 @@ Good to Know ~~~~~~~~~~~~ .. tip:: - Api Platform is configured to prevent modifications to orders not in the ``cart`` state. There is only a few specific actions allowed. This is done by preventing orders not in the state ``cart`` from loading if the api's method is not ``GET``. So if you need to add an endpoint to an api that needs to edit orders that are not in the state ``cart`` you will need to whitelist your api. This can be done by adding your api's route to the ``sylius.api.doctrine_extension.order_shop_user_item.filter_cart.allowed_non_get_operations`` parameter. - - + API Platform is configured to prevent modifications to orders not in the ``cart`` state. There are only a few + specific actions allowed. This is done by preventing orders not in the state ``cart`` from loading if the method + is not ``GET``. So if you need to add an endpoint to an API that needs to edit orders that are not in the state + ``cart``, you will need to whitelist this endpoint. This can be done by adding your API route to the + ``sylius.api.doctrine_extension.order_shop_user_item.filter_cart.allowed_non_get_operations`` parameter. How to remove an endpoint? -------------------------- From f78d02d8a00351fc584dbaba71392b144da4cfe4 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Tue, 29 Oct 2024 07:23:48 +0100 Subject: [PATCH 4/5] Update docs/customization/api/adding_and_removing_endpoints.rst --- docs/customization/api/adding_and_removing_endpoints.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/customization/api/adding_and_removing_endpoints.rst b/docs/customization/api/adding_and_removing_endpoints.rst index 3003d489ff..27f523d8ef 100644 --- a/docs/customization/api/adding_and_removing_endpoints.rst +++ b/docs/customization/api/adding_and_removing_endpoints.rst @@ -32,6 +32,7 @@ Good to Know is not ``GET``. So if you need to add an endpoint to an API that needs to edit orders that are not in the state ``cart``, you will need to whitelist this endpoint. This can be done by adding your API route to the ``sylius.api.doctrine_extension.order_shop_user_item.filter_cart.allowed_non_get_operations`` parameter. + How to remove an endpoint? -------------------------- From 0e1c9ab4ddcdafd2404e4dff1eb8fdb13695d7e4 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Tue, 29 Oct 2024 09:40:15 +0100 Subject: [PATCH 5/5] [Admin] Pass admin path name parameter to AdminUriBasedSectionResolver --- src/Sylius/Bundle/AdminBundle/Resources/config/services.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sylius/Bundle/AdminBundle/Resources/config/services.xml b/src/Sylius/Bundle/AdminBundle/Resources/config/services.xml index b52eeea818..95583745c2 100644 --- a/src/Sylius/Bundle/AdminBundle/Resources/config/services.xml +++ b/src/Sylius/Bundle/AdminBundle/Resources/config/services.xml @@ -69,7 +69,7 @@ - /admin + /%sylius_admin.path_name%