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

Make possible to be strict about format in JsonBodyListener #18

Open
shouze opened this issue Aug 17, 2017 · 2 comments
Open

Make possible to be strict about format in JsonBodyListener #18

shouze opened this issue Aug 17, 2017 · 2 comments

Comments

@shouze
Copy link
Member

shouze commented Aug 17, 2017

Description

Steps to reproduce the issue:

  1. Have a Symfony controller action that return this kind of response:
return new JsonResponse(['key' => $request->get('key')]);
  1. then request this action like that:
# We don't set any Accept nor Content-Type header
curl -d '{"key": "value"} http://domain.tld/path 

Describe the results you received:

HTTP/1.1 200 OK

{"key": null}

Describe the results you expected:

HTTP/1.1 406 Not Acceptable

Note: Here we consider that we implement HTTP content negociation the right way so the client must provide a Content-Type header.

From where to start

The actual part of JsonBodyListener incrimined code is:

https://github.com/rezzza/symfony-json-rest-api/blob/67639f9383eb4637f6ea56eed136af5373282428/src/JsonBodyListener.php#L34-L40

Other source(s) to watch for inspiration

FOSRestBundle FormatListener.php#L58-L75:

            $format = $request->getRequestFormat(null);
            if (null === $format) {
                $accept = $this->formatNegotiator->getBest('');
                if (null !== $accept && 0.0 < $accept->getQuality()) {
                    $format = $request->getFormat($accept->getValue());
                    if (null !== $format) {
                        $request->attributes->set('media_type', $accept->getValue());
                    }
                }
            }

            if (null === $format) {
                if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
                    throw new NotAcceptableHttpException('No matching accepted Response format could be determined');
                }

                return;
            }
@tyx
Copy link
Member

tyx commented Aug 17, 2017

We need the routing part

@shouze
Copy link
Member Author

shouze commented Sep 28, 2017

ping @tyx I guess this is closed by #21 and #23?

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

No branches or pull requests

2 participants