Skip to content

Commit ddbecf6

Browse files
committed
Polish 'Fix Mustache to not ignore native fetcher'
See gh-21060
1 parent 5199c11 commit ddbecf6

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,19 @@ public void setEnvironment(Environment environment) {
4343

4444
@Override
4545
public VariableFetcher createFetcher(Object ctx, String name) {
46-
VariableFetcher fetcher = super.createFetcher(ctx, name);
47-
if (fetcher != null) {
48-
return new PropertyVariableFetcher(fetcher);
46+
VariableFetcher nativeFetcher = super.createFetcher(ctx, name);
47+
if (nativeFetcher != null) {
48+
return new PropertyVariableFetcher(nativeFetcher);
4949
}
5050
if (this.environment.containsProperty(name)) {
5151
return new PropertyVariableFetcher();
5252
}
5353
return null;
5454
}
5555

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

5861
private final VariableFetcher nativeFetcher;
@@ -61,29 +64,29 @@ private class PropertyVariableFetcher implements VariableFetcher {
6164
this.nativeFetcher = null;
6265
}
6366

64-
PropertyVariableFetcher(VariableFetcher nativeFetcher) {
65-
this.nativeFetcher = nativeFetcher;
67+
PropertyVariableFetcher(VariableFetcher delegate) {
68+
this.nativeFetcher = delegate;
6669
}
6770

6871
@Override
6972
public Object get(Object ctx, String name) {
70-
Object result;
71-
if (this.nativeFetcher != null) {
72-
try {
73-
result = this.nativeFetcher.get(ctx, name);
74-
if (result != null && result != Template.NO_FETCHER_FOUND) {
75-
return result;
76-
}
77-
}
78-
catch (Exception ex) {
79-
// fall through
80-
}
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;
8182
}
82-
result = MustacheEnvironmentCollector.this.environment.getProperty(name);
83-
if (result == null) {
84-
return Template.NO_FETCHER_FOUND;
83+
catch (Exception ex) {
84+
return null;
8585
}
86-
return result;
86+
}
87+
88+
private Object getFromEnvironment(String name) {
89+
return MustacheEnvironmentCollector.this.environment.getProperty(name);
8790
}
8891

8992
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ void environmentCollectorCompoundKeyStandardMap() {
6868

6969
@Test
7070
void environmentCollectorCompoundKeyWithBean() {
71-
assertThat(this.compiler.compile("Hello: {{foo.name}}")
72-
.execute(Collections.singletonMap("foo", new Foo()))).isEqualTo("Hello: Foo");
71+
assertThat(this.compiler.compile("Hello: {{foo.name}}").execute(Collections.singletonMap("foo", new Foo())))
72+
.isEqualTo("Hello: Foo");
7373
}
7474

7575
@Test
7676
void environmentCollectorCompoundKeyWithBeanPrefersEnvironment() {
77-
assertThat(this.compiler.compile("Hello: {{bar.name}}")
78-
.execute(Collections.singletonMap("bar", new Foo()))).isEqualTo("Hello: Bar");
77+
assertThat(this.compiler.compile("Hello: {{bar.name}}").execute(Collections.singletonMap("bar", new Foo())))
78+
.isEqualTo("Hello: Bar");
7979
}
8080

8181
@Test
@@ -94,17 +94,19 @@ void environmentCollectorSimpleKeyMap() {
9494
static class Application {
9595

9696
}
97-
97+
9898
static class Foo {
99+
99100
private String name = "Foo";
100101

101-
public String getName() {
102-
return name;
102+
String getName() {
103+
return this.name;
103104
}
104105

105-
public void setName(String name) {
106+
void setName(String name) {
106107
this.name = name;
107108
}
109+
108110
}
109111

110112
}

0 commit comments

Comments
 (0)