Skip to content

HTTP content negotiation does not respect media type parameters [SPR-13365] #17949

@spring-projects-issues

Description

@spring-projects-issues

Benedict Adamson opened SPR-13365 and commented

The
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor
class does not respect media-type parameters when doing content negotiation. In the writeWithMessageConverters method, if a requestedType has a non empty MimeType.getParameters() map, those parameters have no effect on which type is the selectedMediaType. I believe that HTTP standard conformance requires that those parameters do have an effect on content negotiation.

The cause of this seems to be that writeWithMessageConverters uses MimeType.isCompatibleWith(MimeType), which ignores the parameters, and then (indirectly) sorts the produceable media types using MediaType.SPECIFICITY_COMPARATOR, which also ignores the parameters.


Worse still, if the selectedMediaType (the supported media type of the chosen HTTP message converter) differs from the requested type only in the media-type parameters, the Content-Type header of the response will contain the requested media type even though the response body is actually of the selectedMediaType.

The cause of that seems to be that the AbstractMessageConverterMethodProcessor.getMostSpecificMediaType(MediaType, MediaType) chooses the acceptType rather than the produceTypeToUse when the two types have equal specificity, according to the MediaType.SPECIFICITY_COMPARATOR, and that comparator ignores the media-type parameters.


Affects: 4.1 GA

Issue Links:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions