Skip to content

Commit d8f6ea2

Browse files
committed
Only use attributes and decouple from Symfony\Component\HttpFoundation\Request
1 parent dbb819e commit d8f6ea2

File tree

9 files changed

+23
-40
lines changed

9 files changed

+23
-40
lines changed

src/Api/FormatsProvider.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
use ApiPlatform\Core\Exception\InvalidArgumentException;
1717
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18-
use ApiPlatform\Core\Util\RequestAttributesExtractor;
19-
use Symfony\Component\HttpFoundation\Request;
2018

2119
/**
2220
* {@inheritdoc}
@@ -39,35 +37,25 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
3937
/**
4038
* Finds formats from a Resource class.
4139
*/
42-
public function fromRequest(Request $request): array
40+
public function getFormatsFromAttributes(array $attributes): array
4341
{
44-
if ($this->formats) {
45-
return $this->formats;
46-
}
47-
48-
if (!$attributes = RequestAttributesExtractor::extractAttributes($request)) {
49-
$this->formats = $this->configuredFormats;
50-
51-
return $this->formats;
42+
if (!$attributes) {
43+
return $this->configuredFormats;
5244
}
5345

5446
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
5547

5648
if (!$formats = (array) $resourceMetadata->getOperationAttribute($attributes, 'formats', [], true)) {
57-
$this->formats = $this->configuredFormats;
58-
59-
return $this->formats;
49+
return $this->configuredFormats;
6050
}
6151

62-
$this->populateFormats($formats);
63-
64-
return $this->formats;
52+
return $this->getOperationFormats($formats);
6553
}
6654

6755
/**
6856
* Filter and populate the acceptable formats.
6957
*/
70-
private function populateFormats(array $annotationFormats)
58+
private function getOperationFormats(array $annotationFormats): array
7159
{
7260
$resourceFormats = [];
7361
foreach ($annotationFormats as $format => $value) {
@@ -82,6 +70,6 @@ private function populateFormats(array $annotationFormats)
8270
throw new InvalidArgumentException(sprintf("You either need to add the format '%s' to your project configuration or declare a mime type for it in your annotation.", $value));
8371
}
8472

85-
$this->formats = $resourceFormats;
73+
return $resourceFormats;
8674
}
8775
}

src/Api/FormatsProviderInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace ApiPlatform\Core\Api;
1515

16-
use Symfony\Component\HttpFoundation\Request;
17-
1816
/**
1917
* Extracts formats for a given operation according to the retrieved Metadata.
2018
*
@@ -25,5 +23,5 @@ interface FormatsProviderInterface
2523
/**
2624
* Finds formats for an operation.
2725
*/
28-
public function fromRequest(Request $request): array;
26+
public function getFormatsFromAttributes(array $attributes): array;
2927
}

src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Core\Exception\RuntimeException;
1919
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2020
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
21+
use ApiPlatform\Core\Util\RequestAttributesExtractor;
2122
use Symfony\Component\HttpFoundation\Request;
2223
use Symfony\Component\HttpFoundation\Response;
2324
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -74,7 +75,7 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName
7475
public function __invoke(Request $request)
7576
{
7677
if (null !== $this->formatsProvider) {
77-
$this->formats = $this->formatsProvider->fromRequest($request);
78+
$this->formats = $this->formatsProvider->getFormatsFromAttributes(RequestAttributesExtractor::extractAttributes($request));
7879
}
7980

8081
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version, $this->formats);

src/Documentation/Action/DocumentationAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Api\FormatsProviderInterface;
1717
use ApiPlatform\Core\Documentation\Documentation;
1818
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
19+
use ApiPlatform\Core\Util\RequestAttributesExtractor;
1920
use Symfony\Component\HttpFoundation\Request;
2021

2122
/**
@@ -52,7 +53,7 @@ public function __invoke(Request $request = null): Documentation
5253
$request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
5354

5455
if (null !== $this->formatsProvider) {
55-
$this->formats = $this->formatsProvider->fromRequest($request);
56+
$this->formats = $this->formatsProvider->getFormatsFromAttributes(RequestAttributesExtractor::extractAttributes($request));
5657
}
5758
}
5859

src/EventListener/AddFormatListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\EventListener;
1515

1616
use ApiPlatform\Core\Api\FormatsProviderInterface;
17+
use ApiPlatform\Core\Util\RequestAttributesExtractor;
1718
use Negotiation\Negotiator;
1819
use Symfony\Component\HttpFoundation\Request;
1920
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
@@ -55,7 +56,7 @@ public function onKernelRequest(GetResponseEvent $event)
5556
}
5657

5758
if (null !== $this->formatsProvider) {
58-
$this->formats = $this->formatsProvider->fromRequest($request);
59+
$this->formats = $this->formatsProvider->getFormatsFromAttributes(RequestAttributesExtractor::extractAttributes($request));
5960
}
6061

6162
$this->populateMimeTypes();

src/EventListener/DeserializeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function onKernelRequest(GetResponseEvent $event)
6060
return;
6161
}
6262
if (null !== $this->formatsProvider) {
63-
$this->formats = $this->formatsProvider->fromRequest($request);
63+
$this->formats = $this->formatsProvider->getFormatsFromAttributes($attributes);
6464
}
6565

6666
$format = $this->getFormat($request);

tests/Api/FormatsProviderTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1818
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1919
use PHPUnit\Framework\TestCase;
20-
use Symfony\Component\HttpFoundation\Request;
2120

2221
/**
2322
* @author Anthony GRASSIOT <antograssiot@free.fr>
@@ -26,48 +25,44 @@ class FormatsProviderTest extends TestCase
2625
{
2726
public function testNoResourceClass()
2827
{
29-
$request = new Request();
3028
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
3129
$resourceMetadataFactoryProphecy->create()->shouldNotBeCalled();
3230

3331
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => 'application/ld+json']);
3432

35-
$this->assertSame(['jsonld' => 'application/ld+json'], $formatProvider->fromRequest($request));
33+
$this->assertSame(['jsonld' => 'application/ld+json'], $formatProvider->getFormatsFromAttributes([]));
3634
}
3735

3836
public function testResourceClassWithoutFilters()
3937
{
40-
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
4138
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
4239
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata())->shouldBeCalled();
4340

4441
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json']]);
4542

46-
$this->assertSame(['jsonld' => ['application/ld+json']], $formatProvider->fromRequest($request));
43+
$this->assertSame(['jsonld' => ['application/ld+json']], $formatProvider->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get']));
4744
}
4845

4946
public function testResourceClassWithFilters()
5047
{
51-
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
5248
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
5349
$resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => 'jsonld']);
5450
$resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled();
5551

5652
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json'], 'json' => ['application/json']]);
5753

58-
$this->assertSame(['jsonld' => ['application/ld+json']], $formatProvider->fromRequest($request));
54+
$this->assertSame(['jsonld' => ['application/ld+json']], $formatProvider->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get']));
5955
}
6056

6157
public function testResourceClassWithFiltersOverRiddingMimeTypes()
6258
{
63-
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
6459
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
6560
$resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['jsonld' => ['application/foo'], 'bar' => ['application/bar', 'application/baz'], 'buz' => 'application/fuz']]);
6661
$resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled();
6762

6863
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json'], 'json' => ['application/json']]);
6964

70-
$this->assertSame(['jsonld' => ['application/foo'], 'bar' => ['application/bar', 'application/baz'], 'buz' => ['application/fuz']], $formatProvider->fromRequest($request));
65+
$this->assertSame(['jsonld' => ['application/foo'], 'bar' => ['application/bar', 'application/baz'], 'buz' => ['application/fuz']], $formatProvider->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get']));
7166
}
7267

7368
/**
@@ -76,13 +71,12 @@ public function testResourceClassWithFiltersOverRiddingMimeTypes()
7671
*/
7772
public function testBadFormatShortDeclaration()
7873
{
79-
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
8074
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
8175
$resourceMetadata = new ResourceMetadata(null, null, null, null, null, ['formats' => ['foo']]);
8276
$resourceMetadataFactoryProphecy->create('Foo')->willReturn($resourceMetadata)->shouldBeCalled();
8377

8478
$formatProvider = new FormatsProvider($resourceMetadataFactoryProphecy->reveal(), ['jsonld' => ['application/ld+json']]);
8579

86-
$formatProvider->fromRequest($request);
80+
$formatProvider->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get']);
8781
}
8882
}

tests/EventListener/AddFormatListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ public function testInvalidRouteFormat()
189189

190190
public function testResourceClassSupportedRequestFormat()
191191
{
192-
$request = new Request([], [], ['_api_resource_class' => 'Foo']);
192+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
193193
$request->setRequestFormat('csv');
194194

195195
$eventProphecy = $this->prophesize(GetResponseEvent::class);
196196
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
197197
$event = $eventProphecy->reveal();
198198

199199
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
200-
$formatsProviderProphecy->fromRequest($request)->willReturn(['csv' => ['text/csv']])->shouldbeCalled();
200+
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'get', 'receive' => true])->willReturn(['csv' => ['text/csv']])->shouldbeCalled();
201201

202202
$listener = new AddFormatListener(new Negotiator(), ['xml' => ['text/xml']], $formatsProviderProphecy->reveal());
203203
$listener->onKernelRequest($event);

tests/EventListener/DeserializeListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testDeserializeResourceClassSupportedFormat(string $method, bool
148148
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->willReturn([])->shouldBeCalled();
149149

150150
$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
151-
$formatsProviderProphecy->fromRequest($request)->willReturn(self::FORMATS)->shouldbeCalled();
151+
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'collection_operation_name' => 'post', 'receive' => true])->willReturn(self::FORMATS)->shouldbeCalled();
152152

153153
$listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), ['xml' => ['text/xml']], $formatsProviderProphecy->reveal());
154154

0 commit comments

Comments
 (0)