Switch writeAcceptCharset to false by default

Closes gh-22506
This commit is contained in:
Rossen Stoyanchev 2019-05-08 09:56:52 -04:00
parent a9f0eae4ab
commit 7e6e3d7027
9 changed files with 14 additions and 30 deletions

View File

@ -117,11 +117,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
this.supportedMediaTypes.add(MediaType.MULTIPART_FORM_DATA);
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.partConverters.add(new ByteArrayHttpMessageConverter());
this.partConverters.add(stringHttpMessageConverter);
this.partConverters.add(new StringHttpMessageConverter());
this.partConverters.add(new ResourceHttpMessageConverter());
applyDefaultCharset();

View File

@ -81,8 +81,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
/**
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
* <p>Default is {@code true}.
* Delegates to {@link StringHttpMessageConverter#setWriteAcceptCharset(boolean)}.
*/
public void setWriteAcceptCharset(boolean writeAcceptCharset) {
this.stringHttpMessageConverter.setWriteAcceptCharset(writeAcceptCharset);

View File

@ -52,7 +52,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
@Nullable
private volatile List<Charset> availableCharsets;
private boolean writeAcceptCharset = true;
private boolean writeAcceptCharset = false;
/**
@ -76,7 +76,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
* Whether the {@code Accept-Charset} header should be written to any outgoing
* request sourced from the value of {@link Charset#availableCharsets()}.
* The behavior is suppressed if the header has already been set.
* <p>Default is {@code true}.
* <p>As of 5.2, by default is set to {@code false}.
*/
public void setWriteAcceptCharset(boolean writeAcceptCharset) {
this.writeAcceptCharset = writeAcceptCharset;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@ -107,6 +107,7 @@ public class ObjectToStringHttpMessageConverterTests {
@Test
public void writeAcceptCharset() throws IOException {
this.converter.setWriteAcceptCharset(true);
this.converter.write(new Date(), null, this.response);
assertNotNull(this.servletResponse.getHeader("Accept-Charset"));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@ -79,7 +79,7 @@ public class StringHttpMessageConverterTests {
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1));
assertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType());
assertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength());
assertFalse(headers.getAcceptCharset().isEmpty());
assertTrue(headers.getAcceptCharset().isEmpty());
}
@Test
@ -91,7 +91,7 @@ public class StringHttpMessageConverterTests {
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8));
assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
assertFalse(headers.getAcceptCharset().isEmpty());
assertTrue(headers.getAcceptCharset().isEmpty());
}
@Test // SPR-8867
@ -106,7 +106,7 @@ public class StringHttpMessageConverterTests {
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8));
assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
assertFalse(headers.getAcceptCharset().isEmpty());
assertTrue(headers.getAcceptCharset().isEmpty());
}
}

View File

@ -831,11 +831,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* @param messageConverters the list to add the default message converters to
*/
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(stringHttpMessageConverter);
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new ResourceRegionHttpMessageConverter());
try {

View File

@ -149,12 +149,8 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini
*/
private void initMessageConverters() {
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(4);
messageConverters.add(new ByteArrayHttpMessageConverter());
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
messageConverters.add(stringHttpMessageConverter);
messageConverters.add(new StringHttpMessageConverter());
try {
messageConverters.add(new SourceHttpMessageConverter<>());

View File

@ -105,12 +105,9 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
public ExceptionHandlerExceptionResolver() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.messageConverters = new ArrayList<>();
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(stringHttpMessageConverter);
this.messageConverters.add(new StringHttpMessageConverter());
try {
this.messageConverters.add(new SourceHttpMessageConverter<>());
}

View File

@ -193,12 +193,9 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
public RequestMappingHandlerAdapter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.messageConverters = new ArrayList<>(4);
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(stringHttpMessageConverter);
this.messageConverters.add(new StringHttpMessageConverter());
try {
this.messageConverters.add(new SourceHttpMessageConverter<>());
}