Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SerializeErrorRenderer unable to provide a CSV error response. #50413

Open
infinitum11 opened this issue May 24, 2023 · 4 comments
Open

SerializeErrorRenderer unable to provide a CSV error response. #50413

infinitum11 opened this issue May 24, 2023 · 4 comments

Comments

@infinitum11
Copy link

infinitum11 commented May 24, 2023

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.

@infinitum11
Copy link
Author

Another solution is to explicitly add an additional request format in the config file.

# config/packages/framework.yaml
framework:
  request:
    formats:
      csv: ['text/csv', 'application/csv', 'text/x-comma-separated-values', 'text/x-csv'],

But this is something you would like to get out of the box without additional config management.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@yceruto
Copy link
Member

yceruto commented Nov 28, 2023

I think CSV format is common enough to be included in the default list.

@infinitum11 up for a PR to add csv format?

@carsonbot carsonbot removed the Stalled label Nov 28, 2023
@infinitum11
Copy link
Author

I can't remember, but there was a side effect somewhere after appending a "csv" key-value pair to $formats. I need to review my code again and try to remember/find out what was wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants