Skip to content

Commit 1c3a019

Browse files
committed
Fix a bug where mappings are dropped from rollover requests. (#45411)
We accidentally introduced this bug when adding a typeless version of the rollover request. The bug is not present if include_type_name is set to true.
1 parent 352739d commit 1c3a019

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
8282
throw new IllegalArgumentException("The mapping definition cannot be nested under a type " +
8383
"[" + MapperService.SINGLE_MAPPING_NAME + "] unless include_type_name is set to true.");
8484
}
85-
request.createIndexRequest.mapping(MapperService.SINGLE_MAPPING_NAME, parser.map());
85+
request.createIndexRequest.mapping(MapperService.SINGLE_MAPPING_NAME, mappings);
8686
}
8787
}, CreateIndexRequest.MAPPINGS, ObjectParser.ValueType.OBJECT);
8888
PARSER.declareField((parser, request, context) -> request.createIndexRequest.aliases(parser.map()),

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.action.ActionRequestValidationException;
2323
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
2424
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestTests;
25+
import org.elasticsearch.common.bytes.BytesArray;
2526
import org.elasticsearch.common.bytes.BytesReference;
2627
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2728
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
@@ -32,9 +33,11 @@
3233
import org.elasticsearch.common.unit.TimeValue;
3334
import org.elasticsearch.common.xcontent.XContentBuilder;
3435
import org.elasticsearch.common.xcontent.XContentFactory;
36+
import org.elasticsearch.common.xcontent.XContentHelper;
3537
import org.elasticsearch.common.xcontent.XContentParseException;
3638
import org.elasticsearch.common.xcontent.XContentType;
3739
import org.elasticsearch.index.RandomCreateIndexGenerator;
40+
import org.elasticsearch.index.mapper.MapperService;
3841
import org.elasticsearch.indices.IndicesModule;
3942
import org.elasticsearch.test.ESTestCase;
4043
import org.elasticsearch.test.XContentTestUtils;
@@ -115,6 +118,35 @@ public void testParsingWithIndexSettings() throws Exception {
115118
assertThat(request.getCreateIndexRequest().settings().getAsInt("number_of_shards", 0), equalTo(10));
116119
}
117120

121+
public void testTypelessMappingParsing() throws Exception {
122+
final RolloverRequest request = new RolloverRequest(randomAlphaOfLength(10), randomAlphaOfLength(10));
123+
final XContentBuilder builder = XContentFactory.jsonBuilder()
124+
.startObject()
125+
.startObject("mappings")
126+
.startObject("properties")
127+
.startObject("field1")
128+
.field("type", "keyword")
129+
.endObject()
130+
.endObject()
131+
.endObject()
132+
.endObject();
133+
134+
boolean includeTypeName = false;
135+
request.fromXContent(includeTypeName, createParser(builder));
136+
137+
CreateIndexRequest createIndexRequest = request.getCreateIndexRequest();
138+
String mapping = createIndexRequest.mappings().get(MapperService.SINGLE_MAPPING_NAME);
139+
assertNotNull(mapping);
140+
141+
Map<String, Object> parsedMapping = XContentHelper.convertToMap(
142+
new BytesArray(mapping), false, XContentType.JSON).v2();
143+
144+
@SuppressWarnings("unchecked")
145+
Map<String, Object> properties = (Map<String, Object>) parsedMapping.get(MapperService.SINGLE_MAPPING_NAME);
146+
assertNotNull(properties);
147+
assertFalse(properties.isEmpty());
148+
}
149+
118150
public void testSerialize() throws Exception {
119151
RolloverRequest originalRequest = new RolloverRequest("alias-index", "new-index-name");
120152
originalRequest.addMaxIndexDocsCondition(randomNonNegativeLong());

0 commit comments

Comments
 (0)