Skip to content

Commit

Permalink
Check if root mapping is actually valid
Browse files Browse the repository at this point in the history
When a mapping is declared and the type is known from the uri
then the type can be skipped in the body (see elastic#4483). However,
there was no check if the given keys actually make a valid mapping.

closes elastic#5864
  • Loading branch information
brwe committed May 8, 2014
1 parent bac0627 commit becc208
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
Expand Up @@ -285,9 +285,18 @@ private Tuple<String, Map<String, Object>> extractMapping(String type, Map<Strin
if (type == null || type.equals(rootName)) {
mapping = new Tuple<>(rootName, (Map<String, Object>) root.get(rootName));
} else {
checkRootKeysValid(root);
mapping = new Tuple<>(type, root);
}

return mapping;
}

private void checkRootKeysValid(Map<String, Object> root) {
for (String key : root.keySet()) {
if (!rootTypeParsers.containsKey(key) && !key.equals("properties")) {
throw new MapperParsingException("Got unrecognized key "+ key + " in root of mapping for type ");
}
}
}
}
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.lucene.all.AllEntries;
Expand All @@ -33,18 +34,15 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperTestUtils;
import org.elasticsearch.index.mapper.*;
import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.mapper.internal.*;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;

import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
Expand Down Expand Up @@ -323,4 +321,42 @@ public void testMultiField_defaults() throws IOException {
assertThat(allEntries.fields(), hasSize(1));
assertThat(allEntries.fields(), hasItem("foo.bar"));
}

// issue https://github.com/elasticsearch/elasticsearch/issues/5864
@Test(expected = MapperParsingException.class)
public void testMisplacedMappingAsRoot() throws IOException {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/misplaced_mapping_key_in_root.json");
DocumentMapper docMapper = MapperTestUtils.newParser().parse("test", mapping);
}

// issue https://github.com/elasticsearch/elasticsearch/issues/5864
@Test(expected = MapperParsingException.class)
public void testMisplacedTypeInRoot() throws IOException {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/misplaced_type_in_root.json");
DocumentMapper docMapper = MapperTestUtils.newParser().parse("test", mapping);
}

// issue https://github.com/elasticsearch/elasticsearch/issues/5864
@Test(expected = MapperParsingException.class)
public void testMistypedTypeInRoot() throws IOException {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/all/mistyped_type_in_root.json");
DocumentMapper docMapper = MapperTestUtils.newParser().parse("test", mapping);
}

// issue https://github.com/elasticsearch/elasticsearch/issues/5864
@Test
public void testRootMappersStillWorking() {
String mapping = "{";
Map<String, String> rootTypes = new HashMap<>();
//just pick some example from DocumentMapperParser.rootTypeParsers
rootTypes.put(SizeFieldMapper.NAME, "{\"enabled\" : true}");
rootTypes.put(IndexFieldMapper.NAME, "{\"enabled\" : true}");
rootTypes.put(SourceFieldMapper.NAME, "{\"enabled\" : true}");
rootTypes.put(TypeFieldMapper.NAME, "{\"store\" : true}");
for (String key : rootTypes.keySet()) {
mapping += "\"" + key+ "\"" + ":" + rootTypes.get(key) + ",\n";
}
mapping += "\"properties\":{}}" ;
MapperTestUtils.newParser().parse("test", mapping);
}
}
@@ -0,0 +1,11 @@
{
"mapping": {
"test": {
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
@@ -0,0 +1,8 @@
{
"type": "string",
"properties": {
"foo": {
"type": "string"
}
}
}
@@ -0,0 +1,9 @@
{
"testX": {
"properties": {
"foo": {
"type": "string"
}
}
}
}

0 comments on commit becc208

Please sign in to comment.