Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Request::getContentType() method name is confusing #39750

Closed
@pounard

Description

@pounard

While working on a project where we do not use the symfony/serializer directly, I wrote this kind of code in a controller:

            if ('application/json' !== $request->getContentType()) {
                throw new UnsupportedMediaTypeHttpException('Unsupported Media Type');
            }

This sample code is simple and stupid, but reading it, it's explicit and understandable.

It took me a few minutes and step debugging to understand why it wasn't working, this method isn't actually returning the content type, (e.g. application/json for example), but symfony/serializer own format value instead (here simply json).

My conclusion here is that the serializer component internals is leaking over the request logic, and it should not. Symfony request object is used by many frameworks, as a standalone component. And some Symfony-driven applications don't use the serializer component.

I think that most users will assume while reading the above code that getContentType() actually return the Content-Type header value, and not a reduction of it that is suitable to only symfony/serializer. In my opinion method should be renamed to something such as getContentFormat() or getContentSerializerFormat().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good first issueIdeal for your first contribution! (some Symfony experience may be required)HttpFoundationRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions