diff --git a/features/filter/filter_validation.feature b/features/filter/filter_validation.feature index 326c23982f8..5119643e553 100644 --- a/features/filter/filter_validation.feature +++ b/features/filter/filter_validation.feature @@ -16,7 +16,7 @@ Feature: Validate filters based upon filter description Scenario: Required filter should throw an error if not set When I am on "/filter_validators" Then the response status code should be 422 - And the JSON node "detail" should be equal to 'required: This value should not be blank.\nrequired-allow-empty: The parameter "required-allow-empty" is required.' + And the JSON node "detail" should be equal to 'required: This value should not be blank.\nrequired-allow-empty: This value should not be null.' Scenario: Required filter should not throw an error if set When I am on "/array_filter_validators?arrayRequired[]=foo&indexedArrayRequired[foo]=foo" diff --git a/features/http_cache/headers.feature b/features/http_cache/headers.feature index 2fe42bd14c3..7c000f79b05 100644 --- a/features/http_cache/headers.feature +++ b/features/http_cache/headers.feature @@ -9,4 +9,4 @@ Feature: Default values of HTTP cache headers Then the response status code should be 200 And the header "Etag" should be equal to '"032297ac74d75a50"' And the header "Cache-Control" should be equal to "max-age=60, public, s-maxage=3600" - And the header "Vary" should be equal to "Accept, Cookie" + And the header "Vary" should be equal to "Accept, Cookie, Accept-Language" diff --git a/src/Validator/Util/ParameterValidationConstraints.php b/src/Validator/Util/ParameterValidationConstraints.php index 93fcc7502a7..836c74ef8b0 100644 --- a/src/Validator/Util/ParameterValidationConstraints.php +++ b/src/Validator/Util/ParameterValidationConstraints.php @@ -133,7 +133,7 @@ public static function getParameterValidationConstraints(Parameter $parameter, ? } if ($required && false !== $allowEmptyValue) { - $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey())); + $assertions[] = new NotNull(); } if (isset($schema['minItems']) || isset($schema['maxItems'])) { diff --git a/tests/Fixtures/TestBundle/ApiResource/TranslateValidationError.php b/tests/Fixtures/TestBundle/ApiResource/TranslateValidationError.php new file mode 100644 index 00000000000..0fb8b907031 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/TranslateValidationError.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource; + +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\Operation; +use ApiPlatform\Metadata\QueryParameter; +use Symfony\Component\Validator\Constraints as Assert; + +#[GetCollection( + uriTemplate: '/translate_validation_error', + parameters: [ + 'name' => new QueryParameter( + description: 'Nome della persona', + constraints: [ + new Assert\NotBlank(), + ] + ), + 'surname' => new QueryParameter( + description: 'Cognome della persona', + required: true, + ), + ], + provider: [self::class, 'provide'] +)] +class TranslateValidationError +{ + public static function provide(Operation $operation, array $uriVariables = []): array + { + return []; + } +} diff --git a/tests/Fixtures/app/AppKernel.php b/tests/Fixtures/app/AppKernel.php index 878f774c1ca..2513b7d02d2 100644 --- a/tests/Fixtures/app/AppKernel.php +++ b/tests/Fixtures/app/AppKernel.php @@ -139,6 +139,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load 'annotations' => false, 'handle_all_throwables' => true, 'uid' => ['default_uuid_version' => 7, 'time_based_uuid_version' => 7], + 'set_locale_from_accept_language' => true, ]; } else { $config = [ @@ -155,6 +156,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load 'router' => ['utf8' => true], 'http_method_override' => false, 'annotations' => false, + 'set_locale_from_accept_language' => true, ]; } diff --git a/tests/Functional/Parameters/ParameterTest.php b/tests/Functional/Parameters/ParameterTest.php index 2df72c458c9..122b195df1f 100644 --- a/tests/Functional/Parameters/ParameterTest.php +++ b/tests/Functional/Parameters/ParameterTest.php @@ -14,6 +14,7 @@ namespace ApiPlatform\Tests\Functional\Parameters; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\TranslateValidationError; use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter; use ApiPlatform\Tests\SetupClassResourcesTrait; use PHPUnit\Framework\Attributes\DataProvider; @@ -29,7 +30,7 @@ final class ParameterTest extends ApiTestCase */ public static function getResources(): array { - return [WithParameter::class]; + return [WithParameter::class, TranslateValidationError::class]; } public function testWithGroupFilter(): void diff --git a/tests/Functional/Parameters/ValidationTest.php b/tests/Functional/Parameters/ValidationTest.php index 803f1645e29..1476e74556b 100644 --- a/tests/Functional/Parameters/ValidationTest.php +++ b/tests/Functional/Parameters/ValidationTest.php @@ -14,6 +14,7 @@ namespace ApiPlatform\Tests\Functional\Parameters; use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\TranslateValidationError; use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ValidateParameterBeforeProvider; use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter; use ApiPlatform\Tests\SetupClassResourcesTrait; @@ -29,13 +30,13 @@ final class ValidationTest extends ApiTestCase */ public static function getResources(): array { - return [WithParameter::class, ValidateParameterBeforeProvider::class]; + return [WithParameter::class, ValidateParameterBeforeProvider::class, TranslateValidationError::class]; } public function testWithGroupFilter(): void { $response = self::createClient()->request('GET', 'with_parameters_collection'); - $this->assertArraySubset(['violations' => [['message' => 'The parameter "hydra" is required.']]], $response->toArray(false)); + $this->assertArraySubset(['violations' => [['message' => 'This value should not be null.']]], $response->toArray(false)); $response = self::createClient()->request('GET', 'with_parameters_collection?hydra'); $this->assertResponseIsSuccessful(); } @@ -188,4 +189,13 @@ public function testValidatePattern(): void self::createClient()->request('GET', 'validate_parameters?pattern=2'); $this->assertResponseIsSuccessful(); } + + public function testTranslationValidation(): void + { + $res = self::createClient()->request('GET', 'translate_validation_error', ['headers' => ['accept-language' => 'es']]); + $this->assertSame([ + ['propertyPath' => 'name', 'message' => 'Este valor no debería estar vacío.', 'code' => 'c1051bb4-d103-4f74-8988-acbcafc7fdc3'], + ['propertyPath' => 'surname', 'message' => 'Este valor no debería ser nulo.', 'code' => 'ad32d13f-c3d4-423b-909a-857b961eb720'], + ], $res->toArray(false)['violations']); + } }