Skip to content

Commit

Permalink
Merge e59bfe8 into a12ac48
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbarthelet committed Nov 16, 2019
2 parents a12ac48 + e59bfe8 commit c12d69f
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 60 deletions.
23 changes: 10 additions & 13 deletions src/Api/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ interface FilterInterface
* - required: if this filter is required
* - strategy (optional): 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',
* ]
* - schema (optional): additional parameters related to schema description
* e.g. 'schema' => [
* 'type' => 'string',
* 'enum' => [
* 'value1',
* 'value2',
* ],
* ]
* - description (optional): a string describing filter usage
* - swagger (optional/deprecated): additional parameters for the path operation
* - openapi (optional/deprecated): additional parameters for the path operation in the version 3
* The description can contain additional data specific to a filter.
*
* @see \ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer::getFiltersParameters
Expand Down
18 changes: 11 additions & 7 deletions src/Serializer/Filter/GroupFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ 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,
],
$description = [
'property' => null,
'type' => 'string',
'is_collection' => true,
'required' => false,
];

if ($this->whitelist) {
$description['schema'] = ['type' => 'array', 'items' => ['type' => 'string', 'enum' => $this->whitelist]];
}

return ["$this->parameterName[]" => $description];
}
}
19 changes: 1 addition & 18 deletions src/Serializer/Filter/PropertyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,7 @@ public function getDescription(string $resourceClass): array
'type' => 'string',
'is_collection' => true,
'required' => false,
'swagger' => [
'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: '.$example,
'name' => "$this->parameterName[]",
'type' => 'array',
'items' => [
'type' => 'string',
],
],
'openapi' => [
'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: '.$example,
'name' => "$this->parameterName[]",
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
],
],
],
'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: '.$example,
],
];
}
Expand Down
8 changes: 8 additions & 0 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,12 +706,19 @@ private function getFiltersParameters(bool $v3, string $resourceClass, string $o
'required' => $data['required'],
];

if (isset($data['description'])) {
$parameter['description'] = $data['description'];
}

$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'];
}
if (!$v3 && isset($data['schema']['items'])) {
$parameter['items'] = $data['schema']['items'];
}

if ('array' === ($type['type'] ?? '')) {
$deepObject = \in_array($data['type'], [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], true);
Expand All @@ -724,6 +731,7 @@ private function getFiltersParameters(bool $v3, string $resourceClass, string $o
}
}

/** @deprecated schema key should be used instead */
$key = $v3 ? 'openapi' : 'swagger';
if (isset($data[$key])) {
$parameter = $data[$key] + $parameter;
Expand Down
22 changes: 22 additions & 0 deletions tests/Serializer/Filter/GroupFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,26 @@ public function testGetDescription()

$this->assertEquals($expectedDescription, $groupFilter->getDescription(DummyGroup::class));
}

public function testGetDescriptionForWhitelistedGroups()
{
$groupFilter = new GroupFilter('whitlisted_groups', false, ['foo', 'bar']);
$expectedDescription = [
'whitlisted_groups[]' => [
'property' => null,
'type' => 'string',
'is_collection' => true,
'required' => false,
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => ['foo', 'bar'],
],
],
],
];

$this->assertEquals($expectedDescription, $groupFilter->getDescription(DummyGroup::class));
}
}
19 changes: 1 addition & 18 deletions tests/Serializer/Filter/PropertyFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,7 @@ public function testGetDescription()
'type' => 'string',
'is_collection' => true,
'required' => false,
'swagger' => [
'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: custom_properties[]={propertyName}&custom_properties[]={anotherPropertyName}&custom_properties[{nestedPropertyParent}][]={nestedProperty}',
'name' => 'custom_properties[]',
'type' => 'array',
'items' => [
'type' => 'string',
],
],
'openapi' => [
'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: custom_properties[]={propertyName}&custom_properties[]={anotherPropertyName}&custom_properties[{nestedPropertyParent}][]={nestedProperty}',
'name' => 'custom_properties[]',
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
],
],
],
'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: custom_properties[]={propertyName}&custom_properties[]={anotherPropertyName}&custom_properties[{nestedPropertyParent}][]={nestedProperty}',
],
];

Expand Down
52 changes: 50 additions & 2 deletions tests/Swagger/Serializer/DocumentationNormalizerV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1655,14 +1655,31 @@ public function testFilters(): void
'required' => true,
'strategy' => 'exact',
]]),
'f4' => new DummyFilter(['serializer' => [
'property' => 'group',
'type' => 'string',
'required' => false,
'strategy' => 'exact',
'is_collection' => true,
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => [
'group1',
'group2',
],
],
],
]]),
];

foreach ($filters as $filterId => $filter) {
$filterLocatorProphecy->has($filterId)->willReturn(true)->shouldBeCalled();
$filterLocatorProphecy->get($filterId)->willReturn($filter)->shouldBeCalled();
}

$filterLocatorProphecy->has('f4')->willReturn(false)->shouldBeCalled();
$filterLocatorProphecy->has('f5')->willReturn(false)->shouldBeCalled();

$this->normalizeWithFilters($filterLocatorProphecy->reveal());
}
Expand Down Expand Up @@ -1694,6 +1711,23 @@ public function testFiltersWithDeprecatedFilterCollection(): void
'required' => true,
'strategy' => 'exact',
]]),
'f4' => new DummyFilter(['serializer' => [
'property' => 'group',
'type' => 'string',
'required' => false,
'strategy' => 'exact',
'is_collection' => true,
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => [
'group1',
'group2',
],
],
],
]]),
]));
}

Expand Down Expand Up @@ -2051,7 +2085,7 @@ private function normalizeWithFilters($filterLocator): void
'This is a dummy.',
null,
[],
['get' => ['method' => 'GET', 'filters' => ['f1', 'f2', 'f3', 'f4']] + self::OPERATION_FORMATS]
['get' => ['method' => 'GET', 'filters' => ['f1', 'f2', 'f3', 'f4', 'f5']] + self::OPERATION_FORMATS]
);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);
Expand Down Expand Up @@ -2119,6 +2153,20 @@ private function normalizeWithFilters($filterLocator): void
],
'collectionFormat' => 'csv',
],
[
'name' => 'serializer',
'in' => 'query',
'required' => false,
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => [
'group1',
'group2',
],
],
'collectionFormat' => 'multi',
],
[
'name' => 'page',
'in' => 'query',
Expand Down
55 changes: 53 additions & 2 deletions tests/Swagger/Serializer/DocumentationNormalizerV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1512,14 +1512,31 @@ public function testFilters(): void
'enum' => ['asc', 'desc'],
],
]]),
'f5' => new DummyFilter(['serializer' => [
'property' => 'group',
'type' => 'string',
'required' => false,
'strategy' => 'exact',
'is_collection' => true,
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => [
'group1',
'group2',
],
],
],
]]),
];

foreach ($filters as $filterId => $filter) {
$filterLocatorProphecy->has($filterId)->willReturn(true)->shouldBeCalled();
$filterLocatorProphecy->get($filterId)->willReturn($filter)->shouldBeCalled();
}

$filterLocatorProphecy->has('f5')->willReturn(false)->shouldBeCalled();
$filterLocatorProphecy->has('f6')->willReturn(false)->shouldBeCalled();

$this->normalizeWithFilters($filterLocatorProphecy->reveal());
}
Expand Down Expand Up @@ -1560,6 +1577,23 @@ public function testFiltersWithDeprecatedFilterCollection(): void
'enum' => ['asc', 'desc'],
],
]]),
'f5' => new DummyFilter(['serializer' => [
'property' => 'group',
'type' => 'string',
'required' => false,
'strategy' => 'exact',
'is_collection' => true,
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => [
'group1',
'group2',
],
],
],
]]),
]));
}

Expand Down Expand Up @@ -2031,7 +2065,7 @@ private function normalizeWithFilters($filterLocator): void
'This is a dummy.',
null,
[],
['get' => ['method' => 'GET', 'filters' => ['f1', 'f2', 'f3', 'f4', 'f5']] + self::OPERATION_FORMATS]
['get' => ['method' => 'GET', 'filters' => ['f1', 'f2', 'f3', 'f4', 'f5', 'f6']] + self::OPERATION_FORMATS]
);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);
Expand Down Expand Up @@ -2130,6 +2164,23 @@ private function normalizeWithFilters($filterLocator): void
'enum' => ['asc', 'desc'],
],
],
[
'name' => 'serializer',
'in' => 'query',
'required' => false,
'schema' => [
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => [
'group1',
'group2',
],
],
],
'style' => 'form',
'explode' => true,
],
[
'name' => 'page',
'in' => 'query',
Expand Down

0 comments on commit c12d69f

Please sign in to comment.