Skip to content

Commit

Permalink
Fix: Wrong headers on xml added at serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Strahl committed Sep 17, 2020
1 parent d2b227c commit 607bd94
Showing 1 changed file with 15 additions and 5 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

0 comments on commit 607bd94

Please sign in to comment.