From 00e8de6f29439f26b6280c59dfa8f14dff66d2d3 Mon Sep 17 00:00:00 2001 From: Marin Binzari Date: Thu, 26 Jan 2023 16:33:42 +0200 Subject: [PATCH 1/2] fix(metadata): Allow custom http status code for PUT --- src/Symfony/EventListener/RespondListener.php | 2 +- .../EventListener/RespondListenerTest.php | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Symfony/EventListener/RespondListener.php b/src/Symfony/EventListener/RespondListener.php index f330df2fa23..da743e0a141 100644 --- a/src/Symfony/EventListener/RespondListener.php +++ b/src/Symfony/EventListener/RespondListener.php @@ -90,7 +90,7 @@ public function onKernelView(ViewEvent $event): void ) { $status = 301; $headers['Location'] = $this->iriConverter->getIriFromResource($request->attributes->get('data'), UrlGeneratorInterface::ABS_PATH, $operation); - } elseif (HttpOperation::METHOD_PUT === $method && !($attributes['previous_data'] ?? null)) { + } elseif (HttpOperation::METHOD_PUT === $method && !($attributes['previous_data'] ?? null) && $status === null) { $status = Response::HTTP_CREATED; } diff --git a/tests/Symfony/EventListener/RespondListenerTest.php b/tests/Symfony/EventListener/RespondListenerTest.php index 0d5f09acd80..457dc59c0c4 100644 --- a/tests/Symfony/EventListener/RespondListenerTest.php +++ b/tests/Symfony/EventListener/RespondListenerTest.php @@ -16,6 +16,7 @@ use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\Post; +use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; use ApiPlatform\Symfony\EventListener\RespondListener; @@ -234,6 +235,28 @@ public function testSetCustomStatus(): void $this->assertSame(Response::HTTP_ACCEPTED, $event->getResponse()->getStatusCode()); } + public function testSetCustomStatusForPut(): void + { + $request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_operation_name' => 'put', '_api_respond' => true], [], [], ['REQUEST_METHOD' => 'PUT']); + $event = new ViewEvent( + $this->prophesize(HttpKernelInterface::class)->reveal(), + $request, + \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, + 'bar' + ); + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); + $resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn(new ResourceMetadataCollection(Dummy::class, [ + new ApiResource(operations: [ + 'put' => new Put(status: Response::HTTP_ACCEPTED), + ]), + ])); + + $listener = new RespondListener($resourceMetadataFactoryProphecy->reveal()); + $listener->onKernelView($event); + + $this->assertSame(Response::HTTP_ACCEPTED, $event->getResponse()->getStatusCode()); + } + public function testHandleResponse(): void { $listener = new RespondListener(); From 9a868a600a53430704ffbf35597513b45f42234f Mon Sep 17 00:00:00 2001 From: Marin Binzari Date: Thu, 26 Jan 2023 16:40:55 +0200 Subject: [PATCH 2/2] fix: php cs --- src/Symfony/EventListener/RespondListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/EventListener/RespondListener.php b/src/Symfony/EventListener/RespondListener.php index da743e0a141..a9a075ccd23 100644 --- a/src/Symfony/EventListener/RespondListener.php +++ b/src/Symfony/EventListener/RespondListener.php @@ -90,7 +90,7 @@ public function onKernelView(ViewEvent $event): void ) { $status = 301; $headers['Location'] = $this->iriConverter->getIriFromResource($request->attributes->get('data'), UrlGeneratorInterface::ABS_PATH, $operation); - } elseif (HttpOperation::METHOD_PUT === $method && !($attributes['previous_data'] ?? null) && $status === null) { + } elseif (HttpOperation::METHOD_PUT === $method && !($attributes['previous_data'] ?? null) && null === $status) { $status = Response::HTTP_CREATED; }