-
Notifications
You must be signed in to change notification settings - Fork 38.5k
HTTP content negotiation does not respect media type parameters [SPR-13365] #17949
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
Comments
Rossen Stoyanchev commented Can you provide a specific example or two with the requested media type(s) and expected vs actual behavior? |
Benedict Adamson commented Here is some more information about the situation that lead me to discover this problem, with the details anonymized. My web client and server communicate using a custom binary format. I gave the first version of the format a media-type like I need to alter the custom binary format to add some new functionality. I need backwards compatibility, some my client and server should be able to communicate with the new version of the format and the old version of the format. I therefore chose to give the new version of the format a new media-type, like I'm using Test Driven Development, so I added a test-case for my current version of the web server (using |
Benedict Adamson commented I see now that this is a duplicate of #15531. |
Brian Clozel commented Hi Raedwald In my opinion, According to RFC 7231 section 3.1.1.1:
Given that your custom media type is not part of the registry, we can't really decide with this information. RFC2046 section 1 says:
If you're changing the nature of the representation between those two, then you should probably use something like Anyway, I think this question may duplicate #15531 - could you take a look at the comments? |
Brian Clozel commented About the second part of this issue, the Are you sure
Now I may be missing something here. If you've got pointers for this (what's the expected behavior here for a server) or if you think this should be the opposite (and why?) - please let me know! |
Rossen Stoyanchev commented
Precisely, how would we compare the version parameter? A numeric comparison would be the obvious place to start but what if it was 2a, 2.0.1.A, or other. Also suppose there were additional parameters, perhaps major and minor versions, or perhaps completely unrelated to versioning. From a framework perspective we have no knowledge about the meaning of those parameters and their possible values. As Brian outlined above we are quite intentionally sorting based on as much as we can assume, the type and sub-type, quality parameters, and number of parameters. |
Benedict Adamson commented
Compare for equality. Here is a possible ranking scheme:
|
Benedict Adamson commented
As you point out, it examines the number of parameters. But it ignores the values of the parameters, which is what I meant. |
Brian Clozel commented I'm closing this issue as a duplicate, but feel free to open a new issue (improvement proposal) regarding mediatype comparison + media type parameters. This is an interesting issue, but it will require developer interest (votes!) and use cases descriptions to really understand what we can do about that. Thanks! |
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 arequestedType
has a non emptyMimeType.getParameters()
map, those parameters have no effect on which type is theselectedMediaType
. 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
usesMimeType.isCompatibleWith(MimeType)
, which ignores the parameters, and then (indirectly) sorts the produceable media types usingMediaType.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, theContent-Type
header of the response will contain the requested media type even though the response body is actually of theselectedMediaType
.The cause of that seems to be that the
AbstractMessageConverterMethodProcessor.getMostSpecificMediaType(MediaType, MediaType)
chooses theacceptType
rather than theproduceTypeToUse
when the two types have equal specificity, according to theMediaType.SPECIFICITY_COMPARATOR
, and that comparator ignores the media-type parameters.Affects: 4.1 GA
Issue Links:
The text was updated successfully, but these errors were encountered: