Mustache: Ensure internal scope extrators are always operating on a Map

Mustache extracts the key/value pairs for parameter substitution from
objects and maps but it's decided on the first execution. We need to
make sure if the params are null we pass an empty map to ensure we
bind the map based extractor

Closes #6318
This commit is contained in:
Simon Willnauer 2014-05-28 12:20:20 +02:00
parent 82e9a4e80a
commit a5866e226e
2 changed files with 24 additions and 1 deletions

View File

@ -33,6 +33,7 @@ import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.Map;
/**
@ -163,7 +164,7 @@ public class MustacheScriptEngineService extends AbstractComponent implements Sc
public MustacheExecutableScript(Mustache mustache,
Map<String, Object> vars) {
this.mustache = mustache;
this.vars = vars;
this.vars = vars == null ? Collections.EMPTY_MAP : vars;
}
@Override

View File

@ -168,6 +168,28 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
assertHitCount(searchResponse, 2);
}
@Test
// Releates to #6318
public void testSearchRequestFail() throws Exception {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("_all");
try {
String query = "{ \"template\" : { \"query\": {\"match_all\": {}}, \"size\" : \"{{my_size}}\" } }";
BytesReference bytesRef = new BytesArray(query);
searchRequest.templateSource(bytesRef, false);
client().search(searchRequest).get();
fail("expected exception");
} catch (Throwable ex) {
// expected - no params
}
String query = "{ \"template\" : { \"query\": {\"match_all\": {}}, \"size\" : \"{{my_size}}\" }, \"params\" : { \"my_size\": 1 } }";
BytesReference bytesRef = new BytesArray(query);
searchRequest.templateSource(bytesRef, false);
SearchResponse searchResponse = client().search(searchRequest).get();
assertThat(searchResponse.getHits().hits().length, equalTo(1));
}
@Test
public void testThatParametersCanBeSet() throws Exception {
index("test", "type", "1", jsonBuilder().startObject().field("theField", "foo").endObject());