Skip to content

Commit

Permalink
Fix NPE in RangeFieldMapper.doXContentBody
Browse files Browse the repository at this point in the history
RangeFieldMapper.doXContentBody should only serialize format and locale when type is set to 'date_range'.

closes #22925
  • Loading branch information
nknize committed Feb 2, 2017
1 parent ffbe5b9 commit 49864d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Expand Up @@ -405,12 +405,14 @@ protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
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()) {
Expand Down
Expand Up @@ -20,12 +20,14 @@

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;
Expand Down Expand Up @@ -367,4 +369,23 @@ public void testIllegalArguments() throws Exception {
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"));
}
}
}

0 comments on commit 49864d6

Please sign in to comment.