Skip to content

Commit 0edc738

Browse files
authored
fix(state): allow to skip parameter validator provider (#6452)
1 parent 25c5f22 commit 0edc738

17 files changed

+81
-49
lines changed

src/Metadata/ApiResource.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ public function __construct(
960960
$provider = null,
961961
$processor = null,
962962
protected ?OptionsInterface $stateOptions = null,
963-
protected array|Parameters|null $parameters = null,
963+
array|Parameters|null $parameters = null,
964964
protected array $extraProperties = [],
965965
) {
966966
parent::__construct(
@@ -1389,19 +1389,6 @@ public function withExceptionToStatus(array $exceptionToStatus): self
13891389
return $self;
13901390
}
13911391

1392-
public function getQueryParameterValidationEnabled(): ?bool
1393-
{
1394-
return $this->queryParameterValidationEnabled;
1395-
}
1396-
1397-
public function withQueryParameterValidationEnabled(bool $queryParameterValidationEnabled): self
1398-
{
1399-
$self = clone $this;
1400-
$self->queryParameterValidationEnabled = $queryParameterValidationEnabled;
1401-
1402-
return $self;
1403-
}
1404-
14051392
/**
14061393
* @return GraphQlOperation[]
14071394
*/

src/Metadata/Extractor/schema/resources.xsd

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<xsd:attributeGroup ref="base"/>
6767
<xsd:attribute type="xsd:string" name="resolver"/>
6868
<xsd:attribute type="xsd:string" name="class" use="required"/>
69+
<xsd:attribute type="xsd:boolean" name="queryParameterValidationEnabled"/>
6970
<xsd:attribute type="xsd:boolean" name="read"/>
7071
<xsd:attribute type="xsd:boolean" name="deserialize"/>
7172
<xsd:attribute type="xsd:boolean" name="validate"/>

src/Metadata/GraphQl/Operation.php

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function __construct(
8989
$processor = null,
9090
?OptionsInterface $stateOptions = null,
9191
array|Parameters|null $parameters = null,
92+
?bool $queryParameterValidationEnabled = null,
9293
array $extraProperties = []
9394
) {
9495
parent::__construct(
@@ -137,6 +138,7 @@ class: $class,
137138
processor: $processor,
138139
stateOptions: $stateOptions,
139140
parameters: $parameters,
141+
queryParameterValidationEnabled: $queryParameterValidationEnabled,
140142
extraProperties: $extraProperties
141143
);
142144
}

src/Metadata/GraphQl/Query.php

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function __construct(
7272
$processor = null,
7373
?OptionsInterface $stateOptions = null,
7474
array|Parameters|null $parameters = null,
75+
?bool $queryParameterValidationEnabled = null,
7576
array $extraProperties = [],
7677

7778
protected ?bool $nested = null,
@@ -128,6 +129,7 @@ class: $class,
128129
processor: $processor,
129130
stateOptions: $stateOptions,
130131
parameters: $parameters,
132+
queryParameterValidationEnabled: $queryParameterValidationEnabled,
131133
extraProperties: $extraProperties
132134
);
133135
}

src/Metadata/GraphQl/QueryCollection.php

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function __construct(
7373
$processor = null,
7474
protected ?OptionsInterface $stateOptions = null,
7575
array|Parameters|null $parameters = null,
76+
?bool $queryParameterValidationEnabled = null,
7677
array $extraProperties = [],
7778

7879
?bool $nested = null,
@@ -128,6 +129,7 @@ class: $class,
128129
provider: $provider,
129130
processor: $processor,
130131
parameters: $parameters,
132+
queryParameterValidationEnabled: $queryParameterValidationEnabled,
131133
extraProperties: $extraProperties,
132134
nested: $nested,
133135
);

src/Metadata/GraphQl/Subscription.php

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function __construct(
7272
$processor = null,
7373
?OptionsInterface $stateOptions = null,
7474
array|Parameters|null $parameters = null,
75+
?bool $queryParameterValidationEnabled = null,
7576
array $extraProperties = [],
7677
) {
7778
parent::__construct(
@@ -126,6 +127,7 @@ class: $class,
126127
processor: $processor,
127128
stateOptions: $stateOptions,
128129
parameters: $parameters,
130+
queryParameterValidationEnabled: $queryParameterValidationEnabled,
129131
extraProperties: $extraProperties,
130132
);
131133
}

src/Metadata/HttpOperation.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ public function __construct(
153153
protected ?array $openapiContext = null, // TODO Remove in 4.0
154154
protected bool|OpenApiOperation|Webhook|null $openapi = null,
155155
protected ?array $exceptionToStatus = null,
156-
protected ?bool $queryParameterValidationEnabled = null,
157156
protected ?array $links = null,
158157

159158
?string $shortName = null,
@@ -201,6 +200,7 @@ public function __construct(
201200
$processor = null,
202201
?OptionsInterface $stateOptions = null,
203202
array|Parameters|null $parameters = null,
203+
?bool $queryParameterValidationEnabled = null,
204204
array $extraProperties = [],
205205
) {
206206
parent::__construct(
@@ -249,6 +249,7 @@ class: $class,
249249
processor: $processor,
250250
stateOptions: $stateOptions,
251251
parameters: $parameters,
252+
queryParameterValidationEnabled: $queryParameterValidationEnabled,
252253
extraProperties: $extraProperties
253254
);
254255
}
@@ -607,19 +608,6 @@ public function withExceptionToStatus(array $exceptionToStatus): self
607608
return $self;
608609
}
609610

610-
public function getQueryParameterValidationEnabled(): ?bool
611-
{
612-
return $this->queryParameterValidationEnabled;
613-
}
614-
615-
public function withQueryParameterValidationEnabled(bool $queryParameterValidationEnabled): self
616-
{
617-
$self = clone $this;
618-
$self->queryParameterValidationEnabled = $queryParameterValidationEnabled;
619-
620-
return $self;
621-
}
622-
623611
public function getLinks(): ?array
624612
{
625613
return $this->links;

src/Metadata/Metadata.php

+25-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
abstract class Metadata
2222
{
23+
protected ?Parameters $parameters = null;
24+
2325
/**
2426
* @param string|null $deprecationReason https://api-platform.com/docs/core/deprecations/#deprecating-resource-classes-operations-and-properties
2527
* @param string|\Stringable|null $security https://api-platform.com/docs/core/security
@@ -70,12 +72,18 @@ public function __construct(
7072
protected $provider = null,
7173
protected $processor = null,
7274
protected ?OptionsInterface $stateOptions = null,
73-
/**
75+
/*
7476
* @experimental
7577
*/
76-
protected array|Parameters|null $parameters = [],
78+
array|Parameters|null $parameters = null,
79+
protected ?bool $queryParameterValidationEnabled = null,
7780
protected array $extraProperties = []
7881
) {
82+
if (\is_array($parameters) && $parameters) {
83+
$parameters = new Parameters($parameters);
84+
}
85+
86+
$this->parameters = $parameters;
7987
}
8088

8189
public function getShortName(): ?string
@@ -571,18 +579,28 @@ public function withStateOptions(?OptionsInterface $stateOptions): static
571579
return $self;
572580
}
573581

574-
/**
575-
* @return array<string, Parameter>
576-
*/
577-
public function getParameters(): array|Parameters|null
582+
public function getParameters(): ?Parameters
578583
{
579584
return $this->parameters;
580585
}
581586

582587
public function withParameters(array|Parameters $parameters): static
583588
{
584589
$self = clone $this;
585-
$self->parameters = $parameters;
590+
$self->parameters = \is_array($parameters) ? new Parameters($parameters) : $parameters;
591+
592+
return $self;
593+
}
594+
595+
public function getQueryParameterValidationEnabled(): ?bool
596+
{
597+
return $this->queryParameterValidationEnabled;
598+
}
599+
600+
public function withQueryParameterValidationEnabled(bool $queryParameterValidationEnabled): static
601+
{
602+
$self = clone $this;
603+
$self->queryParameterValidationEnabled = $queryParameterValidationEnabled;
586604

587605
return $self;
588606
}

src/Metadata/Operation.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,8 @@ public function __construct(
806806
protected $provider = null,
807807
protected $processor = null,
808808
protected ?OptionsInterface $stateOptions = null,
809-
protected array|Parameters|null $parameters = [],
809+
array|Parameters|null $parameters = null,
810+
?bool $queryParameterValidationEnabled = null,
810811
protected array $extraProperties = [],
811812
) {
812813
parent::__construct(
@@ -848,6 +849,7 @@ class: $class,
848849
processor: $processor,
849850
stateOptions: $stateOptions,
850851
parameters: $parameters,
852+
queryParameterValidationEnabled: $queryParameterValidationEnabled,
851853
extraProperties: $extraProperties,
852854
);
853855
}

src/Metadata/Parameters.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class Parameters implements \IteratorAggregate, \Countable
2323
private array $parameters = [];
2424

2525
/**
26-
* @param array<string, Parameter> $parameters
26+
* @param array<int|string, Parameter> $parameters
2727
*/
2828
public function __construct(array $parameters = [])
2929
{

src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Metadata\Metadata;
2222
use ApiPlatform\Metadata\Operations;
2323
use ApiPlatform\Metadata\Parameter;
24+
use ApiPlatform\Metadata\Parameters;
2425
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2526
use ApiPlatform\Metadata\Util\CamelCaseToSnakeCaseNameConverter;
2627
use Psr\Log\LoggerInterface;
@@ -86,15 +87,15 @@ private function buildResourceOperations(array $attributes, string $resourceClas
8687
$index = -1;
8788
$operationPriority = 0;
8889
$hasApiResource = false;
89-
$globalParameters = [];
90+
$globalParameters = new Parameters();
9091

9192
foreach ($attributes as $attribute) {
9293
if (is_a($attribute->getName(), Parameter::class, true)) {
9394
$parameter = $attribute->newInstance();
9495
if (!$k = $parameter->getKey()) {
9596
throw new RuntimeException('Parameter "key" is mandatory when used on a class.');
9697
}
97-
$globalParameters[$k] = $parameter;
98+
$globalParameters->add($k, $parameter);
9899
continue;
99100
}
100101

@@ -141,7 +142,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
141142

142143
// Loop again and set default operations if none where found
143144
foreach ($resources as $index => $resource) {
144-
if ($globalParameters) {
145+
if (\count($globalParameters) > 0) {
145146
$resources[$index] = $resource = $this->mergeOperationParameters($resource, $globalParameters);
146147
}
147148

@@ -232,17 +233,23 @@ private function hasSameOperation(ApiResource $resource, string $operationClass,
232233
/**
233234
* @template T of Metadata
234235
*
235-
* @param Parameter[] $globalParameters
236-
* @param T $resource
236+
* @param T $resource
237237
*
238238
* @return T
239239
*/
240-
private function mergeOperationParameters(Metadata $resource, array $globalParameters): Metadata
240+
private function mergeOperationParameters(Metadata $resource, Parameters $globalParameters): Metadata
241241
{
242-
$parameters = $resource->getParameters() ?? [];
242+
$parameters = $resource->getParameters() ?? new Parameters();
243+
foreach ($globalParameters as $parameterName => $parameter) {
244+
if ($key = $parameter->getKey()) {
245+
$parameterName = $key;
246+
}
247+
248+
if (!$parameters->has($parameterName)) {
249+
$parameters->add($parameterName, $parameter);
250+
}
251+
}
243252

244-
return $resource->withParameters(
245-
(\is_array($parameters) ? $parameters : iterator_to_array($parameters)) + $globalParameters
246-
);
253+
return $resource->withParameters($parameters);
247254
}
248255
}

0 commit comments

Comments
 (0)