From 5969f0aa1dacfb6e226b67a8a842f0432117e19e Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Mon, 28 Oct 2024 12:33:53 +0100 Subject: [PATCH] [API] Apply statistics constraints sequentially to fix 500 error when using an undefined parameter --- .../Controller/GetStatisticsAction.php | 26 ++++++++++--------- tests/Api/Admin/StatisticsTest.php | 16 ------------ 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php b/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php index 28987ffe48..2933485e58 100644 --- a/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php +++ b/src/Sylius/Bundle/ApiBundle/Controller/GetStatisticsAction.php @@ -90,19 +90,21 @@ final class GetStatisticsAction private function createInputDataConstraints(): array { return [ - 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\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\Expression(expression: 'value["startDate"] < value["endDate"]', message: 'sylius.statistics.end_date.invalid'), ]; } diff --git a/tests/Api/Admin/StatisticsTest.php b/tests/Api/Admin/StatisticsTest.php index 5b8e725b29..6600efaa59 100644 --- a/tests/Api/Admin/StatisticsTest.php +++ b/tests/Api/Admin/StatisticsTest.php @@ -262,10 +262,6 @@ final class StatisticsTest extends JsonApiTestCase 'propertyPath' => '[endDate]', 'message' => 'This field is missing.', ], - [ - 'propertyPath' => '', - 'message' => 'The start date must be earlier than the end date.', - ], ], ]; @@ -288,10 +284,6 @@ final class StatisticsTest extends JsonApiTestCase 'propertyPath' => '[endDate]', 'message' => 'This field is missing.', ], - [ - 'propertyPath' => '', - 'message' => 'The start date must be earlier than the end date.', - ], ], ]; } @@ -355,10 +347,6 @@ 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.', - ], ], ]; } @@ -392,10 +380,6 @@ 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.', - ], ], ];