Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
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;
use Psr\Container\ContainerInterface;

final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
use ParameterValidationResourceMetadataCollectionFactoryTrait;

private const BOOLEAN_VALUES = ['true', 'false', '1', '0'];

public function __construct(
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\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<mixed> $constraints
*/
private function withConstraints(Parameter $parameter, array $constraints): Parameter
{
if (!$constraints) {
return $parameter;
}

return $parameter->withConstraints(1 === \count($constraints) ? $constraints[0] : $constraints);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
use ParameterValidationConstraints;
use ParameterValidationResourceMetadataCollectionFactoryTrait;

public function __construct(
private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading