diff --git a/docs/reference/migration/migrate_7_0/api.asciidoc b/docs/reference/migration/migrate_7_0/api.asciidoc index 3d824c600648f..f7b6f9b2e00a9 100644 --- a/docs/reference/migration/migrate_7_0/api.asciidoc +++ b/docs/reference/migration/migrate_7_0/api.asciidoc @@ -75,3 +75,7 @@ will be for such settings to be copied on such operations. To enable users in `copy_settings` parameter was added on the REST layer. As this behavior will be the only behavior in 8.0.0, this parameter is deprecated in 7.0.0 for removal in 8.0.0. + +==== The deprecated stored script contexts have now been removed +When putting stored scripts, support for storing them with the deprecated `template` context or without a context is +now removed. Scripts must be stored using the `script` context as mentioned in the documentation. diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java index 884e26e7df855..0fbc3fa16afd2 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java @@ -23,7 +23,6 @@ import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.plugins.Plugin; @@ -152,25 +151,22 @@ public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws public void testIndexedTemplateClient() throws Exception { assertAcked(client().admin().cluster().preparePutStoredScript() .setId("testTemplate") - .setContent(new BytesArray("{" + - "\"template\":{" + - " \"query\":{" + - " \"match\":{" + - " \"theField\" : \"{{fieldParam}}\"}" + - " }" + - "}" + - "}"), XContentType.JSON)); - - - assertAcked(client().admin().cluster().preparePutStoredScript() - .setId("testTemplate").setContent(new BytesArray("{" + - "\"template\":{" + - " \"query\":{" + - " \"match\":{" + - " \"theField\" : \"{{fieldParam}}\"}" + - " }" + - "}" + - "}"), XContentType.JSON)); + .setContent( + new BytesArray( + "{" + + " \"script\": {" + + " \"lang\": \"mustache\"," + + " \"source\": {" + + " \"query\": {" + + " \"match\": {" + + " \"theField\": \"{{fieldParam}}\"" + + " }" + + " }" + + " }" + + " }" + + "}" + ), + XContentType.JSON)); GetStoredScriptResponse getResponse = client().admin().cluster() .prepareGetStoredScript("testTemplate").get(); @@ -198,41 +194,32 @@ public void testIndexedTemplateClient() throws Exception { getResponse = client().admin().cluster().prepareGetStoredScript("testTemplate").get(); assertNull(getResponse.getSource()); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } public void testIndexedTemplate() throws Exception { - assertAcked(client().admin().cluster().preparePutStoredScript() - .setId("1a") - .setContent(new BytesArray("{" + - "\"template\":{" + - " \"query\":{" + - " \"match\":{" + - " \"theField\" : \"{{fieldParam}}\"}" + - " }" + - "}" + - "}" - ), XContentType.JSON) + + String script = + "{" + + " \"script\": {" + + " \"lang\": \"mustache\"," + + " \"source\": {" + + " \"query\": {" + + " \"match\": {" + + " \"theField\": \"{{fieldParam}}\"" + + " }" + + " }" + + " }" + + " }" + + "}"; + + assertAcked( + client().admin().cluster().preparePutStoredScript().setId("1a").setContent(new BytesArray(script), XContentType.JSON) ); - assertAcked(client().admin().cluster().preparePutStoredScript() - .setId("2") - .setContent(new BytesArray("{" + - "\"template\":{" + - " \"query\":{" + - " \"match\":{" + - " \"theField\" : \"{{fieldParam}}\"}" + - " }" + - "}" + - "}"), XContentType.JSON) + assertAcked( + client().admin().cluster().preparePutStoredScript().setId("2").setContent(new BytesArray(script), XContentType.JSON) ); - assertAcked(client().admin().cluster().preparePutStoredScript() - .setId("3") - .setContent(new BytesArray("{" + - "\"template\":{" + - " \"match\":{" + - " \"theField\" : \"{{fieldParam}}\"}" + - " }" + - "}"), XContentType.JSON) + assertAcked( + client().admin().cluster().preparePutStoredScript().setId("3").setContent(new BytesArray(script), XContentType.JSON) ); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); @@ -268,7 +255,6 @@ public void testIndexedTemplate() throws Exception { .setScript("2").setScriptType(ScriptType.STORED).setScriptParams(templateParams) .get(); assertHitCount(searchResponse.getResponse(), 1); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } // Relates to #10397 @@ -282,13 +268,27 @@ public void testIndexedTemplateOverwrite() throws Exception { client().admin().indices().prepareRefresh().get(); int iterations = randomIntBetween(2, 11); + String query = + "{" + + " \"script\": {" + + " \"lang\": \"mustache\"," + + " \"source\": {" + + " \"query\": {" + + " \"match_phrase_prefix\": {" + + " \"searchtext\": {" + + " \"query\": \"{{P_Keyword1}}\"," + + " \"slop\": {{slop}}" + + " }" + + " }" + + " }" + + " }" + + " }" + + "}"; for (int i = 1; i < iterations; i++) { assertAcked(client().admin().cluster().preparePutStoredScript() .setId("git01") - .setContent(new BytesArray( - "{\"template\":{\"query\": {\"match_phrase_prefix\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," - + "\"slop\": -1}}}}}"), - XContentType.JSON)); + .setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(-1))), XContentType.JSON) + ); GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript("git01").get(); assertNotNull(getResponse.getSource()); @@ -304,8 +304,8 @@ public void testIndexedTemplateOverwrite() throws Exception { assertAcked(client().admin().cluster().preparePutStoredScript() .setId("git01") - .setContent(new BytesArray("{\"query\": {\"match_phrase_prefix\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + - "\"slop\": 0}}}}"), XContentType.JSON)); + .setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(0))), XContentType.JSON) + ); SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()) .setRequest(new SearchRequest("testindex").types("test")) @@ -313,16 +313,30 @@ public void testIndexedTemplateOverwrite() throws Exception { .get(); assertHitCount(searchResponse.getResponse(), 1); } - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } public void testIndexedTemplateWithArray() throws Exception { - String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}"; + String multiQuery = + "{\n" + + " \"script\": {\n" + + " \"lang\": \"mustache\",\n" + + " \"source\": {\n" + + " \"query\": {\n" + + " \"terms\": {\n" + + " \"theField\": [\n" + + " \"{{#fieldParam}}\",\n" + + " \"{{.}}\",\n" + + " \"{{/fieldParam}}\"\n" + + " ]\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; assertAcked( client().admin().cluster().preparePutStoredScript() .setId("4") - .setContent(BytesReference.bytes(jsonBuilder().startObject().field("template", multiQuery).endObject()), - XContentType.JSON) + .setContent(new BytesArray(multiQuery), XContentType.JSON) ); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON)); @@ -342,7 +356,6 @@ public void testIndexedTemplateWithArray() throws Exception { .setScript("4").setScriptType(ScriptType.STORED).setScriptParams(arrayTemplateParams) .get(); assertHitCount(searchResponse.getResponse(), 5); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java index 4cf686b9c282c..742fb1a74eab4 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java @@ -114,11 +114,7 @@ public void readFrom(StreamInput in) throws IOException { super.readFrom(in); if (in.readBoolean()) { - if (in.getVersion().onOrAfter(Version.V_5_3_0)) { - source = new StoredScriptSource(in); - } else { - source = new StoredScriptSource(in.readString()); - } + source = new StoredScriptSource(in); } else { source = null; } @@ -136,12 +132,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(false); } else { out.writeBoolean(true); - - if (out.getVersion().onOrAfter(Version.V_5_3_0)) { - source.writeTo(out); - } else { - out.writeString(source.getSource()); - } + source.writeTo(out); } if (out.getVersion().onOrAfter(Version.V_6_4_0)) { out.writeString(id); diff --git a/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java b/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java index 885d72bdec6f5..aabef751fc76f 100644 --- a/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java +++ b/server/src/main/java/org/elasticsearch/script/StoredScriptSource.java @@ -19,15 +19,12 @@ package org.elasticsearch.script; -import org.elasticsearch.Version; -import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse; import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.Diff; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -69,16 +66,6 @@ public class StoredScriptSource extends AbstractDiffable imp */ public static final ParseField SCRIPT_PARSE_FIELD = new ParseField("script"); - /** - * Standard {@link ParseField} for outer level of stored script source. - */ - public static final ParseField TEMPLATE_PARSE_FIELD = new ParseField("template"); - - /** - * Standard {@link ParseField} for query on the inner field. - */ - public static final ParseField TEMPLATE_NO_WRAPPER_PARSE_FIELD = new ParseField("query"); - /** * Standard {@link ParseField} for lang on the inner level. */ @@ -194,26 +181,6 @@ private StoredScriptSource build(boolean ignoreEmpty) { PARSER.declareField(Builder::setOptions, XContentParser::mapStrings, OPTIONS_PARSE_FIELD, ValueType.OBJECT); } - private static StoredScriptSource parseRemaining(Token token, XContentParser parser) throws IOException { - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - if (token != Token.START_OBJECT) { - builder.startObject(); - builder.copyCurrentStructure(parser); - builder.endObject(); - } else { - builder.copyCurrentStructure(parser); - } - - String source = Strings.toString(builder); - - if (source == null || source.isEmpty()) { - DEPRECATION_LOGGER.deprecated("empty templates should no longer be used"); - } - - return new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, source, Collections.emptyMap()); - } - } - /** * This will parse XContent into a {@link StoredScriptSource}. The following formats can be parsed: * @@ -271,27 +238,8 @@ private static StoredScriptSource parseRemaining(Token token, XContentParser par * } * } * - * The simple template format: - * - * {@code - * { - * "query" : ... - * } - * } - * - * The complex template format: - * - * {@code - * { - * "template": { - * "query" : ... - * } - * } - * } - * - * Note that templates can be handled as both strings and complex JSON objects. - * Also templates may be part of the 'source' parameter in a script. The Parser - * can handle this case as well. + * Note that the "source" parameter can also handle template parsing including from + * a complex JSON object. * * @param content The content from the request to be parsed as described above. * @return The parsed {@link StoredScriptSource}. @@ -316,7 +264,7 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon if (token != Token.FIELD_NAME) { throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + ", expected [" + - SCRIPT_PARSE_FIELD.getPreferredName() + ", " + TEMPLATE_PARSE_FIELD.getPreferredName()); + SCRIPT_PARSE_FIELD.getPreferredName() + "]"); } String name = parser.currentName(); @@ -329,28 +277,9 @@ public static StoredScriptSource parse(BytesReference content, XContentType xCon } else { throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{, ]"); } - } else if (TEMPLATE_PARSE_FIELD.getPreferredName().equals(name)) { - - DEPRECATION_LOGGER.deprecated("the template context is now deprecated. Specify templates in a \"script\" element."); - - token = parser.nextToken(); - if (token == Token.VALUE_STRING) { - String source = parser.text(); - - if (source == null || source.isEmpty()) { - DEPRECATION_LOGGER.deprecated("empty templates should no longer be used"); - } - - return new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, source, Collections.emptyMap()); - } else { - return parseRemaining(token, parser); - } - } else if (TEMPLATE_NO_WRAPPER_PARSE_FIELD.getPreferredName().equals(name)) { - DEPRECATION_LOGGER.deprecated("the template context is now deprecated. Specify templates in a \"script\" element."); - return parseRemaining(token, parser); } else { - DEPRECATION_LOGGER.deprecated("scripts should not be stored without a context. Specify them in a \"script\" element."); - return parseRemaining(token, parser); + throw new ParsingException(parser.getTokenLocation(), "unexpected field [" + name + "], expected [" + + SCRIPT_PARSE_FIELD.getPreferredName() + "]"); } } catch (IOException ioe) { throw new UncheckedIOException(ioe); @@ -397,16 +326,6 @@ public static Diff readDiffFrom(StreamInput in) throws IOExc private final String source; private final Map options; - /** - * Constructor for use with {@link GetStoredScriptResponse} - * to support the deprecated stored script namespace. - */ - public StoredScriptSource(String source) { - this.lang = null; - this.source = Objects.requireNonNull(source); - this.options = null; - } - /** * Standard StoredScriptSource constructor. * @param lang The language to compile the script with. Must not be {@code null}. @@ -426,35 +345,24 @@ public StoredScriptSource(String lang, String source, Map option * only the source parameter will be read in as a bytes reference. */ public StoredScriptSource(StreamInput in) throws IOException { - if (in.getVersion().onOrAfter(Version.V_5_3_0)) { - this.lang = in.readString(); - this.source = in.readString(); - @SuppressWarnings("unchecked") - Map options = (Map)(Map)in.readMap(); - this.options = options; - } else { - this.lang = null; - this.source = in.readBytesReference().utf8ToString(); - this.options = null; - } + this.lang = in.readString(); + this.source = in.readString(); + @SuppressWarnings("unchecked") + Map options = (Map)(Map)in.readMap(); + this.options = options; } /** - * Writes a {@link StoredScriptSource} to a stream. Version 5.3+ will write - * all of the lang, source, and options parameters. For versions prior to 5.3, - * only the source parameter will be read in as a bytes reference. + * Writes a {@link StoredScriptSource} to a stream. Will write + * all of the lang, source, and options parameters. */ @Override public void writeTo(StreamOutput out) throws IOException { - if (out.getVersion().onOrAfter(Version.V_5_3_0)) { - out.writeString(lang); - out.writeString(source); - @SuppressWarnings("unchecked") - Map options = (Map)(Map)this.options; - out.writeMap(options); - } else { - out.writeBytesReference(new BytesArray(source)); - } + out.writeString(lang); + out.writeString(source); + @SuppressWarnings("unchecked") + Map options = (Map)(Map)this.options; + out.writeMap(options); } /** diff --git a/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java b/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java index 6a17556a1035b..7a856ee13b9d3 100644 --- a/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java +++ b/server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java @@ -77,14 +77,12 @@ public void testGetScript() throws Exception { ScriptMetaData.Builder builder = new ScriptMetaData.Builder(null); XContentBuilder sourceBuilder = XContentFactory.jsonBuilder(); - sourceBuilder.startObject().startObject("template").field("field", "value").endObject().endObject(); - builder.storeScript("template", StoredScriptSource.parse(BytesReference.bytes(sourceBuilder), sourceBuilder.contentType())); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); - - sourceBuilder = XContentFactory.jsonBuilder(); - sourceBuilder.startObject().field("template", "value").endObject(); - builder.storeScript("template_field", StoredScriptSource.parse(BytesReference.bytes(sourceBuilder), sourceBuilder.contentType())); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); + sourceBuilder.startObject().startObject("script") + .field("lang", "_lang") + .startObject("source").field("field", "value").endObject() + .endObject().endObject(); + builder.storeScript("source_template", StoredScriptSource.parse(BytesReference.bytes(sourceBuilder), + sourceBuilder.contentType())); sourceBuilder = XContentFactory.jsonBuilder(); sourceBuilder.startObject().startObject("script").field("lang", "_lang").field("source", "_source").endObject().endObject(); @@ -92,26 +90,25 @@ public void testGetScript() throws Exception { ScriptMetaData scriptMetaData = builder.build(); assertEquals("_source", scriptMetaData.getStoredScript("script").getSource()); - assertEquals("{\"field\":\"value\"}", scriptMetaData.getStoredScript("template").getSource()); - assertEquals("value", scriptMetaData.getStoredScript("template_field").getSource()); + assertEquals("{\"field\":\"value\"}", scriptMetaData.getStoredScript("source_template").getSource()); } public void testDiff() throws Exception { ScriptMetaData.Builder builder = new ScriptMetaData.Builder(null); - builder.storeScript("1", StoredScriptSource.parse(new BytesArray("{\"foo\":\"abc\"}"), XContentType.JSON)); - assertWarnings("scripts should not be stored without a context. Specify them in a \"script\" element."); - builder.storeScript("2", StoredScriptSource.parse(new BytesArray("{\"foo\":\"def\"}"), XContentType.JSON)); - assertWarnings("scripts should not be stored without a context. Specify them in a \"script\" element."); - builder.storeScript("3", StoredScriptSource.parse(new BytesArray("{\"foo\":\"ghi\"}"), XContentType.JSON)); - assertWarnings("scripts should not be stored without a context. Specify them in a \"script\" element."); + builder.storeScript("1", StoredScriptSource.parse( + new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"abc\"}}}"), XContentType.JSON)); + builder.storeScript("2", StoredScriptSource.parse( + new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"def\"}}}"), XContentType.JSON)); + builder.storeScript("3", StoredScriptSource.parse( + new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"ghi\"}}}"), XContentType.JSON)); ScriptMetaData scriptMetaData1 = builder.build(); builder = new ScriptMetaData.Builder(scriptMetaData1); - builder.storeScript("2", StoredScriptSource.parse(new BytesArray("{\"foo\":\"changed\"}"), XContentType.JSON)); - assertWarnings("scripts should not be stored without a context. Specify them in a \"script\" element."); + builder.storeScript("2", StoredScriptSource.parse( + new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"changed\"}}}"), XContentType.JSON)); builder.deleteScript("3"); - builder.storeScript("4", StoredScriptSource.parse(new BytesArray("{\"foo\":\"jkl\"}"), XContentType.JSON)); - assertWarnings("scripts should not be stored without a context. Specify them in a \"script\" element."); + builder.storeScript("4", StoredScriptSource.parse( + new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"jkl\"}}}"), XContentType.JSON)); ScriptMetaData scriptMetaData2 = builder.build(); ScriptMetaData.ScriptMetadataDiff diff = (ScriptMetaData.ScriptMetadataDiff) scriptMetaData2.diff(scriptMetaData1); diff --git a/server/src/test/java/org/elasticsearch/script/StoredScriptSourceTests.java b/server/src/test/java/org/elasticsearch/script/StoredScriptSourceTests.java index 49e2623626895..79d5c67bc782e 100644 --- a/server/src/test/java/org/elasticsearch/script/StoredScriptSourceTests.java +++ b/server/src/test/java/org/elasticsearch/script/StoredScriptSourceTests.java @@ -40,19 +40,21 @@ protected StoredScriptSource createTestInstance() { try { XContentBuilder template = XContentBuilder.builder(xContentType.xContent()); template.startObject(); - template.startObject("query"); - template.startObject("match"); - template.field("title", "{{query_string}}"); - template.endObject(); + template.startObject("script"); + { + template.field("lang", "mustache"); + template.startObject("source"); + template.startObject("query").startObject("match").field("title", "{{query_string}}").endObject(); + template.endObject(); + template.endObject(); + } template.endObject(); template.endObject(); Map options = new HashMap<>(); if (randomBoolean()) { options.put(Script.CONTENT_TYPE_OPTION, xContentType.mediaType()); } - StoredScriptSource source = StoredScriptSource.parse(BytesReference.bytes(template), xContentType); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); - return source; + return StoredScriptSource.parse(BytesReference.bytes(template), xContentType); } catch (IOException e) { throw new AssertionError("Failed to create test instance", e); } @@ -84,7 +86,7 @@ protected StoredScriptSource mutateInstance(StoredScriptSource instance) throws newTemplate.endObject(); newTemplate.endObject(); - switch (between(0, 3)) { + switch (between(0, 2)) { case 0: source = Strings.toString(newTemplate); break; @@ -92,12 +94,9 @@ protected StoredScriptSource mutateInstance(StoredScriptSource instance) throws lang = randomAlphaOfLengthBetween(1, 20); break; case 2: + default: options = new HashMap<>(options); options.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); - break; - case 3: - default: - return new StoredScriptSource(Strings.toString(newTemplate)); } return new StoredScriptSource(lang, source, options); } diff --git a/server/src/test/java/org/elasticsearch/script/StoredScriptTests.java b/server/src/test/java/org/elasticsearch/script/StoredScriptTests.java index 04483c869d9b3..627d67dc833e4 100644 --- a/server/src/test/java/org/elasticsearch/script/StoredScriptTests.java +++ b/server/src/test/java/org/elasticsearch/script/StoredScriptTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.script; import org.elasticsearch.ResourceNotFoundException; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; @@ -66,49 +67,6 @@ public void testSourceParsing() throws Exception { assertThat(parsed, equalTo(source)); } - // simple template value string - try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { - builder.startObject().field("template", "code").endObject(); - - StoredScriptSource parsed = StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON); - StoredScriptSource source = new StoredScriptSource("mustache", "code", Collections.emptyMap()); - - assertThat(parsed, equalTo(source)); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); - } - - // complex template with wrapper template object - try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { - builder.startObject().field("template").startObject().field("query", "code").endObject().endObject(); - String code; - - try (XContentBuilder cb = XContentFactory.contentBuilder(builder.contentType())) { - code = Strings.toString(cb.startObject().field("query", "code").endObject()); - } - - StoredScriptSource parsed = StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON); - StoredScriptSource source = new StoredScriptSource("mustache", code, Collections.emptyMap()); - - assertThat(parsed, equalTo(source)); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); - } - - // complex template with no wrapper object - try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { - builder.startObject().field("query", "code").endObject(); - String code; - - try (XContentBuilder cb = XContentFactory.contentBuilder(builder.contentType())) { - code = Strings.toString(cb.startObject().field("query", "code").endObject()); - } - - StoredScriptSource parsed = StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON); - StoredScriptSource source = new StoredScriptSource("mustache", code, Collections.emptyMap()); - - assertThat(parsed, equalTo(source)); - assertWarnings("the template context is now deprecated. Specify templates in a \"script\" element."); - } - // complex template using script as the field name try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { builder.startObject().startObject("script").field("lang", "mustache") @@ -206,6 +164,15 @@ public void testSourceParsingErrors() throws Exception { StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON)); assertThat(iae.getMessage(), equalTo("illegal compiler options [{option=option}] specified")); } + + // check for unsupported template context + try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { + builder.startObject().field("template", "code").endObject(); + ParsingException pEx = expectThrows(ParsingException.class, () -> + StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON)); + assertThat(pEx.getMessage(), equalTo("unexpected field [template], expected ["+ + StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName()+ "]")); + } } public void testEmptyTemplateDeprecations() throws IOException { @@ -219,19 +186,6 @@ public void testEmptyTemplateDeprecations() throws IOException { assertWarnings("empty templates should no longer be used"); } - try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { - builder.startObject().field("template", "").endObject(); - - StoredScriptSource parsed = StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON); - StoredScriptSource source = new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, "", Collections.emptyMap()); - - assertThat(parsed, equalTo(source)); - assertWarnings( - "the template context is now deprecated. Specify templates in a \"script\" element.", - "empty templates should no longer be used" - ); - } - try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { builder.startObject().field("script").startObject().field("lang", "mustache") .field("source", "").endObject().endObject(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java index afcbd2499033e..1c1d8dde8cfdb 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java @@ -220,7 +220,11 @@ public void testConditionSearchWithIndexedTemplate() throws Exception { SearchSourceBuilder searchSourceBuilder = searchSource().query(matchQuery("level", "a")); assertAcked(client().admin().cluster().preparePutStoredScript() .setId("my-template") - .setContent(BytesReference.bytes(jsonBuilder().startObject().field("template").value(searchSourceBuilder).endObject()), + .setContent(BytesReference.bytes( + jsonBuilder().startObject().startObject("script") + .field("lang", "mustache") + .field("source").value(searchSourceBuilder) + .endObject().endObject()), XContentType.JSON) .get());