Skip to content

Commit

Permalink
Mustache: Ensure internal scope extrators are always operating on a Map
Browse files Browse the repository at this point in the history
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 elastic#6318
  • Loading branch information
s1monw committed May 28, 2014
1 parent 82e9a4e commit a5866e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Expand Up @@ -33,6 +33,7 @@

import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.Map;

/**
Expand Down Expand Up @@ -163,7 +164,7 @@ private class MustacheExecutableScript implements ExecutableScript {
public MustacheExecutableScript(Mustache mustache,
Map<String, Object> vars) {
this.mustache = mustache;
this.vars = vars;
this.vars = vars == null ? Collections.EMPTY_MAP : vars;
}

@Override
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java
Expand Up @@ -168,6 +168,28 @@ public void testSearchRequestTemplateSource() throws Exception {
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());
Expand Down

0 comments on commit a5866e2

Please sign in to comment.