Skip to content

Commit

Permalink
Merge pull request #8 from Dropelikeit/bugfix/wrong-headers
Browse files Browse the repository at this point in the history
Fix: Wrong headers on xml added at serialization
  • Loading branch information
Dropelikeit authored Sep 17, 2020
2 parents d2b227c + 0f7029e commit abce76d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
20 changes: 15 additions & 5 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down Expand Up @@ -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);

Expand All @@ -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<int|string, mixed> $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
Expand Down
32 changes: 27 additions & 5 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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(
'<?xml version="1.0" encoding="UTF-8"?>
$expectedResult,
$response->getContent()
);
}

/**
* @return array<string, array<int, string>>
* @psalm-return array{with_json: array<int, string>, 'with_xml': array<int, string>}
*/
public function dataProviderCanSerializeWithSerializeType(): array
{
return [
'with_json' => [
Config::SERIALIZE_TYPE_JSON,
'{"amount":12,"text":"Hello World!"}',
],
'with_xml' => [
Config::SERIALIZE_TYPE_XML,
'<?xml version="1.0" encoding="UTF-8"?>
<result>
<amount>12</amount>
<text><![CDATA[Hello World!]]></text>
</result>
',
$response->getContent()
);
],
];
}

/**
Expand Down

0 comments on commit abce76d

Please sign in to comment.