Skip to content

Allow to disable input and output class #2393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2018
Merged
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
8 changes: 4 additions & 4 deletions src/Annotation/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
* @Attribute("filters", type="string[]"),
* @Attribute("graphql", type="array"),
* @Attribute("hydraContext", type="array"),
* @Attribute("inputClass", type="string"),
* @Attribute("inputClass", type="mixed"),
* @Attribute("iri", type="string"),
* @Attribute("itemOperations", type="array"),
* @Attribute("maximumItemsPerPage", type="int"),
* @Attribute("mercure", type="mixed"),
* @Attribute("normalizationContext", type="array"),
* @Attribute("order", type="array"),
* @Attribute("outputClass", type="string"),
* @Attribute("outputClass", type="mixed"),
* @Attribute("paginationClientEnabled", type="bool"),
* @Attribute("paginationClientItemsPerPage", type="bool"),
* @Attribute("paginationClientPartial", type="bool"),
Expand Down Expand Up @@ -277,14 +277,14 @@ final class ApiResource
/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string
* @var string|false
*/
private $inputClass;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var string
* @var string|false
*/
private $outputClass;

Expand Down
1 change: 1 addition & 0 deletions src/EventListener/DeserializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function onKernelRequest(GetResponseEvent $event)
$request->isMethodSafe(false)
|| 'DELETE' === $method
|| !($attributes = RequestAttributesExtractor::extractAttributes($request))
|| false === ($attributes['input_class'] ?? null)
|| !$attributes['receive']
|| (
'' === ($requestContent = $request->getContent())
Expand Down
17 changes: 13 additions & 4 deletions src/EventListener/SerializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,30 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
return;
}

$request->attributes->set('_api_respond', true);
$context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);

if (isset($context['output_class'])) {
if (false === $context['output_class']) {
// If the output class is explicitly set to false, the response must be empty
$event->setControllerResult('');

return;
}

$context['resource_class'] = $context['output_class'];
}

if ($included = $request->attributes->get('_api_included')) {
$context['api_included'] = $included;
}
$resources = new ResourceList();
$context['resources'] = &$resources;
if (isset($context['output_class'])) {
$context['resource_class'] = $context['output_class'];
}

$request->attributes->set('_api_normalization_context', $context);

$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));

$request->attributes->set('_api_respond', true);
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
}

Expand Down
5 changes: 3 additions & 2 deletions src/EventListener/WriteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;

/**
Expand All @@ -40,7 +41,7 @@ public function __construct(DataPersisterInterface $dataPersister, IriConverterI
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if ($request->isMethodSafe(false) || !$request->attributes->has('_api_resource_class') || !$request->attributes->getBoolean('_api_persist', true)) {
if ($request->isMethodSafe(false) || !$request->attributes->getBoolean('_api_persist', true) || !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
return;
}

Expand All @@ -64,7 +65,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
// Controller result must be immutable for _api_write_item_iri
// if it's class changed compared to the base class let's avoid calling the IriConverter
// especially that the Output class could be a DTO that's not referencing any route
if (null !== $this->iriConverter && \get_class($controllerResult) === \get_class($event->getControllerResult())) {
if (null !== $this->iriConverter && (false !== $attributes['output_class'] ?? null) && \get_class($controllerResult) === \get_class($event->getControllerResult())) {
$request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromItem($controllerResult));
}
break;
Expand Down
21 changes: 21 additions & 0 deletions tests/EventListener/DeserializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ public function testDoNotCallWhenReceiveFlagIsFalse()
$listener->onKernelRequest($eventProphecy->reveal());
}

public function testDoNotCallWhenInputClassDisabled()
{
$eventProphecy = $this->prophesize(GetResponseEvent::class);

$request = new Request([], [], ['data' => new \stdClass(), '_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post', '_api_input_class' => false]);
$request->setMethod('POST');
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->deserialize()->shouldNotBeCalled();

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->shouldNotBeCalled();

$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
$formatsProviderProphecy->getFormatsFromAttributes(Argument::type('array'))->shouldNotBeCalled();

$listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal());
$listener->onKernelRequest($eventProphecy->reveal());
}

/**
* @dataProvider methodProvider
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/EventListener/SerializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ public function testSerializeCollectionOperation()
$listener->onKernelView($eventProphecy->reveal());
}

public function testSerializeCollectionOperationWithOutputClassDisabled()
{
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'collection_operation_name' => 'post', 'output_class' => false];
$serializerProphecy = $this->prophesize(SerializerInterface::class);

$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get', '_api_output_class' => false]);
$request->setRequestFormat('xml');

$eventProphecy = $this->prophesize(GetResponseForControllerResultEvent::class);
$eventProphecy->getControllerResult()->willReturn(new \stdClass())->shouldBeCalled();
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
$eventProphecy->setControllerResult('')->shouldBeCalled();

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), true, Argument::type('array'))->willReturn($expectedContext)->shouldBeCalled();

$listener = new SerializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal());
$listener->onKernelView($eventProphecy->reveal());
}

public function testSerializeItemOperation()
{
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'item_operation_name' => 'get'];
Expand Down
51 changes: 36 additions & 15 deletions tests/EventListener/WriteListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function testOnKernelViewWithControllerResultAndPersist()
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1')->shouldBeCalled();

$request = new Request();
$request->attributes->set('_api_resource_class', Dummy::class);
$request = new Request([], [], ['_api_resource_class' => Dummy::class]);

$event = new GetResponseForControllerResultEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
Expand All @@ -51,6 +50,7 @@ public function testOnKernelViewWithControllerResultAndPersist()

foreach (['PATCH', 'PUT', 'POST'] as $httpMethod) {
$request->setMethod($httpMethod);
$request->attributes->set(sprintf('_api_%s_operation_name', 'POST' === $httpMethod ? 'collection' : 'item'), strtolower($httpMethod));

(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
$this->assertSame($dummy, $event->getControllerResult());
Expand All @@ -71,8 +71,7 @@ public function testOnKernelViewWithControllerResultAndPersistReturningVoid()
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
$dataPersisterProphecy->persist($dummy)->shouldBeCalled();

$request = new Request();
$request->attributes->set('_api_resource_class', Dummy::class);
$request = new Request([], [], ['_api_resource_class' => Dummy::class]);

$event = new GetResponseForControllerResultEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
Expand All @@ -83,6 +82,7 @@ public function testOnKernelViewWithControllerResultAndPersistReturningVoid()

foreach (['PATCH', 'PUT', 'POST'] as $httpMethod) {
$request->setMethod($httpMethod);
$request->attributes->set(sprintf('_api_%s_operation_name', 'POST' === $httpMethod ? 'collection' : 'item'), strtolower($httpMethod));

(new WriteListener($dataPersisterProphecy->reveal()))->onKernelView($event);
$this->assertSame($dummy, $event->getControllerResult());
Expand Down Expand Up @@ -112,8 +112,7 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
->shouldBeCalled()
;

$request = new Request();
$request->attributes->set('_api_resource_class', Dummy::class);
$request = new Request([], [], ['_api_resource_class' => Dummy::class]);

foreach (['PATCH', 'PUT', 'POST'] as $httpMethod) {
$event = new GetResponseForControllerResultEvent(
Expand All @@ -124,6 +123,7 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
);

$request->setMethod($httpMethod);
$request->attributes->set(sprintf('_api_%s_operation_name', 'POST' === $httpMethod ? 'collection' : 'item'), strtolower($httpMethod));

(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);

Expand All @@ -132,6 +132,32 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
}
}

public function testOnKernelViewDoNotCallIriConverterWhenOutputClassDisabled()
{
$dummy = new Dummy();
$dummy->setName('Dummyrino');

$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();

$dataPersisterProphecy->persist($dummy)->willReturn($dummy)->shouldBeCalled();

$request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_collection_operation_name' => 'post', '_api_output_class' => false]);
$request->setMethod('POST');

$event = new GetResponseForControllerResultEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
$request,
HttpKernelInterface::MASTER_REQUEST,
$dummy
);

(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
}

public function testOnKernelViewWithControllerResultAndRemove()
{
$dummy = new Dummy();
Expand All @@ -144,9 +170,8 @@ public function testOnKernelViewWithControllerResultAndRemove()
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();

$request = new Request();
$request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_item_operation_name' => 'delete']);
$request->setMethod('DELETE');
$request->attributes->set('_api_resource_class', Dummy::class);

$event = new GetResponseForControllerResultEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
Expand All @@ -171,9 +196,8 @@ public function testOnKernelViewWithSafeMethod()
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();

$request = new Request();
$request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_item_operation_name' => 'head']);
$request->setMethod('HEAD');
$request->attributes->set('_api_resource_class', Dummy::class);

$event = new GetResponseForControllerResultEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
Expand All @@ -198,10 +222,8 @@ public function testOnKernelViewWithPersistFlagOff()
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();

$request = new Request();
$request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_item_operation_name' => 'head', '_api_persist' => false]);
$request->setMethod('HEAD');
$request->attributes->set('_api_resource_class', Dummy::class);
$request->attributes->set('_api_persist', false);

$event = new GetResponseForControllerResultEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
Expand Down Expand Up @@ -252,9 +274,8 @@ public function testOnKernelViewWithNoDataPersisterSupport()
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();

$request = new Request();
$request = new Request([], [], ['_api_resource_class' => 'Dummy', '_api_collection_operation_name' => 'post']);
$request->setMethod('POST');
$request->attributes->set('_api_resource_class', 'Dummy');

$event = new GetResponseForControllerResultEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
Expand Down