diff --git a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 4d12f2cc35a..95701174759 100644 --- a/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -537,6 +537,11 @@ public class DocumentMapper implements ToXContent { throw (MapperParsingException) e; } + // Throw a more meaningful message if the document is empty. + if (source.source() != null && source.source().length() == 0) { + throw new MapperParsingException("failed to parse, document is empty"); + } + throw new MapperParsingException("failed to parse", e); } finally { // only close the parser when its not provided externally diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/simple/SimpleMapperTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/simple/SimpleMapperTests.java index 002392e49c0..fff1e4c9f0c 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/simple/SimpleMapperTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/simple/SimpleMapperTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperParser; +import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.test.unit.index.mapper.MapperTests; import org.testng.annotations.Test; @@ -113,4 +114,21 @@ public class SimpleMapperTests { DocumentMapper builtDocMapper = MapperTests.newParser().parse(builtMapping); assertThat((String) builtDocMapper.meta().get("param1"), equalTo("value1")); } + + @Test + public void testNoDocumentSent() throws Exception { + DocumentMapperParser mapperParser = MapperTests.newParser(); + DocumentMapper docMapper = doc("test", + rootObject("person") + .add(object("name").add(stringField("first").store(true).index(false))) + ).build(mapperParser); + + BytesReference json = new BytesArray("".getBytes()); + try { + docMapper.parse("person", "1", json).rootDoc(); + assertThat("this point is never reached", false); + } catch (MapperParsingException e) { + assertThat(e.getMessage(), equalTo("failed to parse, document is empty")); + } + } }