diff --git a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php index cd52043e351..2a8e27b4356 100644 --- a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php @@ -14,7 +14,7 @@ namespace ApiPlatform\Laravel\Metadata; use ApiPlatform\Metadata\Parameter; -use ApiPlatform\Metadata\Parameters; +use ApiPlatform\Metadata\Resource\Factory\ParameterValidationResourceMetadataCollectionFactoryTrait; use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; use Illuminate\Validation\Rule; @@ -22,6 +22,8 @@ final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface { + use ParameterValidationResourceMetadataCollectionFactoryTrait; + private const BOOLEAN_VALUES = ['true', 'false', '1', '0']; public function __construct( @@ -35,37 +37,7 @@ public function create(string $resourceClass): ResourceMetadataCollection $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass); foreach ($resourceMetadataCollection as $i => $resource) { - $operations = $resource->getOperations(); - - foreach ($operations as $operationName => $operation) { - $parameters = $operation->getParameters() ?? new Parameters(); - foreach ($operation->getParameters() ?? [] as $key => $parameter) { - $parameters = $parameters->with($key, $this->addSchemaValidation($parameter)); - } - - if (\count($parameters) > 0) { - $operations->add($operationName, $operation->withParameters($parameters)); - } - } - - $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort()); - - if (!$graphQlOperations = $resource->getGraphQlOperations()) { - continue; - } - - foreach ($graphQlOperations as $operationName => $operation) { - $parameters = $operation->getParameters() ?? new Parameters(); - foreach ($operation->getParameters() ?? [] as $key => $parameter) { - $parameters = $parameters->with($key, $this->addSchemaValidation($parameter)); - } - - if (\count($parameters) > 0) { - $graphQlOperations[$operationName] = $operation->withParameters($parameters); - } - } - - $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations); + $resourceMetadataCollection[$i] = $this->addParametersValidation($resource); } return $resourceMetadataCollection; @@ -150,14 +122,6 @@ private function addSchemaValidation(Parameter $parameter): Parameter $assertions[] = Rule::in(self::BOOLEAN_VALUES); } - if (!$assertions) { - return $parameter; - } - - if (1 === \count($assertions)) { - return $parameter->withConstraints($assertions[0]); - } - - return $parameter->withConstraints($assertions); + return $this->withConstraints($parameter, $assertions); } } diff --git a/src/Laravel/Tests/Metadata/ParameterValidationResourceMetadataCollectionFactoryTest.php b/src/Laravel/Tests/Metadata/ParameterValidationResourceMetadataCollectionFactoryTest.php new file mode 100644 index 00000000000..ebe1959685c --- /dev/null +++ b/src/Laravel/Tests/Metadata/ParameterValidationResourceMetadataCollectionFactoryTest.php @@ -0,0 +1,59 @@ + + * + * 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\Laravel\Tests\Metadata; + +use ApiPlatform\Laravel\Metadata\ParameterValidationResourceMetadataCollectionFactory; +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\GraphQl\QueryCollection; +use ApiPlatform\Metadata\QueryParameter; +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; +use PHPUnit\Framework\TestCase; + +final class ParameterValidationResourceMetadataCollectionFactoryTest extends TestCase +{ + public function testAddsRulesFromSchema(): void + { + $decorated = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); + $decorated->method('create')->willReturn(new ResourceMetadataCollection(\stdClass::class, [ + new ApiResource( + operations: [ + 'get_collection' => new GetCollection(parameters: [ + 'page' => new QueryParameter(key: 'page', schema: ['minimum' => 1]), + 'name' => new QueryParameter(key: 'name', schema: ['minLength' => 2, 'maxLength' => 10], required: true), + 'search' => new QueryParameter(key: 'search'), + ]), + ], + graphQlOperations: [ + 'collection_query' => new QueryCollection(parameters: [ + 'page' => new QueryParameter(key: 'page', schema: ['maximum' => 30]), + ]), + ], + ), + ])); + + $resourceMetadataCollection = (new ParameterValidationResourceMetadataCollectionFactory($decorated))->create(\stdClass::class); + + $parameters = $resourceMetadataCollection->getOperation('get_collection')->getParameters(); + $this->assertNotNull($parameters); + $this->assertSame('gte:1', $parameters->get('page')?->getConstraints()); + $this->assertSame(['required', 'between:2,10'], $parameters->get('name')?->getConstraints()); + $this->assertNull($parameters->get('search')?->getConstraints()); + + $graphQlParameters = $resourceMetadataCollection->getOperation('collection_query')->getParameters(); + $this->assertNotNull($graphQlParameters); + $this->assertSame('lte:30', $graphQlParameters->get('page')?->getConstraints()); + } +} diff --git a/src/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactoryTrait.php b/src/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactoryTrait.php new file mode 100644 index 00000000000..dae44c78f5d --- /dev/null +++ b/src/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactoryTrait.php @@ -0,0 +1,75 @@ + + * + * 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\Metadata\Resource\Factory; + +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Parameter; +use ApiPlatform\Metadata\Parameters; + +/** + * @internal + * + * This trait shares the common logic between the Symfony and Laravel parameter validation factories + */ +trait ParameterValidationResourceMetadataCollectionFactoryTrait +{ + abstract private function addSchemaValidation(Parameter $parameter): Parameter; + + private function addParametersValidation(ApiResource $resource): ApiResource + { + $operations = $resource->getOperations(); + + foreach ($operations as $operationName => $operation) { + $parameters = $operation->getParameters() ?? new Parameters(); + foreach ($operation->getParameters() ?? [] as $key => $parameter) { + $parameters = $parameters->with($key, $this->addSchemaValidation($parameter)); + } + + if (\count($parameters) > 0) { + $operations->add($operationName, $operation->withParameters($parameters)); + } + } + + $resource = $resource->withOperations($operations->sort()); + + if (!$graphQlOperations = $resource->getGraphQlOperations()) { + return $resource; + } + + foreach ($graphQlOperations as $operationName => $operation) { + $parameters = $operation->getParameters() ?? new Parameters(); + foreach ($operation->getParameters() ?? [] as $key => $parameter) { + $parameters = $parameters->with($key, $this->addSchemaValidation($parameter)); + } + + if (\count($parameters) > 0) { + $graphQlOperations[$operationName] = $operation->withParameters($parameters); + } + } + + return $resource->withGraphQlOperations($graphQlOperations); + } + + /** + * @param list $constraints + */ + private function withConstraints(Parameter $parameter, array $constraints): Parameter + { + if (!$constraints) { + return $parameter; + } + + return $parameter->withConstraints(1 === \count($constraints) ? $constraints[0] : $constraints); + } +} diff --git a/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php b/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php index ee76d9dc2a4..56b00011fb5 100644 --- a/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php @@ -18,6 +18,7 @@ use ApiPlatform\Metadata\Parameter; use ApiPlatform\Metadata\Parameters; use ApiPlatform\Metadata\QueryParameter; +use ApiPlatform\Metadata\Resource\Factory\ParameterValidationResourceMetadataCollectionFactoryTrait; use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; @@ -28,6 +29,7 @@ final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface { use ParameterValidationConstraints; + use ParameterValidationResourceMetadataCollectionFactoryTrait; public function __construct( private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, @@ -43,15 +45,12 @@ public function create(string $resourceClass): ResourceMetadataCollection $defaultParams = $this->buildDefaultParameters(); foreach ($resourceMetadataCollection as $i => $resource) { - $resource = $this->applyDefaults($resource, $defaultParams); + $resource = $this->addParametersValidation($this->applyDefaults($resource, $defaultParams)); $operations = $resource->getOperations(); foreach ($operations as $operationName => $operation) { $parameters = $operation->getParameters() ?? new Parameters(); - foreach ($operation->getParameters() ?? [] as $key => $parameter) { - $parameters = $parameters->with($key, $this->addSchemaValidation($parameter)); - } // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system. $explicitParametersCount = 0; @@ -72,24 +71,7 @@ public function create(string $resourceClass): ResourceMetadataCollection } } - $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort()); - - if (!$graphQlOperations = $resource->getGraphQlOperations()) { - continue; - } - - foreach ($graphQlOperations as $operationName => $operation) { - $parameters = $operation->getParameters() ?? new Parameters(); - foreach ($operation->getParameters() ?? [] as $key => $parameter) { - $parameters = $parameters->with($key, $this->addSchemaValidation($parameter)); - } - - if (\count($parameters) > 0) { - $graphQlOperations[$operationName] = $operation->withParameters($parameters); - } - } - - $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations); + $resourceMetadataCollection[$i] = $resource->withOperations($operations); } return $resourceMetadataCollection; @@ -101,16 +83,7 @@ private function addSchemaValidation(Parameter $parameter, ?array $schema = null return $parameter; } - $assertions = $this->getParameterValidationConstraints($parameter, $schema, $required, $openApi); - if (!$assertions) { - return $parameter; - } - - if (1 === \count($assertions)) { - return $parameter->withConstraints($assertions[0]); - } - - return $parameter->withConstraints($assertions); + return $this->withConstraints($parameter, $this->getParameterValidationConstraints($parameter, $schema, $required, $openApi)); } private function addFilterValidation(HttpOperation $operation): Parameters diff --git a/src/Validator/composer.json b/src/Validator/composer.json index 50ad44fa9e5..1f0b5d1ab6a 100644 --- a/src/Validator/composer.json +++ b/src/Validator/composer.json @@ -23,7 +23,7 @@ ], "require": { "php": ">=8.2", - "api-platform/metadata": "^5.0", + "api-platform/metadata": "^5.1", "api-platform/state": "^5.1", "symfony/type-info": "^7.4 || ^8.0", "symfony/http-kernel": "^7.4 || ^8.0",