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.
This commit is contained in:
Luca Cavanna 2021-02-08 14:17:31 +01:00 committed by GitHub
parent 8d975d2ae6
commit 0ca6819882
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 22 deletions

View file

@ -434,7 +434,7 @@ public class AnnotatedTextFieldMapperTests extends MapperTestCase {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
mapper.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")));
mapper.mapping().toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")));
builder.endObject();
String mappingString = Strings.toString(builder);

View file

@ -14,21 +14,18 @@ import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.IndexAnalyzers;
import org.elasticsearch.index.mapper.MapperService.MergeReason;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;
public class DocumentMapper implements ToXContentFragment {
public class DocumentMapper {
private final String type;
private final CompressedXContent mappingSource;
private final DocumentParser documentParser;
@ -56,7 +53,7 @@ public class DocumentMapper implements ToXContentFragment {
this.mappingLookup = MappingLookup.fromMapping(mapping, documentParser, indexSettings, indexAnalyzers);
try {
mappingSource = new CompressedXContent(this, XContentType.JSON, ToXContent.EMPTY_PARAMS);
mappingSource = new CompressedXContent(mapping, XContentType.JSON, ToXContent.EMPTY_PARAMS);
} catch (Exception e) {
throw new ElasticsearchGenerationException("failed to serialize source for type [" + type + "]", e);
}
@ -155,9 +152,4 @@ public class DocumentMapper implements ToXContentFragment {
this.mappingLookup.checkLimits(settings);
}
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return mapping().toXContent(builder, params);
}
}

View file

@ -235,10 +235,9 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
assert currentSource.equals(newSource) :
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
+ "to be the same as new mapping [" + newSource + "]";
final CompressedXContent mapperSource = new CompressedXContent(Strings.toString(mapper));
assert currentSource.equals(mapperSource) :
assert currentSource.equals(mapper.mappingSource()) :
"expected current mapping [" + currentSource + "] for type [" + mapping.type() + "] "
+ "to be the same as new mapping [" + mapperSource + "]";
+ "to be the same as new mapping [" + mapper.mappingSource() + "]";
}
} else {

View file

@ -336,10 +336,10 @@ public class ParametrizedMapperTests extends MapperServiceTestCase {
"\"is_interim\":{\"type\":\"boolean\"}}}}}}";
MapperService mapperService = createMapperService(mapping);
assertEquals(mapping, Strings.toString(mapperService.documentMapper()));
assertEquals(mapping, Strings.toString(mapperService.documentMapper().mapping()));
mapperService.merge("_doc", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
assertEquals(mapping, Strings.toString(mapperService.documentMapper()));
assertEquals(mapping, Strings.toString(mapperService.documentMapper().mapping()));
}
// test custom serializer

View file

@ -255,7 +255,7 @@ public class TextFieldMapperTests extends MapperTestCase {
assertEquals(
"{\"_doc\":{\"properties\":{\"field\":{\"type\":\"text\",\"fields\":{\"subfield\":{\"type\":\"long\"}},\"fielddata\":true}}}}",
Strings.toString(mapperService.documentMapper()));
Strings.toString(mapperService.documentMapper().mapping()));
}
public void testEnableStore() throws IOException {
@ -295,7 +295,7 @@ public class TextFieldMapperTests extends MapperTestCase {
mapping.endObject().endObject().endObject();
DocumentMapper mapper = createDocumentMapper(mapping);
String serialized = Strings.toString(mapper);
String serialized = Strings.toString(mapper.mapping());
assertThat(serialized, containsString("\"offsets\":{\"type\":\"text\",\"index_options\":\"offsets\"}"));
assertThat(serialized, containsString("\"freqs\":{\"type\":\"text\",\"index_options\":\"freqs\"}"));
assertThat(serialized, containsString("\"docs\":{\"type\":\"text\",\"index_options\":\"docs\"}"));
@ -370,7 +370,7 @@ public class TextFieldMapperTests extends MapperTestCase {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
createDocumentMapper(fieldMapping(this::minimalMapping)).toXContent(
createDocumentMapper(fieldMapping(this::minimalMapping)).mapping().toXContent(
builder,
new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true"))
);

View file

@ -94,7 +94,7 @@ public class DateScriptFieldTypeTests extends AbstractNonTextScriptFieldTypeTest
MapperService mapperService = createMapperService(mapping.get());
MappedFieldType fieldType = mapperService.fieldType("field");
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
}
public void testDateWithLocale() throws IOException {
@ -105,7 +105,7 @@ public class DateScriptFieldTypeTests extends AbstractNonTextScriptFieldTypeTest
MapperService mapperService = createMapperService(mapping.get());
MappedFieldType fieldType = mapperService.fieldType("field");
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
}
public void testDateWithLocaleAndFormat() throws IOException {
@ -116,7 +116,7 @@ public class DateScriptFieldTypeTests extends AbstractNonTextScriptFieldTypeTest
MapperService mapperService = createMapperService(mapping.get());
MappedFieldType fieldType = mapperService.fieldType("field");
assertThat(fieldType, instanceOf(DateScriptFieldType.class));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper()));
assertEquals(Strings.toString(mapping.get()), Strings.toString(mapperService.documentMapper().mapping()));
}
public void testFormat() throws IOException {