Skip to content

Commit 3ad3836

Browse files
authored
feat(metadata): attribute Parameter (#6246)
1 parent 3cbef6a commit 3ad3836

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1004
-43
lines changed

src/Metadata/ApiFilter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ final class ApiFilter
2626
{
2727
/**
2828
* @param string|class-string<FilterInterface>|class-string<LegacyFilterInterface> $filterClass
29+
* @param string $alias a filter tag alias to be referenced in a Parameter
2930
*/
3031
public function __construct(
3132
public string $filterClass,
3233
public ?string $id = null,
3334
public ?string $strategy = null,
3435
public array $properties = [],
3536
public array $arguments = [],
37+
public ?string $alias = null,
3638
) {
3739
if (!is_a($this->filterClass, FilterInterface::class, true) && !is_a($this->filterClass, LegacyFilterInterface::class, true)) {
3840
throw new InvalidArgumentException(sprintf('The filter class "%s" does not implement "%s". Did you forget a use statement?', $this->filterClass, FilterInterface::class));

src/Metadata/ApiResource.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ public function __construct(
960960
$provider = null,
961961
$processor = null,
962962
protected ?OptionsInterface $stateOptions = null,
963+
protected array|Parameters|null $parameters = null,
963964
protected array $extraProperties = [],
964965
) {
965966
parent::__construct(
@@ -1000,6 +1001,7 @@ class: $class,
10001001
provider: $provider,
10011002
processor: $processor,
10021003
stateOptions: $stateOptions,
1004+
parameters: $parameters,
10031005
extraProperties: $extraProperties
10041006
);
10051007

src/Metadata/Delete.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function __construct(
9494
$provider = null,
9595
$processor = null,
9696
?OptionsInterface $stateOptions = null,
97+
array|Parameters|null $parameters = null,
9798
array $extraProperties = [],
9899
) {
99100
parent::__construct(
@@ -170,6 +171,7 @@ class: $class,
170171
processor: $processor,
171172
extraProperties: $extraProperties,
172173
collectDenormalizationErrors: $collectDenormalizationErrors,
174+
parameters: $parameters,
173175
stateOptions: $stateOptions,
174176
);
175177
}

src/Metadata/Extractor/XmlResourceExtractor.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
1717
use ApiPlatform\Metadata\GetCollection;
18+
use ApiPlatform\Metadata\HeaderParameter;
1819
use ApiPlatform\Metadata\Post;
20+
use ApiPlatform\Metadata\QueryParameter;
1921
use ApiPlatform\Metadata\Tests\Fixtures\StateOptions;
2022
use ApiPlatform\OpenApi\Model\ExternalDocumentation;
2123
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
22-
use ApiPlatform\OpenApi\Model\Parameter;
24+
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
2325
use ApiPlatform\OpenApi\Model\RequestBody;
2426
use ApiPlatform\State\OptionsInterface;
2527
use Symfony\Component\Config\Util\XmlUtils;
@@ -97,6 +99,7 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
9799
'stateOptions' => $this->buildStateOptions($resource),
98100
'links' => $this->buildLinks($resource),
99101
'headers' => $this->buildHeaders($resource),
102+
'parameters' => $this->buildParameters($resource),
100103
]);
101104
}
102105

@@ -200,7 +203,7 @@ private function buildOpenapi(\SimpleXMLElement $resource): bool|OpenApiOperatio
200203

201204
if (isset($openapi->parameters->parameter)) {
202205
foreach ($openapi->parameters->parameter as $parameter) {
203-
$data['parameters'][(string) $parameter->attributes()->name] = new Parameter(
206+
$data['parameters'][(string) $parameter->attributes()->name] = new OpenApiParameter(
204207
name: $this->phpize($parameter, 'name', 'string'),
205208
in: $this->phpize($parameter, 'in', 'string'),
206209
description: $this->phpize($parameter, 'description', 'string'),
@@ -494,4 +497,48 @@ private function buildHeaders(\SimpleXMLElement $resource): ?array
494497

495498
return $headers;
496499
}
500+
501+
/**
502+
* @return array<string, \ApiPlatform\Metadata\Parameter>
503+
*/
504+
private function buildParameters(\SimpleXMLElement $resource): ?array
505+
{
506+
if (!$resource->parameters) {
507+
return null;
508+
}
509+
510+
$parameters = [];
511+
foreach ($resource->parameters->parameter as $parameter) {
512+
$key = (string) $parameter->attributes()->key;
513+
$cl = ('header' === (string) $parameter->attributes()->in) ? HeaderParameter::class : QueryParameter::class;
514+
$parameters[$key] = new $cl(
515+
key: $key,
516+
required: $this->phpize($parameter, 'required', 'bool'),
517+
schema: isset($parameter->schema->values) ? $this->buildValues($parameter->schema->values) : null,
518+
openApi: isset($parameter->openapi) ? new OpenApiParameter(
519+
name: $this->phpize($parameter->openapi, 'name', 'string'),
520+
in: $this->phpize($parameter->openapi, 'in', 'string'),
521+
description: $this->phpize($parameter->openapi, 'description', 'string'),
522+
required: $this->phpize($parameter->openapi, 'required', 'bool'),
523+
deprecated: $this->phpize($parameter->openapi, 'deprecated', 'bool'),
524+
allowEmptyValue: $this->phpize($parameter->openapi, 'allowEmptyValue', 'bool'),
525+
schema: isset($parameter->openapi->schema->values) ? $this->buildValues($parameter->openapi->schema->values) : null,
526+
style: $this->phpize($parameter->openapi, 'style', 'string'),
527+
explode: $this->phpize($parameter->openapi, 'explode', 'bool'),
528+
allowReserved: $this->phpize($parameter->openapi, 'allowReserved', 'bool'),
529+
example: $this->phpize($parameter->openapi, 'example', 'string'),
530+
examples: isset($parameter->openapi->examples->values) ? new \ArrayObject($this->buildValues($parameter->openapi->examples->values)) : null,
531+
content: isset($parameter->openapi->content->values) ? new \ArrayObject($this->buildValues($parameter->openapi->content->values)) : null,
532+
) : null,
533+
provider: $this->phpize($parameter, 'provider', 'string'),
534+
filter: $this->phpize($parameter, 'filter', 'string'),
535+
property: $this->phpize($parameter, 'property', 'string'),
536+
description: $this->phpize($parameter, 'description', 'string'),
537+
priority: $this->phpize($parameter, 'priority', 'integer'),
538+
extraProperties: $this->buildExtraProperties($parameter, 'extraProperties') ?? [],
539+
);
540+
}
541+
542+
return $parameters;
543+
}
497544
}

src/Metadata/Extractor/YamlResourceExtractor.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
1717
use ApiPlatform\Metadata\GetCollection;
18+
use ApiPlatform\Metadata\HeaderParameter;
1819
use ApiPlatform\Metadata\Post;
20+
use ApiPlatform\Metadata\QueryParameter;
1921
use ApiPlatform\Metadata\Tests\Fixtures\StateOptions;
2022
use ApiPlatform\OpenApi\Model\ExternalDocumentation;
2123
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
@@ -124,6 +126,7 @@ private function buildExtendedBase(array $resource): array
124126
'stateOptions' => $this->buildStateOptions($resource),
125127
'links' => $this->buildLinks($resource),
126128
'headers' => $this->buildHeaders($resource),
129+
'parameters' => $this->buildParameters($resource),
127130
]);
128131
}
129132

@@ -450,4 +453,47 @@ private function buildHeaders(array $resource): ?array
450453

451454
return $headers;
452455
}
456+
457+
/**
458+
* @return array<string, \ApiPlatform\Metadata\Parameter>
459+
*/
460+
private function buildParameters(array $resource): ?array
461+
{
462+
if (!isset($resource['parameters']) || !\is_array($resource['parameters'])) {
463+
return null;
464+
}
465+
466+
$parameters = [];
467+
foreach ($resource['parameters'] as $key => $parameter) {
468+
$cl = ($parameter['in'] ?? 'query') === 'header' ? HeaderParameter::class : QueryParameter::class;
469+
$parameters[$key] = new $cl(
470+
key: $key,
471+
required: $this->phpize($parameter, 'required', 'bool'),
472+
schema: $parameter['schema'],
473+
openApi: ($parameter['openapi'] ?? null) ? new Parameter(
474+
name: $parameter['openapi']['name'],
475+
in: $parameter['in'] ?? 'query',
476+
description: $parameter['openapi']['description'] ?? '',
477+
required: $parameter['openapi']['required'] ?? $parameter['required'] ?? false,
478+
deprecated: $parameter['openapi']['deprecated'] ?? false,
479+
allowEmptyValue: $parameter['openapi']['allowEmptyValue'] ?? false,
480+
schema: $parameter['openapi']['schema'] ?? $parameter['schema'] ?? [],
481+
style: $parameter['openapi']['style'] ?? null,
482+
explode: $parameter['openapi']['explode'] ?? false,
483+
allowReserved: $parameter['openapi']['allowReserved '] ?? false,
484+
example: $parameter['openapi']['example'] ?? null,
485+
examples: isset($parameter['openapi']['examples']) ? new \ArrayObject($parameter['openapi']['examples']) : null,
486+
content: isset($parameter['openapi']['content']) ? new \ArrayObject($parameter['openapi']['content']) : null
487+
) : null,
488+
provider: $this->phpize($parameter, 'provider', 'string'),
489+
filter: $this->phpize($parameter, 'filter', 'string'),
490+
property: $this->phpize($parameter, 'property', 'string'),
491+
description: $this->phpize($parameter, 'description', 'string'),
492+
priority: $this->phpize($parameter, 'priority', 'integer'),
493+
extraProperties: $this->buildArrayValue($parameter, 'extraProperties') ?? [],
494+
);
495+
}
496+
497+
return $parameters;
498+
}
453499
}

src/Metadata/Extractor/schema/resources.xsd

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,29 @@
433433
</xsd:sequence>
434434
</xsd:group>
435435

436+
<xsd:element name="parameter">
437+
<xsd:complexType>
438+
<xsd:sequence maxOccurs="unbounded">
439+
<xsd:element name="openApi" minOccurs="0" type="openApiOperation"/>
440+
<xsd:element name="schema" minOccurs="0" type="sequenceWithValues"/>
441+
<xsd:element name="extraProperties" minOccurs="0" type="sequenceWithValues"/>
442+
</xsd:sequence>
443+
<xsd:attribute type="xsd:string" name="key" use="required"/>
444+
<xsd:attribute type="xsd:string" name="in"/>
445+
<xsd:attribute type="xsd:string" name="provider"/>
446+
<xsd:attribute type="xsd:string" name="filter"/>
447+
<xsd:attribute type="xsd:string" name="property"/>
448+
<xsd:attribute type="xsd:string" name="description"/>
449+
<xsd:attribute type="xsd:boolean" name="required"/>
450+
</xsd:complexType>
451+
</xsd:element>
452+
453+
<xsd:complexType name="parameters">
454+
<xsd:sequence maxOccurs="unbounded">
455+
<xsd:element ref="parameter"/>
456+
</xsd:sequence>
457+
</xsd:complexType>
458+
436459
<xsd:group name="extendedBase">
437460
<xsd:sequence>
438461
<xsd:group ref="base"/>
@@ -444,6 +467,7 @@
444467
<xsd:element name="inputFormats" minOccurs="0" type="formats"/>
445468
<xsd:element name="openapiContext" minOccurs="0" type="sequenceWithValues"/> <!-- TODO Remove in 4.0 -->
446469
<xsd:element name="openapi" minOccurs="0" type="openApiOperation"/>
470+
<xsd:element name="parameters" minOccurs="0" type="parameters"/>
447471
<xsd:element name="options" minOccurs="0" type="sequenceWithValues"/>
448472
<xsd:element name="outputFormats" minOccurs="0" type="formats"/>
449473
<xsd:element name="paginationViaCursor" minOccurs="0" type="paginationViaCursor"/>

src/Metadata/FilterInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ interface FilterInterface
6363
* The description can contain additional data specific to a filter.
6464
*
6565
* @see \ApiPlatform\OpenApi\Factory\OpenApiFactory::getFiltersParameters
66+
*
67+
* @return array<string, array{property: string, type: string, required: bool, strategy: string, is_collection: bool, openapi: array<string, mixed>, schema: array<string, mixed>}>
6668
*/
6769
public function getDescription(string $resourceClass): array;
6870
}

src/Metadata/Get.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function __construct(
9494
$provider = null,
9595
$processor = null,
9696
?OptionsInterface $stateOptions = null,
97+
array|Parameters|null $parameters = null,
9798
array $extraProperties = [],
9899
) {
99100
parent::__construct(
@@ -169,6 +170,7 @@ class: $class,
169170
provider: $provider,
170171
processor: $processor,
171172
stateOptions: $stateOptions,
173+
parameters: $parameters,
172174
extraProperties: $extraProperties,
173175
);
174176
}

src/Metadata/GetCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function __construct(
9494
$provider = null,
9595
$processor = null,
9696
?OptionsInterface $stateOptions = null,
97+
array|Parameters|null $parameters = null,
9798
array $extraProperties = [],
9899
private ?string $itemUriTemplate = null,
99100
) {
@@ -169,6 +170,7 @@ class: $class,
169170
name: $name,
170171
provider: $provider,
171172
processor: $processor,
173+
parameters: $parameters,
172174
extraProperties: $extraProperties,
173175
stateOptions: $stateOptions,
174176
);

src/Metadata/GraphQl/Operation.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Metadata\Link;
1717
use ApiPlatform\Metadata\Operation as AbstractOperation;
18+
use ApiPlatform\Metadata\Parameters;
1819
use ApiPlatform\State\OptionsInterface;
1920

2021
class Operation extends AbstractOperation
@@ -84,6 +85,7 @@ public function __construct(
8485
$provider = null,
8586
$processor = null,
8687
?OptionsInterface $stateOptions = null,
88+
array|Parameters|null $parameters = null,
8789
array $extraProperties = []
8890
) {
8991
parent::__construct(
@@ -131,6 +133,7 @@ class: $class,
131133
provider: $provider,
132134
processor: $processor,
133135
stateOptions: $stateOptions,
136+
parameters: $parameters,
134137
extraProperties: $extraProperties
135138
);
136139
}

0 commit comments

Comments
 (0)