diff --git a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java index 5f5a3d85fe1..0d9810bd405 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java @@ -396,7 +396,11 @@ public class IpFieldMapper extends FieldMapper implements AllFieldMapper.Include super.doXContentBody(builder, includeDefaults, params); if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", InetAddresses.toAddrString((InetAddress) fieldType().nullValue())); + Object nullValue = fieldType().nullValue(); + if (nullValue != null) { + nullValue = InetAddresses.toAddrString((InetAddress) nullValue); + } + builder.field("null_value", nullValue); } if (includeDefaults || ignoreMalformed.explicit()) { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/ip/IpFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/ip/IpFieldMapperTests.java index 25979c5a632..d99b37fc667 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/ip/IpFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/ip/IpFieldMapperTests.java @@ -25,6 +25,8 @@ import org.apache.lucene.index.IndexableField; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.network.InetAddresses; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.DocumentMapper; @@ -269,4 +271,22 @@ public class IpFieldMapperTests extends ESSingleNodeTestCase { assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))), dvField.binaryValue()); assertFalse(dvField.fieldType().stored()); } + + public void testSerializeDefaults() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("field").field("type", "ip").endObject().endObject() + .endObject().endObject().string(); + + DocumentMapper docMapper = parser.parse("type", new CompressedXContent(mapping)); + IpFieldMapper mapper = (IpFieldMapper)docMapper.root().getMapper("field"); + XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); + mapper.doXContentBody(builder, true, ToXContent.EMPTY_PARAMS); + String got = builder.endObject().string(); + + // it would be nice to check the entire serialized default mapper, but there are + // a whole lot of bogus settings right now it picks up from calling super.doXContentBody... + assertTrue(got, got.contains("\"null_value\":null")); + assertTrue(got, got.contains("\"ignore_malformed\":false")); + assertTrue(got, got.contains("\"include_in_all\":false")); + } }