Fail in metadata parsing if the id path is not a value but rather an array or an object.

Closes #2275
This commit is contained in:
Simon Willnauer 2013-03-01 13:00:29 +01:00
parent b03f3fcd6c
commit 3c1f291801
2 changed files with 32 additions and 0 deletions

View File

@ -33,6 +33,7 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.internal.TimestampFieldMapper;
import java.io.IOException;
@ -442,6 +443,9 @@ public class MappingMetaData {
boolean incLocationTimestamp = false;
if (context.idParsingStillNeeded() && fieldName.equals(idPart)) {
if (context.locationId + 1 == id.pathElements().length) {
if (!t.isValue()) {
throw new MapperParsingException("id field must be a value but was either an object or an array");
}
context.id = parser.textOrNull();
context.idResolved = true;
} else {

View File

@ -22,6 +22,7 @@ package org.elasticsearch.test.unit.cluster.metadata;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.compress.CompressedString;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.testng.annotations.Test;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -49,6 +50,33 @@ public class MappingMetaDataParserTests {
assertThat(parseContext.timestamp(), nullValue());
assertThat(parseContext.timestampResolved(), equalTo(false));
}
@Test
public void testFailIfIdIsNoValue() throws Exception {
MappingMetaData md = new MappingMetaData("type1", new CompressedString(""),
new MappingMetaData.Id("id"),
new MappingMetaData.Routing(true, "routing"),
new MappingMetaData.Timestamp(true, "timestamp", "dateOptionalTime"));
byte[] bytes = jsonBuilder().startObject().field("field1", "value1").field("field2", "value2")
.startArray("id").value("id").endArray().field("routing", "routing_value").field("timestamp", "1").endObject().bytes().toBytes();
MappingMetaData.ParseContext parseContext = md.createParseContext(null, "routing_value", "1");
try {
md.parse(XContentFactory.xContent(bytes).createParser(bytes), parseContext);
assert false;
} catch (MapperParsingException ex) {
// bogus its an array
}
bytes = jsonBuilder().startObject().field("field1", "value1").field("field2", "value2")
.startObject("id").field("x", "id").endObject().field("routing", "routing_value").field("timestamp", "1").endObject().bytes().toBytes();
parseContext = md.createParseContext(null, "routing_value", "1");
try {
md.parse(XContentFactory.xContent(bytes).createParser(bytes), parseContext);
assert false;
} catch (MapperParsingException ex) {
// bogus its an object
}
}
@Test
public void testParseRoutingAlone() throws Exception {