Commit ddbecf62 authored by Phillip Webb's avatar Phillip Webb

Polish 'Fix Mustache to not ignore native fetcher'

See gh-21060
parent 5199c11e
...@@ -43,9 +43,9 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements En ...@@ -43,9 +43,9 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements En
@Override @Override
public VariableFetcher createFetcher(Object ctx, String name) { public VariableFetcher createFetcher(Object ctx, String name) {
VariableFetcher fetcher = super.createFetcher(ctx, name); VariableFetcher nativeFetcher = super.createFetcher(ctx, name);
if (fetcher != null) { if (nativeFetcher != null) {
return new PropertyVariableFetcher(fetcher); return new PropertyVariableFetcher(nativeFetcher);
} }
if (this.environment.containsProperty(name)) { if (this.environment.containsProperty(name)) {
return new PropertyVariableFetcher(); return new PropertyVariableFetcher();
...@@ -53,6 +53,9 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements En ...@@ -53,6 +53,9 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements En
return null; return null;
} }
/**
* {@link VariableFetcher} that also checks the {@link Environment}.
*/
private class PropertyVariableFetcher implements VariableFetcher { private class PropertyVariableFetcher implements VariableFetcher {
private final VariableFetcher nativeFetcher; private final VariableFetcher nativeFetcher;
...@@ -61,29 +64,29 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements En ...@@ -61,29 +64,29 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements En
this.nativeFetcher = null; this.nativeFetcher = null;
} }
PropertyVariableFetcher(VariableFetcher nativeFetcher) { PropertyVariableFetcher(VariableFetcher delegate) {
this.nativeFetcher = nativeFetcher; this.nativeFetcher = delegate;
} }
@Override @Override
public Object get(Object ctx, String name) { public Object get(Object ctx, String name) {
Object result; Object result = getFromNativeFetcher(ctx, name);
if (this.nativeFetcher != null) { result = (result != null) ? result : getFromEnvironment(name);
try { return (result != null) ? result : Template.NO_FETCHER_FOUND;
result = this.nativeFetcher.get(ctx, name); }
if (result != null && result != Template.NO_FETCHER_FOUND) {
return result; private Object getFromNativeFetcher(Object ctx, String name) {
} try {
} Object result = (this.nativeFetcher != null) ? this.nativeFetcher.get(ctx, name) : null;
catch (Exception ex) { return (result != Template.NO_FETCHER_FOUND) ? result : null;
// fall through
}
} }
result = MustacheEnvironmentCollector.this.environment.getProperty(name); catch (Exception ex) {
if (result == null) { return null;
return Template.NO_FETCHER_FOUND;
} }
return result; }
private Object getFromEnvironment(String name) {
return MustacheEnvironmentCollector.this.environment.getProperty(name);
} }
} }
......
...@@ -68,14 +68,14 @@ class MustacheStandaloneIntegrationTests { ...@@ -68,14 +68,14 @@ class MustacheStandaloneIntegrationTests {
@Test @Test
void environmentCollectorCompoundKeyWithBean() { void environmentCollectorCompoundKeyWithBean() {
assertThat(this.compiler.compile("Hello: {{foo.name}}") assertThat(this.compiler.compile("Hello: {{foo.name}}").execute(Collections.singletonMap("foo", new Foo())))
.execute(Collections.singletonMap("foo", new Foo()))).isEqualTo("Hello: Foo"); .isEqualTo("Hello: Foo");
} }
@Test @Test
void environmentCollectorCompoundKeyWithBeanPrefersEnvironment() { void environmentCollectorCompoundKeyWithBeanPrefersEnvironment() {
assertThat(this.compiler.compile("Hello: {{bar.name}}") assertThat(this.compiler.compile("Hello: {{bar.name}}").execute(Collections.singletonMap("bar", new Foo())))
.execute(Collections.singletonMap("bar", new Foo()))).isEqualTo("Hello: Bar"); .isEqualTo("Hello: Bar");
} }
@Test @Test
...@@ -94,17 +94,19 @@ class MustacheStandaloneIntegrationTests { ...@@ -94,17 +94,19 @@ class MustacheStandaloneIntegrationTests {
static class Application { static class Application {
} }
static class Foo { static class Foo {
private String name = "Foo"; private String name = "Foo";
public String getName() { String getName() {
return name; return this.name;
} }
public void setName(String name) { void setName(String name) {
this.name = name; this.name = name;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment