diff --git a/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java b/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java index a2b5759deab..f053c4f89b6 100644 --- a/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java +++ b/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java @@ -241,14 +241,17 @@ public class DocumentMapperParser extends AbstractIndexComponent { } catch (Exception e) { throw new MapperParsingException("failed to parse mapping definition", e); } - - if (root.keySet().size() == 0) { - throw new MapperParsingException("malformed mapping definition: no JSON root object found"); - } - - // we always assume the first and single key is the mapping type root - if (root.keySet().size() != 1) { + int size = root.size(); + switch (size) { + case 0: + // if we don't have any keys throw an exception + throw new MapperParsingException("malformed mapping no root object found"); + case 1: + break; + default: + // we always assume the first and single key is the mapping type root throw new MapperParsingException("mapping must have the `type` as the root object"); + } String rootName = root.keySet().iterator().next(); diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/source/DefaultSourceMappingTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/source/DefaultSourceMappingTests.java index c3021347aae..65da32fc779 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/source/DefaultSourceMappingTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/source/DefaultSourceMappingTests.java @@ -28,18 +28,18 @@ import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ParsedDocument; +import org.elasticsearch.test.integration.ElasticsearchTestCase; import org.elasticsearch.test.unit.index.mapper.MapperTestUtils; import org.junit.Test; import java.util.Map; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; /** * */ -public class DefaultSourceMappingTests { +public class DefaultSourceMappingTests extends ElasticsearchTestCase { @Test public void testNoFormat() throws Exception { @@ -136,17 +136,25 @@ public class DefaultSourceMappingTests { DocumentMapper mapper = MapperTestUtils.newParser().parse("my_type", null, defaultMapping); assertThat(mapper.type(), equalTo("my_type")); assertThat(mapper.sourceMapper().enabled(), equalTo(false)); - try { - mapper = MapperTestUtils.newParser().parse(null, null, defaultMapping); + mapper = MapperTestUtils.newParser().parse(null, getRandom().nextBoolean() ? null : "", defaultMapping); assertThat(mapper.type(), equalTo("my_type")); assertThat(mapper.sourceMapper().enabled(), equalTo(false)); assert false; } catch (MapperParsingException e) { // all is well } + try { + mapper = MapperTestUtils.newParser().parse(null, "{}", defaultMapping); + assertThat(mapper.type(), equalTo("my_type")); + assertThat(mapper.sourceMapper().enabled(), equalTo(false)); + assert false; + } catch (MapperParsingException e) { + assertThat(e.getMessage(), equalTo("malformed mapping no root object found")); + // all is well + } } - + @Test public void testDefaultMappingAndWithMappingOverride() throws Exception { String defaultMapping = XContentFactory.jsonBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING)