Description
Symfony version(s) affected
6.2.10
Description
The SerializerErrorRenderer class cannot extract a csv acceptable header from a request. The static method SerializerErrorRenderer::getPreferredFormat delegates that work to the Request->getPreferredFormat method which is unable to return "csv" because static::$formats doesn't contain information about "csv" format (in the case when request attribute "_format" is not set explicitly).
How to reproduce
Create a controller that throws any exception.
#[Route('/exception', name: 'app_exception')]
public function exception(Request $request): Response
{
throw new \Exception('boom!');
}
Run a cURL request including the "Accept: text/csv" header.
curl -X GET -i --header 'Accept: text/csv' http://localhost:8000/exception
Expected result:
content-type: text/csv; charset=UTF-8
...
[CSV encoded body]
Actual result:
content-type: text/html; charset=UTF-8
...
[HTML encoded body]
Possible Solution
Add a 'csv' => ['text/csv', 'application/csv', 'text/x-comma-separated-values', 'text/x-csv'],
line into initializeFormats method of Request class.
protected static function initializeFormats()
{
static::$formats = [
'html' => ['text/html', 'application/xhtml+xml'],
'txt' => ['text/plain'],
'js' => ['application/javascript', 'application/x-javascript', 'text/javascript'],
'css' => ['text/css'],
'json' => ['application/json', 'application/x-json'],
'jsonld' => ['application/ld+json'],
'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
'rdf' => ['application/rdf+xml'],
'atom' => ['application/atom+xml'],
'rss' => ['application/rss+xml'],
'form' => ['application/x-www-form-urlencoded', 'multipart/form-data'],
'csv' => ['text/csv', 'application/csv', 'text/x-comma-separated-values', 'text/x-csv'],
];
}
Additional Context
Everything works fine when the format is explicitly set in a preview mode i.e.
curl -X GET -i --header 'Accept: text/csv' http://localhost:8000/_error/500.csv
The request attribute "_format" is set to "csv" in this case.