Skip to content

Commit

Permalink
Add property for media types of reactive Mustache views
Browse files Browse the repository at this point in the history
Closes gh-28858
  • Loading branch information
wilkinsona committed Mar 17, 2022
1 parent 3a5a748 commit b787ea4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.http.MediaType;
import org.springframework.util.MimeType;

/**
Expand All @@ -42,6 +44,8 @@ public class MustacheProperties {

private final Servlet servlet = new Servlet();

private final Reactive reactive = new Reactive();

/**
* View names that can be resolved.
*/
Expand Down Expand Up @@ -81,6 +85,10 @@ public Servlet getServlet() {
return this.servlet;
}

public Reactive getReactive() {
return this.reactive;
}

public String getPrefix() {
return this.prefix;
}
Expand Down Expand Up @@ -318,4 +326,21 @@ public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {

}

public static class Reactive {

/**
* Media types supported by Mustache views.
*/
private List<MediaType> mediaTypes;

public List<MediaType> getMediaTypes() {
return this.mediaTypes;
}

public void setMediaTypes(List<MediaType> mediaTypes) {
this.mediaTypes = mediaTypes;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.web.reactive.result.view.MustacheViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -36,11 +37,13 @@ class MustacheReactiveWebConfiguration {
@ConditionalOnProperty(prefix = "spring.mustache", name = "enabled", matchIfMissing = true)
MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler, MustacheProperties mustache) {
MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler);
resolver.setPrefix(mustache.getPrefix());
resolver.setSuffix(mustache.getSuffix());
resolver.setViewNames(mustache.getViewNames());
resolver.setRequestContextAttribute(mustache.getRequestContextAttribute());
resolver.setCharset(mustache.getCharsetName());
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(mustache::getPrefix).to(resolver::setPrefix);
map.from(mustache::getSuffix).to(resolver::setSuffix);
map.from(mustache::getViewNames).to(resolver::setViewNames);
map.from(mustache::getRequestContextAttribute).to(resolver::setRequestContextAttribute);
map.from(mustache::getCharsetName).to(resolver::setCharset);
map.from(mustache.getReactive()::getMediaTypes).to(resolver::setSupportedMediaTypes);
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
return resolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,10 @@
"name": "spring.mustache.prefix",
"defaultValue": "classpath:/templates/"
},
{
"name": "spring.mustache.reactive.media-types",
"defaultValue": "text/html;charset=UTF-8"
},
{
"name": "spring.mustache.suffix",
"defaultValue": ".mustache"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.autoconfigure.mustache;

import java.util.Arrays;
import java.util.function.Supplier;

import com.samskivert.mustache.Mustache;
Expand All @@ -31,6 +32,7 @@
import org.springframework.boot.web.servlet.view.MustacheViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -133,6 +135,8 @@ void defaultReactiveViewResolverConfiguration() {
assertThat(viewResolver).extracting("prefix").isEqualTo("classpath:/templates/");
assertThat(viewResolver).extracting("requestContextAttribute").isNull();
assertThat(viewResolver).extracting("suffix").isEqualTo(".mustache");
assertThat(viewResolver.getSupportedMediaTypes())
.containsExactly(MediaType.parseMediaType("text/html;charset=UTF-8"));
});
}

Expand Down Expand Up @@ -238,6 +242,14 @@ void suffixCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
assertViewResolverProperty(kind, "spring.mustache.suffix=.tache", "suffix", ".tache");
}

@Test
void mediaTypesCanBeCustomizedOnReactiveViewResolver() {
assertViewResolverProperty(ViewResolverKind.REACTIVE,
"spring.mustache.reactive.media-types=text/xml;charset=UTF-8,text/plain;charset=UTF-16", "mediaTypes",
Arrays.asList(MediaType.parseMediaType("text/xml;charset=UTF-8"),
MediaType.parseMediaType("text/plain;charset=UTF-16")));
}

private void assertViewResolverProperty(ViewResolverKind kind, String property, String field,
Object expectedValue) {
kind.runner().withConfiguration(AutoConfigurations.of(MustacheAutoConfiguration.class))
Expand Down

0 comments on commit b787ea4

Please sign in to comment.