Skip to content

Make possible to be strict about format in JsonBodyListener #18

@shouze

Description

@shouze

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;
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions