Skip to content

Commit 1647461

Browse files
committed
Fix IndexTemplateMetaData parsing from xContent (#30917)
We failed to register "aliases" and "version" into the list of keywords in the IndexTemplateMetaData; then fail to parse the following index template. ``` { "aliases": {"log": {}}, "index_patterns": ["pattern-1"] } ``` This commit registers that missing keywords.
1 parent e270696 commit 1647461

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ public void writeTo(StreamOutput out) throws IOException {
274274

275275
public static class Builder {
276276

277-
private static final Set<String> VALID_FIELDS = Sets.newHashSet("template", "order", "mappings", "settings", "index_patterns");
277+
private static final Set<String> VALID_FIELDS = Sets.newHashSet(
278+
"template", "order", "mappings", "settings", "index_patterns", "aliases", "version");
278279
static {
279280
VALID_FIELDS.addAll(IndexMetaData.customPrototypes.keySet());
280281
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import static java.util.Collections.singletonMap;
4444
import static org.elasticsearch.cluster.metadata.AliasMetaData.newAliasMetaDataBuilder;
4545
import static org.hamcrest.CoreMatchers.equalTo;
46+
import static org.hamcrest.Matchers.contains;
4647

4748
public class IndexTemplateMetaDataTests extends ESTestCase {
4849

@@ -167,4 +168,54 @@ DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithoutPa
167168
assertThat(ex.getMessage(), equalTo("Index patterns must not be null or empty; got null"));
168169
}
169170
}
171+
172+
public void testParseTemplateWithAliases() throws Exception {
173+
String templateInJSON = "{\"aliases\": {\"log\":{}}, \"index_patterns\": [\"pattern-1\"]}";
174+
try (XContentParser parser =
175+
XContentHelper.createParser(NamedXContentRegistry.EMPTY,
176+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateInJSON), XContentType.JSON)) {
177+
IndexTemplateMetaData template = IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100));
178+
assertThat(template.aliases().containsKey("log"), equalTo(true));
179+
assertThat(template.patterns(), contains("pattern-1"));
180+
}
181+
}
182+
183+
public void testFromToXContent() throws Exception {
184+
String templateName = randomUnicodeOfCodepointLengthBetween(1, 10);
185+
IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData.builder(templateName);
186+
templateBuilder.patterns(Arrays.asList("pattern-1"));
187+
int numAlias = between(0, 5);
188+
for (int i = 0; i < numAlias; i++) {
189+
AliasMetaData.Builder alias = AliasMetaData.builder(randomAlphaOfLengthBetween(1, 100));
190+
if (randomBoolean()) {
191+
alias.indexRouting(randomRealisticUnicodeOfLengthBetween(1, 100));
192+
}
193+
if (randomBoolean()) {
194+
alias.searchRouting(randomRealisticUnicodeOfLengthBetween(1, 100));
195+
}
196+
templateBuilder.putAlias(alias);
197+
}
198+
if (randomBoolean()) {
199+
templateBuilder.settings(Settings.builder().put("index.setting-1", randomLong()));
200+
templateBuilder.settings(Settings.builder().put("index.setting-2", randomTimeValue()));
201+
}
202+
if (randomBoolean()) {
203+
templateBuilder.order(randomInt());
204+
}
205+
if (randomBoolean()) {
206+
templateBuilder.version(between(0, 100));
207+
}
208+
if (randomBoolean()) {
209+
templateBuilder.putMapping("doc", "{\"doc\":{\"properties\":{\"type\":\"text\"}}}");
210+
}
211+
IndexTemplateMetaData template = templateBuilder.build();
212+
XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.JSON.xContent()));
213+
builder.startObject();
214+
IndexTemplateMetaData.Builder.toXContent(template, builder, ToXContent.EMPTY_PARAMS);
215+
builder.endObject();
216+
try (XContentParser parser = createParser(shuffleXContent(builder))) {
217+
IndexTemplateMetaData parsed = IndexTemplateMetaData.Builder.fromXContent(parser, templateName);
218+
assertThat(parsed, equalTo(template));
219+
}
220+
}
170221
}

0 commit comments

Comments
 (0)