From 607bd94c07360f1cab29acc4f10b557eb96f64d4 Mon Sep 17 00:00:00 2001 From: Marcel Strahl Date: Thu, 17 Sep 2020 17:18:12 +0200 Subject: [PATCH 1/2] Fix: Wrong headers on xml added at serialization --- src/ResponseFactory.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index 89cbaeb..e7522b3 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -7,9 +7,11 @@ use Dropelikeit\LaravelJmsSerializer\Config\Config; use Dropelikeit\LaravelJmsSerializer\Config\ConfigInterface; use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType; +use Illuminate\Http\Response as LaravelResponse; use JMS\Serializer\SerializationContext; use JMS\Serializer\SerializerInterface; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; /** * @author Marcel Strahl @@ -70,7 +72,7 @@ public function withSerializeType(string $serializeType): self return $instance; } - public function create(object $jmsResponse): JsonResponse + public function create(object $jmsResponse): Response { $initialType = $this->getInitialType($jmsResponse); @@ -81,19 +83,27 @@ public function create(object $jmsResponse): JsonResponse $initialType ); - return new JsonResponse($content, $this->status, ['application/json'], true); + if ($this->serializeType === Config::SERIALIZE_TYPE_XML) { + return new LaravelResponse($content, $this->status, ['Content-Type' => 'application/xml']); + } + + return new JsonResponse($content, $this->status, ['Content-Type' => 'application/json'], true); } /** * @param array $jmsResponse * - * @return JsonResponse + * @return Response */ - public function createFromArray(array $jmsResponse): JsonResponse + public function createFromArray(array $jmsResponse): Response { $content = $this->serializer->serialize($jmsResponse, $this->serializeType, $this->context); - return new JsonResponse($content, $this->status, ['application/json'], true); + if ($this->serializeType === Config::SERIALIZE_TYPE_XML) { + return new LaravelResponse($content, $this->status, ['Content-Type' => 'application/xml']); + } + + return new JsonResponse($content, $this->status, ['Content-Type' => 'application/json'], true); } private function getInitialType(object $jmsResponse): ?string From 0f7029ef8ce47fdd27c617cb7759893c51e890f2 Mon Sep 17 00:00:00 2001 From: Marcel Strahl Date: Thu, 17 Sep 2020 17:29:23 +0200 Subject: [PATCH 2/2] Fix: add an test for serialize-controlled response creation --- tests/ResponseFactoryTest.php | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 2de57cf..7305447 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -180,9 +180,13 @@ public function canUseGivenContext(): void } /** + * @param string $changeSerializeTypeTo + * @param string $expectedResult + * * @test + * @dataProvider dataProviderCanSerializeWithSerializeType */ - public function canWithSerializeType(): void + public function canSerializeWithSerializeType(string $changeSerializeTypeTo, string $expectedResult): void { $this->config ->expects(self::once()) @@ -201,19 +205,37 @@ public function canWithSerializeType(): void $responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config); $responseFactory->withContext(SerializationContext::create()->setSerializeNull(true)); - $responseFactory = $responseFactory->withSerializeType(Config::SERIALIZE_TYPE_XML); + $responseFactory = $responseFactory->withSerializeType($changeSerializeTypeTo); $response = $responseFactory->create(new Dummy()); self::assertEquals( - ' + $expectedResult, + $response->getContent() + ); + } + + /** + * @return array> + * @psalm-return array{with_json: array, 'with_xml': array} + */ + public function dataProviderCanSerializeWithSerializeType(): array + { + return [ + 'with_json' => [ + Config::SERIALIZE_TYPE_JSON, + '{"amount":12,"text":"Hello World!"}', + ], + 'with_xml' => [ + Config::SERIALIZE_TYPE_XML, + ' 12 ', - $response->getContent() - ); + ], + ]; } /**