Skip to content

Commit 04c21af

Browse files
authored
Clean up IndexTemplateMetaData xcontent serialization (#54003)
IndexTemplateMetaData x-content serialization is a bit confused and trappy at the moment, with multiple static methods on the Builder object which may or may not emit the template name and/or mapping types. This commit consolidates everything into a single toXContent() method on the base class, with different options passed through as ToXContent.Params. Deserialization is converted to use an ObjectParser, taking the template name as a context.
1 parent 36377d7 commit 04c21af

File tree

9 files changed

+125
-191
lines changed

9 files changed

+125
-191
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/template/get/GetIndexTemplatesResponse.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
2323
import org.elasticsearch.common.io.stream.StreamInput;
2424
import org.elasticsearch.common.io.stream.StreamOutput;
25-
import org.elasticsearch.common.xcontent.ToXContent;
2625
import org.elasticsearch.common.xcontent.ToXContentObject;
2726
import org.elasticsearch.common.xcontent.XContentBuilder;
2827

@@ -31,8 +30,6 @@
3130
import java.util.List;
3231
import java.util.Objects;
3332

34-
import static java.util.Collections.singletonMap;
35-
3633
public class GetIndexTemplatesResponse extends ActionResponse implements ToXContentObject {
3734

3835
private final List<IndexTemplateMetaData> indexTemplates;
@@ -77,11 +74,9 @@ public int hashCode() {
7774

7875
@Override
7976
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
80-
params = new ToXContent.DelegatingMapParams(singletonMap("reduce_mappings", "true"), params);
81-
8277
builder.startObject();
8378
for (IndexTemplateMetaData indexTemplateMetaData : getIndexTemplates()) {
84-
IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params);
79+
indexTemplateMetaData.toXContent(builder, params);
8580
}
8681
builder.endObject();
8782
return builder;

server/src/main/java/org/elasticsearch/cluster/ClusterState.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.common.io.stream.StreamOutput;
5454
import org.elasticsearch.common.io.stream.VersionedNamedWriteable;
5555
import org.elasticsearch.common.settings.Settings;
56+
import org.elasticsearch.common.xcontent.ToXContent;
5657
import org.elasticsearch.common.xcontent.ToXContentFragment;
5758
import org.elasticsearch.common.xcontent.XContentBuilder;
5859
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -426,10 +427,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
426427
coordinationMetaData().toXContent(builder, params);
427428
builder.endObject();
428429

430+
ToXContent.Params templateParams = new DelegatingMapParams(Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true"), params);
429431
builder.startObject("templates");
430432
for (ObjectCursor<IndexTemplateMetaData> cursor : metaData().templates().values()) {
431-
IndexTemplateMetaData templateMetaData = cursor.value;
432-
IndexTemplateMetaData.Builder.toXContentWithTypes(templateMetaData, builder, params);
433+
cursor.value.toXContent(builder, templateParams);
433434
}
434435
builder.endObject();
435436

server/src/main/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaData.java

Lines changed: 90 additions & 163 deletions
Large diffs are not rendered by default.

server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,9 +1404,11 @@ public static void toXContent(MetaData metaData, XContentBuilder builder, ToXCon
14041404
builder.endObject();
14051405
}
14061406

1407+
ToXContent.Params typedParams
1408+
= new ToXContent.DelegatingMapParams(Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true"), params);
14071409
builder.startObject("templates");
14081410
for (ObjectCursor<IndexTemplateMetaData> cursor : metaData.templates().values()) {
1409-
IndexTemplateMetaData.Builder.toXContentWithTypes(cursor.value, builder, params);
1411+
cursor.value.toXContent(builder, typedParams);
14101412
}
14111413
builder.endObject();
14121414

@@ -1471,7 +1473,7 @@ public static MetaData fromXContent(XContentParser parser, boolean preserveUnkno
14711473
builder.hashesOfConsistentSettings(parser.mapStrings());
14721474
} else if ("templates".equals(currentFieldName)) {
14731475
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
1474-
builder.put(IndexTemplateMetaData.Builder.fromXContent(parser, parser.currentName()));
1476+
builder.put(IndexTemplateMetaData.fromXContent(parser, parser.currentName()));
14751477
}
14761478
} else {
14771479
try {

server/src/main/java/org/elasticsearch/cluster/metadata/TemplateUpgradeService.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
import java.util.concurrent.atomic.AtomicInteger;
5858
import java.util.function.UnaryOperator;
5959

60-
import static java.util.Collections.singletonMap;
61-
6260
/**
6361
* Upgrades Templates on behalf of installed {@link Plugin}s when a node joins the cluster
6462
*/
@@ -248,14 +246,12 @@ Optional<Tuple<Map<String, BytesReference>, Set<String>>> calculateTemplateChang
248246
return Optional.empty();
249247
}
250248

251-
private static final ToXContent.Params PARAMS = new ToXContent.MapParams(singletonMap("reduce_mappings", "true"));
249+
private static final ToXContent.Params PARAMS
250+
= new ToXContent.MapParams(Map.of(IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
252251

253252
private BytesReference toBytesReference(IndexTemplateMetaData templateMetaData) {
254253
try {
255-
return XContentHelper.toXContent((builder, params) -> {
256-
IndexTemplateMetaData.Builder.toInnerXContentWithTypes(templateMetaData, builder, params);
257-
return builder;
258-
}, XContentType.JSON, PARAMS, false);
254+
return XContentHelper.toXContent(templateMetaData, XContentType.JSON, PARAMS, false);
259255
} catch (IOException ex) {
260256
throw new IllegalStateException("Cannot serialize template [" + templateMetaData.getName() + "]", ex);
261257
}

server/src/test/java/org/elasticsearch/cluster/metadata/IndexTemplateMetaDataTests.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.util.Arrays;
3636
import java.util.Collections;
37+
import java.util.Map;
3738

3839
import static org.hamcrest.CoreMatchers.equalTo;
3940
import static org.hamcrest.Matchers.contains;
@@ -54,21 +55,23 @@ public void testIndexTemplateMetaDataXContentRoundTrip() throws Exception {
5455
final IndexTemplateMetaData indexTemplateMetaData;
5556
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
5657
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateBytes, XContentType.JSON)) {
57-
indexTemplateMetaData = IndexTemplateMetaData.Builder.fromXContent(parser, "test");
58+
indexTemplateMetaData = IndexTemplateMetaData.fromXContent(parser, "test");
5859
}
5960

6061
final BytesReference templateBytesRoundTrip;
62+
final ToXContent.Params params = new ToXContent.MapParams(
63+
Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true", IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
6164
try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent)) {
6265
builder.startObject();
63-
IndexTemplateMetaData.Builder.toXContentWithTypes(indexTemplateMetaData, builder, ToXContent.EMPTY_PARAMS);
66+
indexTemplateMetaData.toXContent(builder, params);
6467
builder.endObject();
6568
templateBytesRoundTrip = BytesReference.bytes(builder);
6669
}
6770

6871
final IndexTemplateMetaData indexTemplateMetaDataRoundTrip;
6972
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
7073
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateBytesRoundTrip, XContentType.JSON)) {
71-
indexTemplateMetaDataRoundTrip = IndexTemplateMetaData.Builder.fromXContent(parser, "test");
74+
indexTemplateMetaDataRoundTrip = IndexTemplateMetaData.fromXContent(parser, "test");
7275
}
7376
assertThat(indexTemplateMetaData, equalTo(indexTemplateMetaDataRoundTrip));
7477
}
@@ -97,7 +100,7 @@ public void testValidateInvalidIndexPatterns() throws Exception {
97100
XContentHelper.createParser(NamedXContentRegistry.EMPTY,
98101
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithEmptyPattern), XContentType.JSON)) {
99102
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
100-
() -> IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
103+
() -> IndexTemplateMetaData.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
101104
assertThat(ex.getMessage(), equalTo("Index patterns must not be null or empty; got []"));
102105
}
103106

@@ -112,7 +115,7 @@ DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithEmpty
112115
XContentHelper.createParser(NamedXContentRegistry.EMPTY,
113116
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithoutPattern), XContentType.JSON)) {
114117
final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
115-
() -> IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
118+
() -> IndexTemplateMetaData.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)));
116119
assertThat(ex.getMessage(), equalTo("Index patterns must not be null or empty; got null"));
117120
}
118121
}
@@ -122,7 +125,7 @@ public void testParseTemplateWithAliases() throws Exception {
122125
try (XContentParser parser =
123126
XContentHelper.createParser(NamedXContentRegistry.EMPTY,
124127
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateInJSON), XContentType.JSON)) {
125-
IndexTemplateMetaData template = IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100));
128+
IndexTemplateMetaData template = IndexTemplateMetaData.fromXContent(parser, randomAlphaOfLengthBetween(1, 100));
126129
assertThat(template.aliases().containsKey("log"), equalTo(true));
127130
assertThat(template.patterns(), contains("pattern-1"));
128131
}
@@ -157,12 +160,14 @@ public void testFromToXContent() throws Exception {
157160
templateBuilder.putMapping("doc", "{\"doc\":{\"properties\":{\"type\":\"text\"}}}");
158161
}
159162
IndexTemplateMetaData template = templateBuilder.build();
163+
final ToXContent.Params params = new ToXContent.MapParams(
164+
Map.of(IndexTemplateMetaData.INCLUDE_TYPE_NAME, "true", IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
160165
XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.JSON.xContent()));
161166
builder.startObject();
162-
IndexTemplateMetaData.Builder.toXContentWithTypes(template, builder, ToXContent.EMPTY_PARAMS);
167+
template.toXContent(builder, params);
163168
builder.endObject();
164169
try (XContentParser parser = createParser(shuffleXContent(builder))) {
165-
IndexTemplateMetaData parsed = IndexTemplateMetaData.Builder.fromXContent(parser, templateName);
170+
IndexTemplateMetaData parsed = IndexTemplateMetaData.fromXContent(parser, templateName);
166171
assertThat(parsed, equalTo(template));
167172
}
168173
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/TemplateUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void loadTemplateIntoMap(String resource, Map<String, IndexTemplat
4747
final String template = loadTemplate(resource, version, versionProperty);
4848
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
4949
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template)) {
50-
map.put(templateName, IndexTemplateMetaData.Builder.fromXContent(parser, templateName));
50+
map.put(templateName, IndexTemplateMetaData.fromXContent(parser, templateName));
5151
} catch (IOException e) {
5252
// TODO: should we handle this with a thrown exception?
5353
logger.error("Error loading template [{}] as part of metadata upgrading", templateName);

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.common.unit.TimeValue;
1919
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2020
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
21+
import org.elasticsearch.common.xcontent.ToXContent;
2122
import org.elasticsearch.common.xcontent.XContentBuilder;
2223
import org.elasticsearch.common.xcontent.XContentFactory;
2324
import org.elasticsearch.common.xcontent.XContentParser;
@@ -107,10 +108,13 @@ protected void doPublish(final RestClient client, final ActionListener<Boolean>
107108
HttpEntity templateToHttpEntity() {
108109
// the internal representation of a template has type nested under mappings.
109110
// this uses xContent to help remove the type before sending to the remote cluster
111+
ToXContent.Params params = new ToXContent.MapParams(Map.of(IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
110112
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
111113
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template.get())) {
112114
XContentBuilder builder = JsonXContent.contentBuilder();
113-
IndexTemplateMetaData.Builder.removeType(IndexTemplateMetaData.Builder.fromXContent(parser, templateName), builder);
115+
builder.startObject();
116+
IndexTemplateMetaData.fromXContent(parser, templateName).toXContent(builder, params);
117+
builder.endObject();
114118
return new StringEntity(BytesReference.bytes(builder).utf8ToString(), ContentType.APPLICATION_JSON);
115119
} catch (IOException ex) {
116120
throw new IllegalStateException("Cannot serialize template [" + templateName + "] for monitoring export", ex);

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.common.util.concurrent.ThreadContext;
2727
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2828
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
29+
import org.elasticsearch.common.xcontent.ToXContent;
2930
import org.elasticsearch.common.xcontent.XContentBuilder;
3031
import org.elasticsearch.common.xcontent.XContentFactory;
3132
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -971,7 +972,10 @@ private String getExternalTemplateRepresentation(String internalRepresentation)
971972
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
972973
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, internalRepresentation)) {
973974
XContentBuilder builder = JsonXContent.contentBuilder();
974-
IndexTemplateMetaData.Builder.removeType(IndexTemplateMetaData.Builder.fromXContent(parser, ""), builder);
975+
ToXContent.Params params = new ToXContent.MapParams(Map.of(IndexTemplateMetaData.INCLUDE_TEMPLATE_NAME, "false"));
976+
builder.startObject();
977+
IndexTemplateMetaData.fromXContent(parser, "").toXContent(builder, params);
978+
builder.endObject();
975979
return BytesReference.bytes(builder).utf8ToString();
976980
}
977981
}

0 commit comments

Comments
 (0)