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) {
|
} catch (Exception e) {
|
||||||
throw new MapperParsingException("failed to parse mapping definition", e);
|
throw new MapperParsingException("failed to parse mapping definition", e);
|
||||||
}
|
}
|
||||||
|
int size = root.size();
|
||||||
if (root.keySet().size() == 0) {
|
switch (size) {
|
||||||
throw new MapperParsingException("malformed mapping definition: no JSON root object found");
|
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
|
// we always assume the first and single key is the mapping type root
|
||||||
if (root.keySet().size() != 1) {
|
|
||||||
throw new MapperParsingException("mapping must have the `type` as the root object");
|
throw new MapperParsingException("mapping must have the `type` as the root object");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String rootName = root.keySet().iterator().next();
|
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.MapperParsingException;
|
||||||
import org.elasticsearch.index.mapper.MapperService;
|
import org.elasticsearch.index.mapper.MapperService;
|
||||||
import org.elasticsearch.index.mapper.ParsedDocument;
|
import org.elasticsearch.index.mapper.ParsedDocument;
|
||||||
|
import org.elasticsearch.test.integration.ElasticsearchTestCase;
|
||||||
import org.elasticsearch.test.unit.index.mapper.MapperTestUtils;
|
import org.elasticsearch.test.unit.index.mapper.MapperTestUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class DefaultSourceMappingTests {
|
public class DefaultSourceMappingTests extends ElasticsearchTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoFormat() throws Exception {
|
public void testNoFormat() throws Exception {
|
||||||
|
@ -136,15 +136,23 @@ public class DefaultSourceMappingTests {
|
||||||
DocumentMapper mapper = MapperTestUtils.newParser().parse("my_type", null, defaultMapping);
|
DocumentMapper mapper = MapperTestUtils.newParser().parse("my_type", null, defaultMapping);
|
||||||
assertThat(mapper.type(), equalTo("my_type"));
|
assertThat(mapper.type(), equalTo("my_type"));
|
||||||
assertThat(mapper.sourceMapper().enabled(), equalTo(false));
|
assertThat(mapper.sourceMapper().enabled(), equalTo(false));
|
||||||
|
|
||||||
try {
|
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.type(), equalTo("my_type"));
|
||||||
assertThat(mapper.sourceMapper().enabled(), equalTo(false));
|
assertThat(mapper.sourceMapper().enabled(), equalTo(false));
|
||||||
assert false;
|
assert false;
|
||||||
} catch (MapperParsingException e) {
|
} catch (MapperParsingException e) {
|
||||||
// all is well
|
// 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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue