Skip to content

Commit

Permalink
Fail in metadata parsing if the id path is not a value but rather an …
Browse files Browse the repository at this point in the history
…array or an object.

Closes #2275
  • Loading branch information
s1monw committed Mar 1, 2013
1 parent b03f3fc commit 3c1f291
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -442,6 +443,9 @@ private void innerParse(XContentParser parser, ParseContext context) throws IOEx
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 {
Expand Down
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -49,6 +50,33 @@ public void testParseIdAlone() throws Exception {
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 {
Expand Down

0 comments on commit 3c1f291

Please sign in to comment.