Skip to content

Commit 75fd391

Browse files
committed
Remove quality parameter from selected media type
Prior to this commit, WebFlux application would keep the quality parameter from the "Accept" request header when selecting a media type for the response. It would then echo it back to the client. While strictly not wrong, this is unnecessary and can confuse HTTP clients. This commit aligns WebFlux's behavior with Spring MVC. Fixes gh-24239
1 parent 8082b33 commit 75fd391

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
156156
}
157157

158158
if (selected != null) {
159+
selected = selected.removeQualityValue();
159160
if (logger.isDebugEnabled()) {
160161
logger.debug("Using '" + selected + "' given " + acceptableTypes +
161162
" and supported " + producibleTypes);

spring-webflux/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public class HandlerResultHandlerTests {
5151

5252

5353
@Test
54-
public void usesContentTypeResolver() throws Exception {
54+
void usesContentTypeResolver() {
5555
TestResultHandler resultHandler = new TestResultHandler(new FixedContentTypeResolver(IMAGE_GIF));
5656
List<MediaType> mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG);
5757
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
@@ -61,7 +61,7 @@ public void usesContentTypeResolver() throws Exception {
6161
}
6262

6363
@Test
64-
public void producibleMediaTypesRequestAttribute() throws Exception {
64+
void producibleMediaTypesRequestAttribute() {
6565
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
6666
exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(IMAGE_GIF));
6767

@@ -72,7 +72,7 @@ public void producibleMediaTypesRequestAttribute() throws Exception {
7272
}
7373

7474
@Test // SPR-9160
75-
public void sortsByQuality() throws Exception {
75+
void sortsByQuality() {
7676
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")
7777
.header("Accept", "text/plain; q=0.5, application/json"));
7878

@@ -83,7 +83,7 @@ public void sortsByQuality() throws Exception {
8383
}
8484

8585
@Test
86-
public void charsetFromAcceptHeader() throws Exception {
86+
void charsetFromAcceptHeader() {
8787
MediaType text8859 = MediaType.parseMediaType("text/plain;charset=ISO-8859-1");
8888
MediaType textUtf8 = MediaType.parseMediaType("text/plain;charset=UTF-8");
8989
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path").accept(text8859));
@@ -93,14 +93,25 @@ public void charsetFromAcceptHeader() throws Exception {
9393
}
9494

9595
@Test // SPR-12894
96-
public void noConcreteMediaType() throws Exception {
96+
void noConcreteMediaType() {
9797
List<MediaType> producible = Collections.singletonList(ALL);
9898
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
9999
MediaType actual = this.resultHandler.selectMediaType(exchange, () -> producible);
100100

101101
assertThat(actual).isEqualTo(APPLICATION_OCTET_STREAM);
102102
}
103103

104+
@Test
105+
void removeQualityParameter() {
106+
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path")
107+
.header("Accept", "text/plain; q=0.5"));
108+
109+
List<MediaType> mediaTypes = Arrays.asList(APPLICATION_JSON, TEXT_PLAIN);
110+
MediaType actual = this.resultHandler.selectMediaType(exchange, () -> mediaTypes);
111+
112+
assertThat(actual).isEqualTo(TEXT_PLAIN);
113+
}
114+
104115

105116
@SuppressWarnings("WeakerAccess")
106117
private static class TestResultHandler extends HandlerResultHandlerSupport {

0 commit comments

Comments
 (0)