Added tests for malformed mappings with no root object
This commit also makes the error message more consistent with other exception messages in the DocumentMapperParser.
This commit is contained in:
parent
27518b5e41
commit
f2dc4f810c
|
@ -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();
|
||||
|
|
|
@ -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,15 +136,23 @@ 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
|
||||
|
|
Loading…
Reference in New Issue