Skip to content

Commit 6547ea5

Browse files
committed
Merge pull request #21060 from dsyer
* pr/21060: Polish 'Fix Mustache to not ignore native fetcher' Fix Mustache to not ignore native fetcher Closes gh-21060
2 parents e9e4a34 + ddbecf6 commit 6547ea5

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.samskivert.mustache.DefaultCollector;
2020
import com.samskivert.mustache.Mustache.Collector;
2121
import com.samskivert.mustache.Mustache.VariableFetcher;
22+
import com.samskivert.mustache.Template;
2223

2324
import org.springframework.context.EnvironmentAware;
2425
import org.springframework.core.env.ConfigurableEnvironment;
@@ -35,29 +36,56 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements En
3536

3637
private ConfigurableEnvironment environment;
3738

38-
private final VariableFetcher propertyFetcher = new PropertyVariableFetcher();
39-
4039
@Override
4140
public void setEnvironment(Environment environment) {
4241
this.environment = (ConfigurableEnvironment) environment;
4342
}
4443

4544
@Override
4645
public VariableFetcher createFetcher(Object ctx, String name) {
47-
VariableFetcher fetcher = super.createFetcher(ctx, name);
48-
if (fetcher != null) {
49-
return fetcher;
46+
VariableFetcher nativeFetcher = super.createFetcher(ctx, name);
47+
if (nativeFetcher != null) {
48+
return new PropertyVariableFetcher(nativeFetcher);
5049
}
5150
if (this.environment.containsProperty(name)) {
52-
return this.propertyFetcher;
51+
return new PropertyVariableFetcher();
5352
}
5453
return null;
5554
}
5655

56+
/**
57+
* {@link VariableFetcher} that also checks the {@link Environment}.
58+
*/
5759
private class PropertyVariableFetcher implements VariableFetcher {
5860

61+
private final VariableFetcher nativeFetcher;
62+
63+
PropertyVariableFetcher() {
64+
this.nativeFetcher = null;
65+
}
66+
67+
PropertyVariableFetcher(VariableFetcher delegate) {
68+
this.nativeFetcher = delegate;
69+
}
70+
5971
@Override
6072
public Object get(Object ctx, String name) {
73+
Object result = getFromNativeFetcher(ctx, name);
74+
result = (result != null) ? result : getFromEnvironment(name);
75+
return (result != null) ? result : Template.NO_FETCHER_FOUND;
76+
}
77+
78+
private Object getFromNativeFetcher(Object ctx, String name) {
79+
try {
80+
Object result = (this.nativeFetcher != null) ? this.nativeFetcher.get(ctx, name) : null;
81+
return (result != Template.NO_FETCHER_FOUND) ? result : null;
82+
}
83+
catch (Exception ex) {
84+
return null;
85+
}
86+
}
87+
88+
private Object getFromEnvironment(String name) {
6189
return MustacheEnvironmentCollector.this.environment.getProperty(name);
6290
}
6391

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @author Dave Syer
3838
*/
3939
@DirtiesContext
40-
@SpringBootTest(webEnvironment = WebEnvironment.NONE, properties = { "env.FOO=There", "foo=World" })
40+
@SpringBootTest(webEnvironment = WebEnvironment.NONE, properties = { "env.FOO=There", "foo=World", "bar.name=Bar" })
4141
class MustacheStandaloneIntegrationTests {
4242

4343
@Autowired
@@ -60,15 +60,53 @@ void environmentCollectorCompoundKeyStandard() {
6060
.isEqualTo("Hello: There");
6161
}
6262

63+
@Test
64+
void environmentCollectorCompoundKeyStandardMap() {
65+
assertThat(this.compiler.standardsMode(true).compile("Hello: {{env.foo}}")
66+
.execute(Collections.singletonMap("world", "World"))).isEqualTo("Hello: There");
67+
}
68+
69+
@Test
70+
void environmentCollectorCompoundKeyWithBean() {
71+
assertThat(this.compiler.compile("Hello: {{foo.name}}").execute(Collections.singletonMap("foo", new Foo())))
72+
.isEqualTo("Hello: Foo");
73+
}
74+
75+
@Test
76+
void environmentCollectorCompoundKeyWithBeanPrefersEnvironment() {
77+
assertThat(this.compiler.compile("Hello: {{bar.name}}").execute(Collections.singletonMap("bar", new Foo())))
78+
.isEqualTo("Hello: Bar");
79+
}
80+
6381
@Test
6482
void environmentCollectorSimpleKey() {
6583
assertThat(this.compiler.compile("Hello: {{foo}}").execute(new Object())).isEqualTo("Hello: World");
6684
}
6785

86+
@Test
87+
void environmentCollectorSimpleKeyMap() {
88+
assertThat(this.compiler.compile("Hello: {{foo}}").execute(Collections.singletonMap("world", "Foo")))
89+
.isEqualTo("Hello: World");
90+
}
91+
6892
@Configuration(proxyBeanMethods = false)
6993
@Import({ MustacheAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
7094
static class Application {
7195

7296
}
7397

98+
static class Foo {
99+
100+
private String name = "Foo";
101+
102+
String getName() {
103+
return this.name;
104+
}
105+
106+
void setName(String name) {
107+
this.name = name;
108+
}
109+
110+
}
111+
74112
}

0 commit comments

Comments
 (0)