Fix NPE in RangeFieldMapper.doXContentBody
RangeFieldMapper.doXContentBody should only serialize format and locale when type is set to 'date_range'. closes #22925
This commit is contained in:
parent
71b2655bb3
commit
58c34f0da9
|
@ -405,12 +405,14 @@ public class RangeFieldMapper extends FieldMapper {
|
|||
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
|
||||
super.doXContentBody(builder, includeDefaults, params);
|
||||
|
||||
if (includeDefaults || (fieldType().dateTimeFormatter() != null
|
||||
&& fieldType().dateTimeFormatter().format().equals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format()) == false)) {
|
||||
if (fieldType().rangeType == RangeType.DATE
|
||||
&& (includeDefaults || (fieldType().dateTimeFormatter() != null
|
||||
&& fieldType().dateTimeFormatter().format().equals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format()) == false))) {
|
||||
builder.field("format", fieldType().dateTimeFormatter().format());
|
||||
}
|
||||
if (includeDefaults || (fieldType().dateTimeFormatter() != null
|
||||
&& fieldType().dateTimeFormatter().locale() != Locale.ROOT)) {
|
||||
if (fieldType().rangeType == RangeType.DATE
|
||||
&& (includeDefaults || (fieldType().dateTimeFormatter() != null
|
||||
&& fieldType().dateTimeFormatter().locale() != Locale.ROOT))) {
|
||||
builder.field("locale", fieldType().dateTimeFormatter().locale());
|
||||
}
|
||||
if (includeDefaults || coerce.explicit()) {
|
||||
|
|
|
@ -20,12 +20,14 @@ package org.elasticsearch.index.mapper;
|
|||
|
||||
import org.apache.lucene.index.IndexableField;
|
||||
import org.elasticsearch.common.compress.CompressedXContent;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.elasticsearch.index.query.RangeQueryBuilder.GT_FIELD;
|
||||
import static org.elasticsearch.index.query.RangeQueryBuilder.GTE_FIELD;
|
||||
|
@ -317,4 +319,23 @@ public class RangeFieldMapperTests extends AbstractNumericFieldMapperTestCase {
|
|||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, runnable);
|
||||
assertThat(e.getMessage(), containsString("should not define a dateTimeFormatter"));
|
||||
}
|
||||
|
||||
public void testSerializeDefaults() throws Exception {
|
||||
for (String type : TYPES) {
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.startObject("properties").startObject("field").field("type", type).endObject().endObject()
|
||||
.endObject().endObject().string();
|
||||
|
||||
DocumentMapper docMapper = parser.parse("type", new CompressedXContent(mapping));
|
||||
RangeFieldMapper mapper = (RangeFieldMapper) docMapper.root().getMapper("field");
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
|
||||
mapper.doXContentBody(builder, true, ToXContent.EMPTY_PARAMS);
|
||||
String got = builder.endObject().string();
|
||||
|
||||
// if type is date_range we check that the mapper contains the default format and locale
|
||||
// otherwise it should not contain a locale or format
|
||||
assertTrue(got, got.contains("\"format\":\"strict_date_optional_time||epoch_millis\"") == type.equals("date_range"));
|
||||
assertTrue(got, got.contains("\"locale\":" + "\"" + Locale.ROOT + "\"") == type.equals("date_range"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue