Skip to content

Commit d1c28c6

Browse files
authored
HLRestClient: Follow-up for put index template api (#30592)
This commit addresses some comments given after the original PR was in. Follow-up #30400
1 parent 17d65c1 commit d1c28c6

File tree

3 files changed

+92
-31
lines changed

3 files changed

+92
-31
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.common.logging.DeprecationLogger;
3838
import org.elasticsearch.common.logging.Loggers;
3939
import org.elasticsearch.common.settings.Settings;
40+
import org.elasticsearch.common.xcontent.DeprecationHandler;
4041
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
4142
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
4243
import org.elasticsearch.common.xcontent.ToXContent;
@@ -45,6 +46,7 @@
4546
import org.elasticsearch.common.xcontent.XContentHelper;
4647
import org.elasticsearch.common.xcontent.XContentParser;
4748
import org.elasticsearch.common.xcontent.XContentType;
49+
import org.elasticsearch.common.xcontent.json.JsonXContent;
4850
import org.elasticsearch.common.xcontent.support.XContentMapValues;
4951

5052
import java.io.IOException;
@@ -543,9 +545,6 @@ public void writeTo(StreamOutput out) throws IOException {
543545

544546
@Override
545547
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
546-
if (customs.isEmpty() == false) {
547-
throw new IllegalArgumentException("Custom data type is no longer supported in index template [" + customs + "]");
548-
}
549548
builder.field("index_patterns", indexPatterns);
550549
builder.field("order", order);
551550
if (version != null) {
@@ -558,8 +557,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
558557

559558
builder.startObject("mappings");
560559
for (Map.Entry<String, String> entry : mappings.entrySet()) {
561-
Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(entry.getValue()), false).v2();
562-
builder.field(entry.getKey(), mapping);
560+
builder.field(entry.getKey());
561+
XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
562+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue());
563+
builder.copyCurrentStructure(parser);
563564
}
564565
builder.endObject();
565566

@@ -568,6 +569,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
568569
alias.toXContent(builder, params);
569570
}
570571
builder.endObject();
572+
573+
for (Map.Entry<String, IndexMetaData.Custom> entry : customs.entrySet()) {
574+
builder.field(entry.getKey(), entry.getValue(), params);
575+
}
576+
571577
return builder;
572578
}
573579
}

server/src/test/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
import org.elasticsearch.action.admin.indices.alias.Alias;
2424
import org.elasticsearch.common.Strings;
2525
import org.elasticsearch.common.bytes.BytesArray;
26-
import org.elasticsearch.common.bytes.BytesReference;
2726
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2827
import org.elasticsearch.common.io.stream.StreamInput;
2928
import org.elasticsearch.common.settings.Settings;
30-
import org.elasticsearch.common.xcontent.ToXContent;
3129
import org.elasticsearch.common.xcontent.XContentFactory;
3230
import org.elasticsearch.common.xcontent.XContentHelper;
31+
import org.elasticsearch.common.xcontent.XContentParser;
3332
import org.elasticsearch.common.xcontent.XContentType;
3433
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
35-
import org.elasticsearch.test.ESTestCase;
34+
import org.elasticsearch.test.AbstractXContentTestCase;
3635

3736
import java.io.IOException;
37+
import java.io.UncheckedIOException;
3838
import java.util.Arrays;
3939
import java.util.Base64;
4040
import java.util.Collections;
@@ -45,7 +45,7 @@
4545
import static org.hamcrest.Matchers.nullValue;
4646
import static org.hamcrest.core.Is.is;
4747

48-
public class PutIndexTemplateRequestTests extends ESTestCase {
48+
public class PutIndexTemplateRequestTests extends AbstractXContentTestCase<PutIndexTemplateRequest> {
4949

5050
// bwc for #21009
5151
public void testPutIndexTemplateRequest510() throws IOException {
@@ -137,13 +137,14 @@ public void testValidateErrorMessage() throws Exception {
137137
assertThat(noError, is(nullValue()));
138138
}
139139

140-
private PutIndexTemplateRequest randomPutIndexTemplateRequest() throws IOException {
140+
@Override
141+
protected PutIndexTemplateRequest createTestInstance() {
141142
PutIndexTemplateRequest request = new PutIndexTemplateRequest();
142143
request.name("test");
143-
if (randomBoolean()){
144+
if (randomBoolean()) {
144145
request.version(randomInt());
145146
}
146-
if (randomBoolean()){
147+
if (randomBoolean()) {
147148
request.order(randomInt());
148149
}
149150
request.patterns(Arrays.asList(generateRandomStringArray(20, 100, false, false)));
@@ -159,30 +160,39 @@ private PutIndexTemplateRequest randomPutIndexTemplateRequest() throws IOExcepti
159160
request.alias(alias);
160161
}
161162
if (randomBoolean()) {
162-
request.mapping("doc", XContentFactory.jsonBuilder().startObject()
163-
.startObject("doc").startObject("properties")
164-
.startObject("field-" + randomInt()).field("type", randomFrom("keyword", "text")).endObject()
165-
.endObject().endObject().endObject());
163+
try {
164+
request.mapping("doc", XContentFactory.jsonBuilder().startObject()
165+
.startObject("doc").startObject("properties")
166+
.startObject("field-" + randomInt()).field("type", randomFrom("keyword", "text")).endObject()
167+
.endObject().endObject().endObject());
168+
} catch (IOException ex) {
169+
throw new UncheckedIOException(ex);
170+
}
166171
}
167-
if (randomBoolean()){
172+
if (randomBoolean()) {
168173
request.settings(Settings.builder().put("setting1", randomLong()).put("setting2", randomTimeValue()).build());
169174
}
170175
return request;
171176
}
172177

173-
public void testFromToXContentPutTemplateRequest() throws Exception {
174-
for (int i = 0; i < 10; i++) {
175-
PutIndexTemplateRequest expected = randomPutIndexTemplateRequest();
176-
XContentType xContentType = randomFrom(XContentType.values());
177-
BytesReference shuffled = toShuffledXContent(expected, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
178-
PutIndexTemplateRequest parsed = new PutIndexTemplateRequest().source(shuffled, xContentType);
179-
assertNotSame(expected, parsed);
180-
assertThat(parsed.version(), equalTo(expected.version()));
181-
assertThat(parsed.order(), equalTo(expected.order()));
182-
assertThat(parsed.patterns(), equalTo(expected.patterns()));
183-
assertThat(parsed.aliases(), equalTo(expected.aliases()));
184-
assertThat(parsed.mappings(), equalTo(expected.mappings()));
185-
assertThat(parsed.settings(), equalTo(expected.settings()));
186-
}
178+
@Override
179+
protected PutIndexTemplateRequest doParseInstance(XContentParser parser) throws IOException {
180+
return new PutIndexTemplateRequest().source(parser.map());
181+
}
182+
183+
@Override
184+
protected void assertEqualInstances(PutIndexTemplateRequest expected, PutIndexTemplateRequest actual) {
185+
assertNotSame(expected, actual);
186+
assertThat(actual.version(), equalTo(expected.version()));
187+
assertThat(actual.order(), equalTo(expected.order()));
188+
assertThat(actual.patterns(), equalTo(expected.patterns()));
189+
assertThat(actual.aliases(), equalTo(expected.aliases()));
190+
assertThat(actual.mappings(), equalTo(expected.mappings()));
191+
assertThat(actual.settings(), equalTo(expected.settings()));
192+
}
193+
194+
@Override
195+
protected boolean supportsUnknownFields() {
196+
return false;
187197
}
188198
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action.admin.indices.template.put;
21+
22+
import org.elasticsearch.common.xcontent.XContentParser;
23+
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
24+
25+
public class PutIndexTemplateResponseTests extends AbstractStreamableXContentTestCase<PutIndexTemplateResponse> {
26+
@Override
27+
protected PutIndexTemplateResponse doParseInstance(XContentParser parser) {
28+
return PutIndexTemplateResponse.fromXContent(parser);
29+
}
30+
31+
@Override
32+
protected PutIndexTemplateResponse createTestInstance() {
33+
return new PutIndexTemplateResponse(randomBoolean());
34+
}
35+
36+
@Override
37+
protected PutIndexTemplateResponse createBlankInstance() {
38+
return new PutIndexTemplateResponse();
39+
}
40+
41+
@Override
42+
protected PutIndexTemplateResponse mutateInstance(PutIndexTemplateResponse response) {
43+
return new PutIndexTemplateResponse(response.isAcknowledged() == false);
44+
}
45+
}

0 commit comments

Comments
 (0)