Merge branch '3.4.x'

Closes gh-44194
This commit is contained in:
Andy Wilkinson 2025-02-10 11:09:38 +00:00
commit 3137f20b4a
2 changed files with 29 additions and 2 deletions

View File

@ -18,7 +18,10 @@ package org.springframework.boot.autoconfigure.mustache;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
@ -41,7 +44,7 @@ public class MustacheProperties {
public static final String DEFAULT_SUFFIX = ".mustache";
private final Servlet servlet = new Servlet();
private final Servlet servlet = new Servlet(this::getCharset);
private final Reactive reactive = new Reactive();
@ -190,6 +193,16 @@ public class MustacheProperties {
*/
private boolean exposeSpringMacroHelpers = true;
private final Supplier<Charset> charset;
public Servlet() {
this.charset = () -> null;
}
private Servlet(Supplier<Charset> charset) {
this.charset = charset;
}
public boolean isAllowRequestOverride() {
return this.allowRequestOverride;
}
@ -215,6 +228,15 @@ public class MustacheProperties {
}
public MimeType getContentType() {
if (this.contentType != null && this.contentType.getCharset() == null) {
Charset charset = this.charset.get();
if (charset != null) {
Map<String, String> parameters = new LinkedHashMap<>();
parameters.put("charset", charset.name());
parameters.putAll(this.contentType.getParameters());
return new MimeType(this.contentType, parameters);
}
}
return this.contentType;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@ -117,6 +117,7 @@ class MustacheAutoConfigurationTests {
assertThat(viewResolver).extracting("allowSessionOverride", InstanceOfAssertFactories.BOOLEAN).isFalse();
assertThat(viewResolver).extracting("cache", InstanceOfAssertFactories.BOOLEAN).isFalse();
assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8");
assertThat(viewResolver).extracting("contentType").isEqualTo("text/html;charset=UTF-8");
assertThat(viewResolver).extracting("exposeRequestAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
assertThat(viewResolver).extracting("exposeSessionAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
assertThat(viewResolver).extracting("exposeSpringMacroHelpers", InstanceOfAssertFactories.BOOLEAN).isTrue();
@ -161,6 +162,10 @@ class MustacheAutoConfigurationTests {
@EnumSource
void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16");
if (kind == ViewResolverKind.SERVLET) {
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "contentType",
"text/html;charset=UTF-16");
}
}
@Test