[API] Apply statistics constraints sequentially to fix 500 error when using an undefined parameter

This commit is contained in:
Grzegorz Sadowski 2024-10-28 12:33:53 +01:00
parent d37c6a8947
commit 5969f0aa1d
No known key found for this signature in database
GPG key ID: EF87FF4E6E3BD364
2 changed files with 14 additions and 28 deletions

View file

@ -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'),
];
}

View file

@ -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.',
],
],
];