Skip to content

Commit

Permalink
Fix missing order field in TemplatesMetadata deserialization (#15509)
Browse files Browse the repository at this point in the history
Signed-off-by: Sooraj Sinha <soosinha@amazon.com>
  • Loading branch information
soosinha authored Sep 3, 2024
1 parent 925f41b commit bcd09ab
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,15 @@ public static TemplatesMetadata fromXContent(XContentParser parser) throws IOExc
String currentFieldName = parser.currentName();
if (currentFieldName == null) {
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
// move to the field name
token = parser.nextToken();
}
currentFieldName = parser.currentName();
}
if (currentFieldName != null) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.START_OBJECT) {
// move to the field name
token = parser.nextToken();
}
if (parser.currentName() != null && token != XContentParser.Token.END_OBJECT) {
do {
builder.put(IndexTemplateMetadata.Builder.fromXContent(parser, parser.currentName()));
}
} while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT);
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,41 @@ public static Metadata randomMetadata() {
return md.build();
}

public void testXContentWithTemplateMetadata() throws IOException {
final TemplatesMetadata templatesMetadata = getTemplatesMetadata(0);
verifyTemplatesMetadata(templatesMetadata);
final TemplatesMetadata templatesMetadata2 = getTemplatesMetadata(2);
verifyTemplatesMetadata(templatesMetadata2);
}

private void verifyTemplatesMetadata(TemplatesMetadata templatesMetadata) throws IOException {
final Metadata originalMeta = Metadata.builder().templates(templatesMetadata).build();
final XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
Metadata.FORMAT.toXContent(builder, originalMeta);
builder.endObject();
try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
final Metadata fromXContentMeta = Metadata.fromXContent(parser);
assertThat(fromXContentMeta.templates(), equalTo(originalMeta.templates()));
}
}

private static TemplatesMetadata getTemplatesMetadata(int numberOfTemplates) {
TemplatesMetadata.Builder builder = TemplatesMetadata.builder();
for (int i = 0; i < numberOfTemplates; i++) {
builder.put(
IndexTemplateMetadata.builder("template" + i)
.order(1234)
.patterns(Arrays.asList(randomAlphaOfLength(3) + "-*"))
.settings(
Settings.builder().put("index.random_index_setting_" + randomAlphaOfLength(3), randomAlphaOfLength(5)).build()
)
.build()
);
}
return builder.build();
}

private static CreateIndexResult createIndices(int numIndices, int numBackingIndices, String dataStreamName) {
// create some indices that do not back a data stream
final List<Index> indices = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,22 @@ public static TemplatesMetadata getTemplatesMetadata() {
return TemplatesMetadata.builder()
.put(
IndexTemplateMetadata.builder("template" + randomAlphaOfLength(3))
.order(1234)
.patterns(Arrays.asList("bar-*", "foo-*"))
.settings(
Settings.builder().put("index.random_index_setting_" + randomAlphaOfLength(3), randomAlphaOfLength(5)).build()
)
.build()
)
.put(
IndexTemplateMetadata.builder("template" + randomAlphaOfLength(3))
.order(5678)
.patterns(Arrays.asList("test-*"))
.settings(
Settings.builder().put("index.random_index_setting_" + randomAlphaOfLength(3), randomAlphaOfLength(5)).build()
)
.build()
)
.build();
}
}

0 comments on commit bcd09ab

Please sign in to comment.