mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-01 16:39:11 +00:00
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:
parent
82e9a4e80a
commit
a5866e226e
@ -33,6 +33,7 @@ import org.elasticsearch.search.lookup.SearchLookup;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -163,7 +164,7 @@ public class MustacheScriptEngineService extends AbstractComponent implements Sc
|
|||||||
public MustacheExecutableScript(Mustache mustache,
|
public MustacheExecutableScript(Mustache mustache,
|
||||||
Map<String, Object> vars) {
|
Map<String, Object> vars) {
|
||||||
this.mustache = mustache;
|
this.mustache = mustache;
|
||||||
this.vars = vars;
|
this.vars = vars == null ? Collections.EMPTY_MAP : vars;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -168,6 +168,28 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|||||||
assertHitCount(searchResponse, 2);
|
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
|
@Test
|
||||||
public void testThatParametersCanBeSet() throws Exception {
|
public void testThatParametersCanBeSet() throws Exception {
|
||||||
index("test", "type", "1", jsonBuilder().startObject().field("theField", "foo").endObject());
|
index("test", "type", "1", jsonBuilder().startObject().field("theField", "foo").endObject());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user