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
71 changes: 71 additions & 0 deletions src/Api/FilterDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\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;
}
}
24 changes: 1 addition & 23 deletions src/Api/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
29 changes: 18 additions & 11 deletions src/Bridge/Doctrine/Common/Filter/OrderFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 3 additions & 8 deletions src/Serializer/Filter/GroupFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)];
}
}
21 changes: 6 additions & 15 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}
}
Expand Down