Description
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()
.