Skip to content

Commit 0ca6819

Browse files
authored
DocumentMapper to not implement ToXContent (#68653)
DocumentMapper does not need to implement ToXContent, in fact it is its inner Mapping that needs to and already does. Consumers can switch to calling mapping() and toXContent against it.
1 parent 8d975d2 commit 0ca6819

File tree

6 files changed

+13
-22
lines changed

6 files changed

+13
-22
lines changed

plugins/mapper-annotated-text/src/internalClusterTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public void testSearchAnalyzerSerialization() throws IOException {
434434

435435
XContentBuilder builder = XContentFactory.jsonBuilder();
436436
builder.startObject();
437-
mapper.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")));
437+
mapper.mapping().toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")));
438438
builder.endObject();
439439

440440
String mappingString = Strings.toString(builder);

server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,18 @@
1414
import org.elasticsearch.common.bytes.BytesArray;
1515
import org.elasticsearch.common.compress.CompressedXContent;
1616
import org.elasticsearch.common.xcontent.ToXContent;
17-
import org.elasticsearch.common.xcontent.ToXContentFragment;
18-
import org.elasticsearch.common.xcontent.XContentBuilder;
1917
import org.elasticsearch.common.xcontent.XContentType;
2018
import org.elasticsearch.index.IndexSettings;
2119
import org.elasticsearch.index.analysis.IndexAnalyzers;
2220
import org.elasticsearch.index.mapper.MapperService.MergeReason;
2321

24-
import java.io.IOException;
2522
import java.util.Arrays;
2623
import java.util.Collection;
2724
import java.util.Collections;
2825
import java.util.Map;
2926
import java.util.stream.Stream;
3027

31-
public class DocumentMapper implements ToXContentFragment {
28+
public class DocumentMapper {
3229
private final String type;
3330
private final CompressedXContent mappingSource;
3431
private final DocumentParser documentParser;
@@ -56,7 +53,7 @@ public DocumentMapper(RootObjectMapper.Builder rootBuilder, MapperService mapper
5653
this.mappingLookup = MappingLookup.fromMapping(mapping, documentParser, indexSettings, indexAnalyzers);
5754

5855
try {
59-
mappingSource = new CompressedXContent(this, XContentType.JSON, ToXContent.EMPTY_PARAMS);
56+
mappingSource = new CompressedXContent(mapping, XContentType.JSON, ToXContent.EMPTY_PARAMS);
6057
} catch (Exception e) {
6158
throw new ElasticsearchGenerationException("failed to serialize source for type [" + type + "]", e);
6259
}
@@ -155,9 +152,4 @@ public void validate(IndexSettings settings, boolean checkLimits) {
155152
this.mappingLookup.checkLimits(settings);
156153
}
157154
}
158-
159-
@Override
160-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
161-
return mapping().toXContent(builder, params);
162-
}
163155
}

server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,9 @@ private void assertMappingVersion(
235235
assert currentSource.equals(newSource) :
236236
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
237237
+ "to be the same as new mapping [" + newSource + "]";
238-
final CompressedXContent mapperSource = new CompressedXContent(Strings.toString(mapper));
239-
assert currentSource.equals(mapperSource) :
238+
assert currentSource.equals(mapper.mappingSource()) :
240239
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
241-
+ "to be the same as new mapping [" + mapperSource + "]";
240+
+ "to be the same as new mapping [" + mapper.mappingSource() + "]";
242241
}
243242

244243
} else {

server/src/test/java/org/elasticsearch/index/mapper/ParametrizedMapperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ public void testObjectSerialization() throws IOException {
336336
"\"is_interim\":{\"type\":\"boolean\"}}}}}}";
337337

338338
MapperService mapperService = createMapperService(mapping);
339-
assertEquals(mapping, Strings.toString(mapperService.documentMapper()));
339+
assertEquals(mapping, Strings.toString(mapperService.documentMapper().mapping()));
340340

341341
mapperService.merge("_doc", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
342-
assertEquals(mapping, Strings.toString(mapperService.documentMapper()));
342+
assertEquals(mapping, Strings.toString(mapperService.documentMapper().mapping()));
343343
}
344344

345345
// test custom serializer

server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public void testBWCSerialization() throws IOException {
255255

256256
assertEquals(
257257
"{\"_doc\":{\"properties\":{\"field\":{\"type\":\"text\",\"fields\":{\"subfield\":{\"type\":\"long\"}},\"fielddata\":true}}}}",
258-
Strings.toString(mapperService.documentMapper()));
258+
Strings.toString(mapperService.documentMapper().mapping()));
259259
}
260260

261261
public void testEnableStore() throws IOException {
@@ -295,7 +295,7 @@ public void testIndexOptions() throws IOException {
295295
mapping.endObject().endObject().endObject();
296296

297297
DocumentMapper mapper = createDocumentMapper(mapping);
298-
String serialized = Strings.toString(mapper);
298+
String serialized = Strings.toString(mapper.mapping());
299299
assertThat(serialized, containsString("\"offsets\":{\"type\":\"text\",\"index_options\":\"offsets\"}"));
300300
assertThat(serialized, containsString("\"freqs\":{\"type\":\"text\",\"index_options\":\"freqs\"}"));
301301
assertThat(serialized, containsString("\"docs\":{\"type\":\"text\",\"index_options\":\"docs\"}"));
@@ -370,7 +370,7 @@ public void testSearchAnalyzerSerialization() throws IOException {
370370

371371
XContentBuilder builder = XContentFactory.jsonBuilder();
372372
builder.startObject();
373-
createDocumentMapper(fieldMapping(this::minimalMapping)).toXContent(
373+
createDocumentMapper(fieldMapping(this::minimalMapping)).mapping().toXContent(
374374
builder,
375375
new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true"))
376376
);

x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/DateScriptFieldTypeTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void testDateWithFormat() throws IOException {
9494
MapperService mapperService = createMapperService(mapping.get());
9595
MappedFieldType fieldType = mapperService.fieldType("field");
9696
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
97-
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
97+
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
9898
}
9999

100100
public void testDateWithLocale() throws IOException {
@@ -105,7 +105,7 @@ public void testDateWithLocale() throws IOException {
105105
MapperService mapperService = createMapperService(mapping.get());
106106
MappedFieldType fieldType = mapperService.fieldType("field");
107107
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
108-
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
108+
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
109109
}
110110

111111
public void testDateWithLocaleAndFormat() throws IOException {
@@ -116,7 +116,7 @@ public void testDateWithLocaleAndFormat() throws IOException {
116116
MapperService mapperService = createMapperService(mapping.get());
117117
MappedFieldType fieldType = mapperService.fieldType("field");
118118
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
119-
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
119+
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
120120
}
121121

122122
public void testFormat() throws IOException {

0 commit comments

Comments
 (0)