-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
RequestedContentTypeResolver does not ignore quality factor when filtering */* media types #29915
Comments
Thanks for the analysis, but you're several steps ahead of us here. A sample application showing the behavior and a short note explaining what you would expect instead would be really helpful. Could you provide a minimal sample please? |
@bclozel simplified it to two test cases: @Test
public void allMediaTypeWithTwoResolvers() {
RequestedContentTypeResolverBuilder builder = new RequestedContentTypeResolverBuilder();
builder.resolver(new HeaderContentTypeResolver());
builder.resolver(new FixedContentTypeResolver(MediaType.APPLICATION_JSON));
RequestedContentTypeResolver resolver = builder.build();
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").accept(MediaType.ALL));
List<MediaType> mediaTypes = resolver.resolveMediaTypes(exchange);
assertThat(mediaTypes).isEqualTo(Collections.singletonList(MediaType.APPLICATION_JSON));
}
@Test
public void allMediaTypeWithQualityFactorAndTwoResolvers() {
RequestedContentTypeResolverBuilder builder = new RequestedContentTypeResolverBuilder();
builder.resolver(new HeaderContentTypeResolver());
builder.resolver(new FixedContentTypeResolver(MediaType.APPLICATION_JSON));
RequestedContentTypeResolver resolver = builder.build();
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/").accept(MediaType.valueOf("*/*;q=0.8")));
List<MediaType> mediaTypes = resolver.resolveMediaTypes(exchange);
assertThat(mediaTypes).isEqualTo(Collections.singletonList(MediaType.APPLICATION_JSON));
} The last one fails it returns:
The code in |
Thanks @nbaars for the report, this has been fixed in 6.0.x and 5.3.x and will be released tomorrow. |
When you have two content type resolvers:
And suppose the FixedContentTypeResolver returns
application/json
.When you have the following controller definitions:
So basically, we have two mappings one produces JSON and one a CSV file.
When you call this controller with:
It produces a JSON message. However, when you call this controller with:
All browsers do add a quality factor by default.
It defaults to whatever HttpMessageWriter can produce the type, in our case, a CSV mapper HttpMessageEncoder and it writes a CSV file with content type
text/csv
. One could argue the problem lies here, however looking at the code inRequestedContentTypeResolverBuilder
:There is a special case for
*/*
in which it tries the next content type resolver. But when it encounters*/*;q=0.8
it does not work. The problem is that:does not apply the method
MediaType#removeQualityValue
, which is necessary to fire thecontinue
block in thebuild
method above.The workaround for this issue is to add
produces
to the first controller method explicitly.The text was updated successfully, but these errors were encountered: