Skip to content

Commit 3ac7f83

Browse files
committed
Relax media type checks in HttpMessageConverters
Prior to this commit, `HttpMessageConverters` would assert that the given converter in `withXmlConverter` has a media type that is equal to "application/xml" in its list of supported converters. This approach would not work if the given converter supports "application/xml;charset=UTF-8" because of the strict equal check being performed. This commit ensures that we only consider the type and subtype of the considered media types when comparing, removing the parameters from the picture. Fixes gh-35801
1 parent deca37d commit 3ac7f83

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

spring-web/src/main/java/org/springframework/http/converter/DefaultHttpMessageConverters.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ abstract static class DefaultBuilder {
162162

163163

164164
void setStringConverter(HttpMessageConverter<?> stringConverter) {
165-
Assert.isTrue(stringConverter.getSupportedMediaTypes().contains(MediaType.TEXT_PLAIN),
166-
"stringConverter should support 'text/plain'");
165+
checkConverterSupports(stringConverter, MediaType.TEXT_PLAIN);
167166
this.stringConverter = stringConverter;
168167
}
169168

@@ -173,20 +172,17 @@ void setKotlinSerializationJsonConverter(HttpMessageConverter<?> kotlinJsonConve
173172
}
174173

175174
void setJsonConverter(HttpMessageConverter<?> jsonConverter) {
176-
Assert.isTrue(jsonConverter.getSupportedMediaTypes().contains(MediaType.APPLICATION_JSON),
177-
"jsonConverter should support 'application/json'");
175+
checkConverterSupports(jsonConverter, MediaType.APPLICATION_JSON);
178176
this.jsonConverter = jsonConverter;
179177
}
180178

181179
void setXmlConverter(HttpMessageConverter<?> xmlConverter) {
182-
Assert.isTrue(xmlConverter.getSupportedMediaTypes().contains(MediaType.TEXT_XML),
183-
"xmlConverter should support 'text/xml'");
180+
checkConverterSupports(xmlConverter, MediaType.TEXT_XML);
184181
this.xmlConverter = xmlConverter;
185182
}
186183

187184
void setSmileConverter(HttpMessageConverter<?> smileConverter) {
188-
Assert.isTrue(smileConverter.getSupportedMediaTypes().contains(new MediaType("application", "x-jackson-smile")),
189-
"smileConverter should support 'application/x-jackson-smile'");
185+
checkConverterSupports(smileConverter, new MediaType("application", "x-jackson-smile"));
190186
this.smileConverter = smileConverter;
191187
}
192188

@@ -196,17 +192,24 @@ void setKotlinSerializationCborConverter(HttpMessageConverter<?> kotlinCborConve
196192
}
197193

198194
void setCborConverter(HttpMessageConverter<?> cborConverter) {
199-
Assert.isTrue(cborConverter.getSupportedMediaTypes().contains(MediaType.APPLICATION_CBOR),
200-
"cborConverter should support 'application/cbor'");
195+
checkConverterSupports(cborConverter, MediaType.APPLICATION_CBOR);
201196
this.cborConverter = cborConverter;
202197
}
203198

204199
void setYamlConverter(HttpMessageConverter<?> yamlConverter) {
205-
Assert.isTrue(yamlConverter.getSupportedMediaTypes().contains(MediaType.APPLICATION_YAML),
206-
"yamlConverter should support 'application/yaml'");
200+
checkConverterSupports(yamlConverter, MediaType.APPLICATION_YAML);
207201
this.yamlConverter = yamlConverter;
208202
}
209203

204+
private void checkConverterSupports(HttpMessageConverter<?> converter, MediaType mediaType) {
205+
for (MediaType supportedMediaType : converter.getSupportedMediaTypes()) {
206+
if (mediaType.equalsTypeAndSubtype(supportedMediaType)) {
207+
return;
208+
}
209+
}
210+
throw new IllegalArgumentException("converter should support '" + mediaType + "'");
211+
}
212+
210213
void addCustomMessageConverter(HttpMessageConverter<?> customConverter) {
211214
Assert.notNull(customConverter, "'customConverter' must not be null");
212215
this.customConverters.add(customConverter);

spring-web/src/test/java/org/springframework/http/converter/DefaultHttpMessageConvertersTests.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,42 +65,47 @@ static Stream<Iterable<HttpMessageConverter<?>>> emptyMessageConverters() {
6565
void failsWhenStringConverterDoesNotSupportMediaType() {
6666
assertThatIllegalArgumentException()
6767
.isThrownBy(() -> HttpMessageConverters.forClient().withStringConverter(new CustomHttpMessageConverter()).build())
68-
.withMessage("stringConverter should support 'text/plain'");
68+
.withMessage("converter should support 'text/plain'");
6969
}
7070

7171
@Test
7272
void failsWhenJsonConverterDoesNotSupportMediaType() {
7373
assertThatIllegalArgumentException()
7474
.isThrownBy(() -> HttpMessageConverters.forClient().withJsonConverter(new CustomHttpMessageConverter()).build())
75-
.withMessage("jsonConverter should support 'application/json'");
75+
.withMessage("converter should support 'application/json'");
76+
}
77+
78+
@Test
79+
void canConfigureXmlConverterWithCharset() {
80+
HttpMessageConverters.forClient().withXmlConverter(new JacksonXmlHttpMessageConverter()).build();
7681
}
7782

7883
@Test
7984
void failsWhenXmlConverterDoesNotSupportMediaType() {
8085
assertThatIllegalArgumentException()
8186
.isThrownBy(() -> HttpMessageConverters.forClient().withXmlConverter(new CustomHttpMessageConverter()).build())
82-
.withMessage("xmlConverter should support 'text/xml'");
87+
.withMessage("converter should support 'text/xml'");
8388
}
8489

8590
@Test
8691
void failsWhenSmileConverterDoesNotSupportMediaType() {
8792
assertThatIllegalArgumentException()
8893
.isThrownBy(() -> HttpMessageConverters.forClient().withSmileConverter(new CustomHttpMessageConverter()).build())
89-
.withMessage("smileConverter should support 'application/x-jackson-smile'");
94+
.withMessage("converter should support 'application/x-jackson-smile'");
9095
}
9196

9297
@Test
9398
void failsWhenCborConverterDoesNotSupportMediaType() {
9499
assertThatIllegalArgumentException()
95100
.isThrownBy(() -> HttpMessageConverters.forClient().withCborConverter(new CustomHttpMessageConverter()).build())
96-
.withMessage("cborConverter should support 'application/cbor'");
101+
.withMessage("converter should support 'application/cbor'");
97102
}
98103

99104
@Test
100105
void failsWhenYamlConverterDoesNotSupportMediaType() {
101106
assertThatIllegalArgumentException()
102107
.isThrownBy(() -> HttpMessageConverters.forClient().withYamlConverter(new CustomHttpMessageConverter()).build())
103-
.withMessage("yamlConverter should support 'application/yaml'");
108+
.withMessage("converter should support 'application/yaml'");
104109
}
105110

106111

0 commit comments

Comments
 (0)