Concrete MediaType check in StringHttpMessageConverter
Closes gh-23287
This commit is contained in:
parent
c480a99a77
commit
9d963abb7d
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -239,7 +239,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
|
|||
protected void addDefaultHeaders(HttpHeaders headers, T t, @Nullable MediaType contentType) throws IOException {
|
||||
if (headers.getContentType() == null) {
|
||||
MediaType contentTypeToUse = contentType;
|
||||
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
|
||||
if (contentType == null || !contentType.isConcrete()) {
|
||||
contentTypeToUse = getDefaultContentType(t);
|
||||
}
|
||||
else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -102,14 +102,14 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
|||
|
||||
|
||||
@Override
|
||||
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType mediaType) throws IOException {
|
||||
protected void addDefaultHeaders(HttpHeaders headers, String s, @Nullable MediaType type) throws IOException {
|
||||
if (headers.getContentType() == null ) {
|
||||
if (mediaType != null && mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
||||
if (type != null && type.isConcrete() && type.isCompatibleWith(MediaType.APPLICATION_JSON)) {
|
||||
// Prevent charset parameter for JSON..
|
||||
headers.setContentType(mediaType);
|
||||
headers.setContentType(type);
|
||||
}
|
||||
}
|
||||
super.addDefaultHeaders(headers, s, mediaType);
|
||||
super.addDefaultHeaders(headers, s, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -131,4 +131,16 @@ public class StringHttpMessageConverterTests {
|
|||
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test // gh-24283
|
||||
public void writeWithWildCardMediaType() throws IOException {
|
||||
String body = "Hello World";
|
||||
this.converter.write(body, MediaType.ALL, this.outputMessage);
|
||||
|
||||
HttpHeaders headers = this.outputMessage.getHeaders();
|
||||
assertThat(this.outputMessage.getBodyAsString(StandardCharsets.US_ASCII)).isEqualTo(body);
|
||||
assertThat(headers.getContentType()).isEqualTo(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
|
||||
assertThat(headers.getContentLength()).isEqualTo(body.getBytes().length);
|
||||
assertThat(headers.getAcceptCharset().isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue