Skip to content

Commit fd737d3

Browse files
committed
refactor(openapi): parameter's filter description
1 parent 34604f4 commit fd737d3

File tree

1 file changed

+83
-64
lines changed

1 file changed

+83
-64
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 83 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Metadata\Exception\RuntimeException;
2828
use ApiPlatform\Metadata\HeaderParameterInterface;
2929
use ApiPlatform\Metadata\HttpOperation;
30+
use ApiPlatform\Metadata\Parameter as ApiPlatformParameter;
3031
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
3132
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
3233
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -331,12 +332,20 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
331332
}
332333
}
333334

335+
$entityClass = $this->getFilterClass($operation);
334336
$openapiParameters = $openapiOperation->getParameters();
335337
foreach ($operation->getParameters() ?? [] as $key => $p) {
336338
if (false === $p->getOpenApi()) {
337339
continue;
338340
}
339341

342+
if (($f = $p->getFilter()) && $this->filterLocator->has($f)) {
343+
$filter = $this->filterLocator->get($f);
344+
foreach ($filter->getDescription($entityClass) as $name => $description) {
345+
$parameters[] = $this->getFilterParameter($name, $description, $operation->getShortName());
346+
}
347+
}
348+
340349
$in = $p instanceof HeaderParameterInterface ? 'header' : 'query';
341350
$parameter = new Parameter($key, $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']);
342351

@@ -654,92 +663,102 @@ private function getLinks(ResourceMetadataCollection $resourceMetadataCollection
654663
private function getFiltersParameters(CollectionOperationInterface|HttpOperation $operation): array
655664
{
656665
$parameters = [];
657-
658666
$resourceFilters = $operation->getFilters();
667+
$entityClass = $this->getFilterClass($operation);
668+
659669
foreach ($resourceFilters ?? [] as $filterId) {
660670
if (!$this->filterLocator->has($filterId)) {
661671
continue;
662672
}
663673

664674
$filter = $this->filterLocator->get($filterId);
665-
$entityClass = $operation->getClass();
666-
if ($options = $operation->getStateOptions()) {
667-
if ($options instanceof DoctrineOptions && $options->getEntityClass()) {
668-
$entityClass = $options->getEntityClass();
669-
}
670-
671-
if ($options instanceof DoctrineODMOptions && $options->getDocumentClass()) {
672-
$entityClass = $options->getDocumentClass();
673-
}
675+
foreach ($filter->getDescription($entityClass) as $name => $description) {
676+
$parameters[] = $this->getFilterParameter($name, $description, $operation->getShortName());
674677
}
678+
}
675679

676-
foreach ($filter->getDescription($entityClass) as $name => $data) {
677-
if (isset($data['swagger'])) {
678-
trigger_deprecation('api-platform/core', '4.0', \sprintf('Using the "swagger" field of the %s::getDescription() (%s) is deprecated.', $filter::class, $operation->getShortName()));
679-
}
680+
return $parameters;
681+
}
680682

681-
if (!isset($data['openapi']) || $data['openapi'] instanceof Parameter) {
682-
$schema = $data['schema'] ?? [];
683+
private function getFilterClass(HttpOperation $operation): string
684+
{
685+
$entityClass = $operation->getClass();
686+
if ($options = $operation->getStateOptions()) {
687+
if ($options instanceof DoctrineOptions && $options->getEntityClass()) {
688+
return $options->getEntityClass();
689+
}
683690

684-
if (isset($data['type']) && \in_array($data['type'] ?? null, Type::$builtinTypes, true) && !isset($schema['type'])) {
685-
$schema += $this->jsonSchemaTypeFactory ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : $this->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false));
686-
}
691+
if ($options instanceof DoctrineODMOptions && $options->getDocumentClass()) {
692+
return $options->getDocumentClass();
693+
}
694+
}
687695

688-
if (!isset($schema['type'])) {
689-
$schema['type'] = 'string';
690-
}
696+
return $entityClass;
697+
}
691698

692-
$style = 'array' === ($schema['type'] ?? null) && \in_array(
693-
$data['type'],
694-
[Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT],
695-
true
696-
) ? 'deepObject' : 'form';
699+
private function getFilterParameter(string $name, array $description, string $shortName): ApiPlatformParameter
700+
{
701+
if (isset($description['swagger'])) {
702+
trigger_deprecation('api-platform/core', '4.0', \sprintf('Using the "swagger" field of the %s::getDescription() (%s) is deprecated.', $filter::class, $shortName));
703+
}
697704

698-
$parameter = isset($data['openapi']) && $data['openapi'] instanceof Parameter ? $data['openapi'] : new Parameter(in: 'query', name: $name, style: $style, explode: $data['is_collection'] ?? false);
705+
if (!isset($description['openapi']) || $description['openapi'] instanceof Parameter) {
706+
$schema = $description['schema'] ?? [];
699707

700-
if ('' === $parameter->getDescription() && ($description = $data['description'] ?? '')) {
701-
$parameter = $parameter->withDescription($description);
702-
}
708+
if (isset($description['type']) && \in_array($description['type'] ?? null, Type::$builtinTypes, true) && !isset($schema['type'])) {
709+
$schema += $this->jsonSchemaTypeFactory ? $this->jsonSchemaTypeFactory->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false)) : $this->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false));
710+
}
703711

704-
if (false === $parameter->getRequired() && false !== ($required = $data['required'] ?? false)) {
705-
$parameter = $parameter->withRequired($required);
706-
}
712+
if (!isset($schema['type'])) {
713+
$schema['type'] = 'string';
714+
}
707715

708-
$parameters[] = $parameter->withSchema($schema);
709-
continue;
710-
}
716+
$style = 'array' === ($schema['type'] ?? null) && \in_array(
717+
$description['type'],
718+
[Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT],
719+
true
720+
) ? 'deepObject' : 'form';
711721

712-
trigger_deprecation('api-platform/core', '4.0', \sprintf('Not using "%s" on the "openapi" field of the %s::getDescription() (%s) is deprecated.', Parameter::class, $filter::class, $operation->getShortName()));
713-
if ($this->jsonSchemaTypeFactory) {
714-
$schema = $data['schema'] ?? (\in_array($data['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false), 'openapi') : ['type' => 'string']);
715-
} else {
716-
$schema = $data['schema'] ?? (\in_array($data['type'], Type::$builtinTypes, true) ? $this->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : ['type' => 'string']);
717-
}
722+
$parameter = isset($description['openapi']) && $description['openapi'] instanceof Parameter ? $description['openapi'] : new Parameter(in: 'query', name: $name, style: $style, explode: $description['is_collection'] ?? false);
718723

719-
$parameters[] = new Parameter(
720-
$name,
721-
'query',
722-
$data['description'] ?? '',
723-
$data['required'] ?? false,
724-
$data['openapi']['deprecated'] ?? false,
725-
$data['openapi']['allowEmptyValue'] ?? true,
726-
$schema,
727-
'array' === $schema['type'] && \in_array(
728-
$data['type'],
729-
[Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT],
730-
true
731-
) ? 'deepObject' : 'form',
732-
$data['openapi']['explode'] ?? ('array' === $schema['type']),
733-
$data['openapi']['allowReserved'] ?? false,
734-
$data['openapi']['example'] ?? null,
735-
isset(
736-
$data['openapi']['examples']
737-
) ? new \ArrayObject($data['openapi']['examples']) : null
738-
);
724+
if ('' === $parameter->getDescription() && ($description = $description['description'] ?? '')) {
725+
$parameter = $parameter->withDescription($description);
739726
}
727+
728+
if (false === $parameter->getRequired() && false !== ($required = $description['required'] ?? false)) {
729+
$parameter = $parameter->withRequired($required);
730+
}
731+
732+
return $parameter->withSchema($schema);
740733
}
741734

742-
return $parameters;
735+
trigger_deprecation('api-platform/core', '4.0', \sprintf('Not using "%s" on the "openapi" field of the %s::getDescription() (%s) is deprecated.', Parameter::class, $filter::class, $shortName));
736+
if ($this->jsonSchemaTypeFactory) {
737+
$schema = $description['schema'] ?? (\in_array($description['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false), 'openapi') : ['type' => 'string']);
738+
} else {
739+
$schema = $description['schema'] ?? (\in_array($description['type'], Type::$builtinTypes, true) ? $this->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false)) : ['type' => 'string']);
740+
}
741+
742+
return new Parameter(
743+
$name,
744+
'query',
745+
$description['description'] ?? '',
746+
$description['required'] ?? false,
747+
$description['openapi']['deprecated'] ?? false,
748+
$description['openapi']['allowEmptyValue'] ?? true,
749+
$schema,
750+
'array' === $schema['type'] && \in_array(
751+
$description['type'],
752+
[Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT],
753+
true
754+
) ? 'deepObject' : 'form',
755+
$description['openapi']['explode'] ?? ('array' === $schema['type']),
756+
$description['openapi']['allowReserved'] ?? false,
757+
$description['openapi']['example'] ?? null,
758+
isset(
759+
$description['openapi']['examples']
760+
) ? new \ArrayObject($description['openapi']['examples']) : null
761+
);
743762
}
744763

745764
private function getPaginationParameters(CollectionOperationInterface|HttpOperation $operation): array

0 commit comments

Comments
 (0)