Skip to content

Commit 2ee23e5

Browse files
soyukavinceAmstoutz
authored andcommitted
fix: filter may not use FilterInterface (api-platform#6858)
* fix: filter may not use FilterInterface * add tests
1 parent 641ebd7 commit 2ee23e5

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ private function addFilterMetadata(Parameter $parameter): Parameter
152152
return $parameter;
153153
}
154154

155+
if (!\is_object($filterId) && !$this->filterLocator->has($filterId)) {
156+
return $parameter;
157+
}
158+
155159
$filter = \is_object($filterId) ? $filterId : $this->filterLocator->get($filterId);
156160

157161
if (!$filter) {
@@ -162,7 +166,7 @@ private function addFilterMetadata(Parameter $parameter): Parameter
162166
$parameter = $parameter->withSchema($schema);
163167
}
164168

165-
if (null === $parameter->getOpenApi() && $filter instanceof OpenApiParameterFilterInterface && ($openApiParameter = $filter->getOpenApiParameters($parameter)) && $openApiParameter instanceof OpenApiParameter) {
169+
if (null === $parameter->getOpenApi() && $filter instanceof OpenApiParameterFilterInterface && ($openApiParameter = $filter->getOpenApiParameters($parameter))) {
166170
$parameter = $parameter->withOpenApi($openApiParameter);
167171
}
168172

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
namespace ApiPlatform\Metadata\Tests\Resource\Factory;
1515

16+
use ApiPlatform\Metadata\ApiProperty;
1617
use ApiPlatform\Metadata\FilterInterface;
1718
use ApiPlatform\Metadata\Parameters;
1819
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
1920
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21+
use ApiPlatform\Metadata\Property\PropertyNameCollection;
2022
use ApiPlatform\Metadata\QueryParameter;
2123
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
2224
use ApiPlatform\Metadata\Resource\Factory\ParameterResourceMetadataCollectionFactory;
@@ -25,12 +27,14 @@
2527
use PHPUnit\Framework\TestCase;
2628
use Psr\Container\ContainerInterface;
2729

28-
class ParameterResourceMetadataCollectionFactoryTests extends TestCase
30+
class ParameterResourceMetadataCollectionFactoryTest extends TestCase
2931
{
3032
public function testParameterFactory(): void
3133
{
3234
$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
35+
$nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'hydra', 'everywhere']));
3336
$propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
37+
$propertyMetadata->method('create')->willReturnOnConsecutiveCalls(new ApiProperty(identifier: true), new ApiProperty(readable: true), new ApiProperty(readable: true));
3438
$filterLocator = $this->createStub(ContainerInterface::class);
3539
$filterLocator->method('has')->willReturn(true);
3640
$filterLocator->method('get')->willReturn(new class implements FilterInterface {
@@ -64,6 +68,24 @@ public function getDescription(string $resourceClass): array
6468
$this->assertEquals(['type' => 'foo'], $hydraParameter->getSchema());
6569
$this->assertEquals(new Parameter('test', 'query'), $hydraParameter->getOpenApi());
6670
$everywhere = $parameters->get('everywhere', QueryParameter::class);
67-
$this->assertEquals(new Parameter('everywhere', 'query', allowEmptyValue: true), $everywhere->getOpenApi());
71+
$this->assertNull($everywhere->getOpenApi());
72+
}
73+
74+
public function testParameterFactoryNoFilter(): void
75+
{
76+
$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
77+
$nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'hydra', 'everywhere']));
78+
$propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
79+
$propertyMetadata->method('create')->willReturnOnConsecutiveCalls(new ApiProperty(identifier: true), new ApiProperty(readable: true), new ApiProperty(readable: true));
80+
$filterLocator = $this->createStub(ContainerInterface::class);
81+
$filterLocator->method('has')->willReturn(false);
82+
$parameter = new ParameterResourceMetadataCollectionFactory(
83+
$nameCollection,
84+
$propertyMetadata,
85+
new AttributesResourceMetadataCollectionFactory(),
86+
$filterLocator
87+
);
88+
$operation = $parameter->create(WithParameter::class)->getOperation('collection');
89+
$this->assertInstanceOf(Parameters::class, $parameters = $operation->getParameters());
6890
}
6991
}

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,18 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
269269

270270
if (($f = $p->getFilter()) && \is_string($f) && $this->filterLocator && $this->filterLocator->has($f)) {
271271
$filter = $this->filterLocator->get($f);
272-
foreach ($filter->getDescription($entityClass) as $name => $description) {
273-
if ($prop = $p->getProperty()) {
274-
$name = str_replace($prop, $key, $name);
272+
273+
if ($d = $filter->getDescription($entityClass)) {
274+
foreach ($d as $name => $description) {
275+
if ($prop = $p->getProperty()) {
276+
$name = str_replace($prop, $key, $name);
277+
}
278+
279+
$openapiParameters[] = $this->getFilterParameter($name, $description, $operation->getShortName(), $f);
275280
}
276281

277-
$openapiParameters[] = $this->getFilterParameter($name, $description, $operation->getShortName(), $f);
282+
continue;
278283
}
279-
280-
continue;
281284
}
282285

283286
$in = $p instanceof HeaderParameterInterface ? 'header' : 'query';

0 commit comments

Comments
 (0)