Skip to content

Commit ea76e46

Browse files
committed
Apply charset to Mustache's content type
Fixes gh-44053
1 parent 89f5bf9 commit ea76e46

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheProperties.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -18,7 +18,10 @@
1818

1919
import java.nio.charset.Charset;
2020
import java.nio.charset.StandardCharsets;
21+
import java.util.LinkedHashMap;
2122
import java.util.List;
23+
import java.util.Map;
24+
import java.util.function.Supplier;
2225

2326
import org.springframework.boot.context.properties.ConfigurationProperties;
2427
import org.springframework.http.MediaType;
@@ -41,7 +44,7 @@ public class MustacheProperties {
4144

4245
public static final String DEFAULT_SUFFIX = ".mustache";
4346

44-
private final Servlet servlet = new Servlet();
47+
private final Servlet servlet = new Servlet(this::getCharset);
4548

4649
private final Reactive reactive = new Reactive();
4750

@@ -190,6 +193,16 @@ public static class Servlet {
190193
*/
191194
private boolean exposeSpringMacroHelpers = true;
192195

196+
private final Supplier<Charset> charset;
197+
198+
public Servlet() {
199+
this.charset = () -> null;
200+
}
201+
202+
private Servlet(Supplier<Charset> charset) {
203+
this.charset = charset;
204+
}
205+
193206
public boolean isAllowRequestOverride() {
194207
return this.allowRequestOverride;
195208
}
@@ -215,6 +228,15 @@ public void setCache(boolean cache) {
215228
}
216229

217230
public MimeType getContentType() {
231+
if (this.contentType != null && this.contentType.getCharset() == null) {
232+
Charset charset = this.charset.get();
233+
if (charset != null) {
234+
Map<String, String> parameters = new LinkedHashMap<>();
235+
parameters.put("charset", charset.name());
236+
parameters.putAll(this.contentType.getParameters());
237+
return new MimeType(this.contentType, parameters);
238+
}
239+
}
218240
return this.contentType;
219241
}
220242

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfigurationTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -117,6 +117,7 @@ void defaultServletViewResolverConfiguration() {
117117
assertThat(viewResolver).extracting("allowSessionOverride", InstanceOfAssertFactories.BOOLEAN).isFalse();
118118
assertThat(viewResolver).extracting("cache", InstanceOfAssertFactories.BOOLEAN).isFalse();
119119
assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8");
120+
assertThat(viewResolver).extracting("contentType").isEqualTo("text/html;charset=UTF-8");
120121
assertThat(viewResolver).extracting("exposeRequestAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
121122
assertThat(viewResolver).extracting("exposeSessionAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
122123
assertThat(viewResolver).extracting("exposeSpringMacroHelpers", InstanceOfAssertFactories.BOOLEAN).isTrue();
@@ -161,6 +162,10 @@ void cacheCanBeCustomizedOnServletViewResolver() {
161162
@EnumSource(ViewResolverKind.class)
162163
void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
163164
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16");
165+
if (kind == ViewResolverKind.SERVLET) {
166+
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "contentType",
167+
"text/html;charset=UTF-16");
168+
}
164169
}
165170

166171
@Test

0 commit comments

Comments
 (0)