Skip to content

Commit ae83082

Browse files
authored
Ensure we emit a warning when using the deprecated 'template' field. (#50831)
Since 6.0, the 'template' field has been deprecated in put template requests in favour of index_patterns. Previously, the PutIndexTemplateRequest would accept the 'template' field in its 'source' methods and silently convert it to 'index_patterns'. This meant that users specifying 'template' in the source would not receive a deprecation warning from the server. This PR makes a small change to no longer silently convert 'template' to 'index_patterns', which ensures that users receive a deprecation warning. Follow-up to #49460.
1 parent 5afa0b7 commit ae83082

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.action.admin.indices.alias.Alias;
2626
import org.elasticsearch.action.support.IndicesOptions;
2727
import org.elasticsearch.action.support.master.MasterNodeRequest;
28+
import org.elasticsearch.common.Nullable;
2829
import org.elasticsearch.common.Strings;
2930
import org.elasticsearch.common.bytes.BytesArray;
3031
import org.elasticsearch.common.bytes.BytesReference;
@@ -64,6 +65,15 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
6465

6566
private List<String> indexPatterns;
6667

68+
/**
69+
* This field corresponds to the deprecated 'template' parameter, which was replaced by
70+
* 'index_patterns' in 6.0. It is stored and rendered to xContent separately from
71+
* 'index_patterns' to ensure that the server emits a deprecation warning when it's been set.
72+
*/
73+
@Deprecated
74+
@Nullable
75+
private String template;
76+
6777
private int order;
6878

6979
private boolean create;
@@ -86,7 +96,7 @@ public PutIndexTemplateRequest(String name) {
8696
@Override
8797
public ActionRequestValidationException validate() {
8898
ActionRequestValidationException validationException = null;
89-
if (indexPatterns == null || indexPatterns.size() == 0) {
99+
if (template == null && (indexPatterns == null || indexPatterns.size() == 0)) {
90100
validationException = addValidationError("index patterns are missing", validationException);
91101
}
92102
return validationException;
@@ -288,7 +298,7 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
288298
String name = entry.getKey();
289299
if (name.equals("template")) {
290300
if(entry.getValue() instanceof String) {
291-
patterns(Collections.singletonList((String) entry.getValue()));
301+
this.template = (String) entry.getValue();
292302
}
293303
} else if (name.equals("index_patterns")) {
294304
if(entry.getValue() instanceof String) {
@@ -297,7 +307,7 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
297307
List<String> elements = ((List<?>) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList());
298308
patterns(elements);
299309
} else {
300-
throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings");
310+
throw new IllegalArgumentException("Malformed [index_patterns] value, should be a string or a list of strings");
301311
}
302312
} else if (name.equals("order")) {
303313
order(XContentMapValues.nodeIntegerValue(entry.getValue(), order()));
@@ -424,7 +434,12 @@ public IndicesOptions indicesOptions() {
424434

425435
@Override
426436
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
427-
builder.field("index_patterns", indexPatterns);
437+
if (template != null) {
438+
builder.field("template", template);
439+
} else {
440+
builder.field("index_patterns", indexPatterns);
441+
}
442+
428443
builder.field("order", order);
429444
if (version != null) {
430445
builder.field("version", version);

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

+27
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import org.elasticsearch.common.unit.ByteSizeValue;
9393
import org.elasticsearch.common.unit.TimeValue;
9494
import org.elasticsearch.common.xcontent.XContentBuilder;
95+
import org.elasticsearch.common.xcontent.XContentFactory;
9596
import org.elasticsearch.common.xcontent.XContentType;
9697
import org.elasticsearch.common.xcontent.json.JsonXContent;
9798
import org.elasticsearch.common.xcontent.support.XContentMapValues;
@@ -1578,6 +1579,32 @@ public void testPutTemplate() throws Exception {
15781579
assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz"));
15791580
}
15801581

1582+
public void testPutTemplateWithDeprecatedTemplateField() throws Exception {
1583+
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template")
1584+
.source(XContentFactory.jsonBuilder()
1585+
.startObject()
1586+
.field("template", "name-*")
1587+
.field("order", 10)
1588+
.startObject("settings")
1589+
.field("number_of_shards", 3)
1590+
.field("number_of_replicas", 0)
1591+
.endObject()
1592+
.endObject());
1593+
1594+
AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest,
1595+
highLevelClient().indices()::putTemplate,
1596+
highLevelClient().indices()::putTemplateAsync,
1597+
expectWarnings("Deprecated field [template] used, replaced by [index_patterns]"));
1598+
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));
1599+
1600+
Map<String, Object> templates = getAsMap("/_template/my-template");
1601+
assertThat(templates.keySet(), hasSize(1));
1602+
assertThat(extractValue("my-template.order", templates), equalTo(10));
1603+
assertThat(extractRawValues("my-template.index_patterns", templates), contains("name-*"));
1604+
assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3"));
1605+
assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0"));
1606+
}
1607+
15811608
public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception {
15821609
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template")
15831610
.patterns(Arrays.asList("pattern-1", "name-*"))

0 commit comments

Comments
 (0)