Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/filter/filter_validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion features/http_cache/headers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion src/Validator/Util/ParameterValidationConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
43 changes: 43 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/TranslateValidationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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 [];
}
}
2 changes: 2 additions & 0 deletions tests/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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,
];
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Functional/Parameters/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions tests/Functional/Parameters/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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']);
}
}
Loading