From 33fa51ae1cdb21af32a2d3fc98c4374e3ff30eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Barthelet?= Date: Thu, 22 Aug 2019 15:10:49 +0200 Subject: [PATCH] Implement FilterDescription object --- src/Api/FilterDescription.php | 71 +++++++++++++++++++ src/Api/FilterInterface.php | 24 +------ .../Common/Filter/OrderFilterTrait.php | 29 +++++--- src/Serializer/Filter/GroupFilter.php | 11 +-- .../Serializer/DocumentationNormalizer.php | 21 ++---- 5 files changed, 99 insertions(+), 57 deletions(-) create mode 100644 src/Api/FilterDescription.php diff --git a/src/Api/FilterDescription.php b/src/Api/FilterDescription.php new file mode 100644 index 00000000000..5172ef19d65 --- /dev/null +++ b/src/Api/FilterDescription.php @@ -0,0 +1,71 @@ + + * + * 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\Core\Api; + +class FilterDescription +{ + private $name; + private $property; + private $type; + private $required; + private $strategy; + private $collection; + private $schema; + + public function __construct(string $name, ?string $property, string $type, bool $required, ?string $strategy, ?bool $collection = false, ?array $schema = null) + { + $this->name = $name; + $this->property = $property; + $this->type = $type; + $this->required = $required; + $this->strategy = $strategy; + $this->collection = $collection; + $this->schema = $schema; + } + + public function getName(): string + { + return $this->name; + } + + public function getProperty(): ?string + { + return $this->property; + } + + public function getType(): string + { + return $this->type; + } + + public function isRequired(): bool + { + return $this->required; + } + + public function getStrategy(): ?string + { + return $this->strategy; + } + + public function isCollection(): bool + { + return $this->collection; + } + + public function getSchema(): array + { + return $this->schema; + } +} diff --git a/src/Api/FilterInterface.php b/src/Api/FilterInterface.php index 970ebdaf940..6bea95f23d7 100644 --- a/src/Api/FilterInterface.php +++ b/src/Api/FilterInterface.php @@ -23,29 +23,7 @@ interface FilterInterface /** * Gets the description of this filter for the given resource. * - * Returns an array with the filter parameter names as keys and array with the following data as values: - * - property: the property where the filter is applied - * - type: the type of the filter - * - required: if this filter is required - * - strategy: the used strategy - * - is_collection (optional): is this filter is collection - * - swagger (optional): additional parameters for the path operation, - * e.g. 'swagger' => [ - * 'description' => 'My Description', - * 'name' => 'My Name', - * 'type' => 'integer', - * ] - * - openapi (optional): additional parameters for the path operation in the version 3 spec, - * e.g. 'openapi' => [ - * 'description' => 'My Description', - * 'name' => 'My Name', - * 'schema' => [ - * 'type' => 'integer', - * ] - * ] - * The description can contain additional data specific to a filter. - * - * @see \ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer::getFiltersParameters + * @return FilterDescription[] */ public function getDescription(string $resourceClass): array; } diff --git a/src/Bridge/Doctrine/Common/Filter/OrderFilterTrait.php b/src/Bridge/Doctrine/Common/Filter/OrderFilterTrait.php index 207f36e5e1c..30d71979cfd 100644 --- a/src/Bridge/Doctrine/Common/Filter/OrderFilterTrait.php +++ b/src/Bridge/Doctrine/Common/Filter/OrderFilterTrait.php @@ -13,7 +13,9 @@ namespace ApiPlatform\Core\Bridge\Doctrine\Common\Filter; +use ApiPlatform\Core\Api\FilterDescription; use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait; +use Symfony\Component\PropertyInfo\Type; /** * Trait for ordering the collection by given properties. @@ -48,18 +50,23 @@ public function getDescription(string $resourceClass): array continue; } $propertyName = $this->normalizePropertyName($property); - $description[sprintf('%s[%s]', $this->orderParameterName, $propertyName)] = [ - 'property' => $propertyName, - 'type' => 'string', - 'required' => false, - 'schema' => [ - 'type' => 'string', - 'enum' => [ - strtolower(OrderFilterInterface::DIRECTION_ASC), - strtolower(OrderFilterInterface::DIRECTION_DESC), + $description[] = new FilterDescription( + sprintf('%s[%s]', $this->orderParameterName, $propertyName), + $propertyName, + Type::BUILTIN_TYPE_STRING, + false, + null, + false, + [ + 'schema' => [ + 'type' => 'string', + 'enum' => [ + strtolower(OrderFilterInterface::DIRECTION_ASC), + strtolower(OrderFilterInterface::DIRECTION_DESC), + ], ], - ], - ]; + ] + ); } return $description; diff --git a/src/Serializer/Filter/GroupFilter.php b/src/Serializer/Filter/GroupFilter.php index 5d504a6c17b..346313eda9b 100644 --- a/src/Serializer/Filter/GroupFilter.php +++ b/src/Serializer/Filter/GroupFilter.php @@ -13,7 +13,9 @@ namespace ApiPlatform\Core\Serializer\Filter; +use ApiPlatform\Core\Api\FilterDescription; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; /** @@ -65,13 +67,6 @@ public function apply(Request $request, bool $normalization, array $attributes, */ public function getDescription(string $resourceClass): array { - return [ - "$this->parameterName[]" => [ - 'property' => null, - 'type' => 'string', - 'is_collection' => true, - 'required' => false, - ], - ]; + return [new FilterDescription("$this->parameterName[]", null, Type::BUILTIN_TYPE_STRING, false, null, true)]; } } diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 3cb9f5b199b..1d99710407a 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -680,22 +680,18 @@ private function getFiltersParameters(bool $v3, string $resourceClass, string $o continue; } - foreach ($filter->getDescription($resourceClass) as $name => $data) { + foreach ($filter->getDescription($resourceClass) as $filterDesciption) { $parameter = [ - 'name' => $name, + 'name' => $filterDesciption->getName(), 'in' => 'query', - 'required' => $data['required'], + 'required' => $filterDesciption->isRequired(), ]; - $type = \in_array($data['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : ['type' => 'string']; - $v3 ? $parameter['schema'] = $type : $parameter += $type; - - if ($v3 && isset($data['schema'])) { - $parameter['schema'] = $data['schema']; - } + $type = \in_array($filterDesciption->getType(), Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($filterDesciption->getType(), false, null, $filterDesciption->isCollection() ?? false)) : ['type' => 'string']; + $v3 ? ($filterDescription->getSchema() ? $parameter['schema'] = $filterDescription->getSchema() : $parameter['schema'] = $type) : $parameter += $type; if ('array' === ($type['type'] ?? '')) { - $deepObject = \in_array($data['type'], [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], true); + $deepObject = \in_array($filterDesciption->getType(), [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], true); if ($v3) { $parameter['style'] = $deepObject ? 'deepObject' : 'form'; @@ -705,11 +701,6 @@ private function getFiltersParameters(bool $v3, string $resourceClass, string $o } } - $key = $v3 ? 'openapi' : 'swagger'; - if (isset($data[$key])) { - $parameter = $data[$key] + $parameter; - } - $parameters[] = $parameter; } }