Skip to content

Commit 7e9828f

Browse files
ycerutonicolas-grekas
authored andcommitted
[HttpFoundation] Do not set the default Content-Type based on the Accept header
1 parent e818d86 commit 7e9828f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ErrorRenderer/SerializerErrorRenderer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\ErrorHandler\ErrorRenderer;
1313

1414
use Symfony\Component\ErrorHandler\Exception\FlattenException;
15+
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\RequestStack;
1617
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1718
use Symfony\Component\Serializer\SerializerInterface;
@@ -30,6 +31,7 @@ class SerializerErrorRenderer implements ErrorRendererInterface
3031

3132
/**
3233
* @param string|callable(FlattenException) $format The format as a string or a callable that should return it
34+
* formats not supported by Request::getMimeTypes() should be given as mime types
3335
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
3436
*/
3537
public function __construct(SerializerInterface $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false)
@@ -57,11 +59,16 @@ public function render(\Throwable $exception): FlattenException
5759

5860
try {
5961
$format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
62+
$headers = [
63+
'Content-Type' => Request::getMimeTypes($format)[0] ?? $format,
64+
'Vary' => 'Accept',
65+
];
6066

6167
return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
6268
'exception' => $exception,
6369
'debug' => \is_bool($this->debug) ? $this->debug : ($this->debug)($exception),
64-
]));
70+
]))
71+
->setHeaders($flattenException->getHeaders() + $headers);
6572
} catch (NotEncodableValueException $e) {
6673
return $this->fallbackErrorRenderer->render($exception);
6774
}

0 commit comments

Comments
 (0)