Skip to content

Commit

Permalink
Make MustacheViewResolvers conditional onspring.mustache.enabled
Browse files Browse the repository at this point in the history
Closes gh-30250
  • Loading branch information
wilkinsona committed Mar 16, 2022
1 parent b97a3ae commit 7f036e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 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 @@ -19,6 +19,7 @@
import com.samskivert.mustache.Mustache.Compiler;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.web.reactive.result.view.MustacheViewResolver;
Expand All @@ -32,6 +33,7 @@ class MustacheReactiveWebConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mustache", name = "enabled", matchIfMissing = true)
MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler, MustacheProperties mustache) {
MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler);
resolver.setPrefix(mustache.getPrefix());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 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 @@ -19,6 +19,7 @@
import com.samskivert.mustache.Mustache.Compiler;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.web.servlet.view.MustacheViewResolver;
Expand All @@ -32,6 +33,7 @@ class MustacheServletWebConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mustache", name = "enabled", matchIfMissing = true)
MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler, MustacheProperties mustache) {
MustacheViewResolver resolver = new MustacheViewResolver(mustacheCompiler);
mustache.applyToMvcViewResolver(resolver);
Expand Down
Expand Up @@ -46,6 +46,16 @@ void registerBeansForServletApp() {
});
}

@Test
void servletViewResolverCanBeDisabled() {
configure(new WebApplicationContextRunner()).withPropertyValues("spring.mustache.enabled=false")
.run((context) -> {
assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(context).doesNotHaveBean(MustacheViewResolver.class);
});
}

@Test
void registerCompilerForServletApp() {
configure(new WebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class)
Expand All @@ -68,6 +78,17 @@ void registerBeansForReactiveApp() {
});
}

@Test
void reactiveViewResolverCanBeDisabled() {
configure(new ReactiveWebApplicationContextRunner()).withPropertyValues("spring.mustache.enabled=false")
.run((context) -> {
assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(context).doesNotHaveBean(
org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class);
});
}

@Test
void registerCompilerForReactiveApp() {
configure(new ReactiveWebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class)
Expand Down

0 comments on commit 7f036e3

Please sign in to comment.