Skip to content

Commit

Permalink
Apply charset to Mustache's content type
Browse files Browse the repository at this point in the history
Fixes gh-44053
  • Loading branch information
wilkinsona committed Feb 10, 2025
1 parent 89f5bf9 commit ea76e46
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 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.
Expand All @@ -18,7 +18,10 @@

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;
Expand All @@ -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();

Expand Down Expand Up @@ -190,6 +193,16 @@ public static class Servlet {
*/
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;
}
Expand All @@ -215,6 +228,15 @@ public void setCache(boolean cache) {
}

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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 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.
Expand Down Expand Up @@ -117,6 +117,7 @@ void defaultServletViewResolverConfiguration() {
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();
Expand Down Expand Up @@ -161,6 +162,10 @@ void cacheCanBeCustomizedOnServletViewResolver() {
@EnumSource(ViewResolverKind.class)
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
Expand Down

0 comments on commit ea76e46

Please sign in to comment.