Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception when content _id is an object #3586

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ protected Field parseCreateField(ParseContext context) throws IOException {
return null;
}
return new Field(names.indexName(), context.id(), fieldType);
} else if (parser.currentName() != null && parser.currentName().equals(Defaults.NAME) && parser.currentToken().equals(XContentParser.Token.START_OBJECT)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could remove the null check by changing the second check to Defaults.NAME.equals(parser.currentName())

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first check should be:

if (parser.currentName() != null && parser.currentName().equals(Defaults.NAME)) {
  if (parser.currentToken().isValue() == false) {
    throw new MapperParsingException("Expected a value as Content id but got " + parser.currentToken());
  }
/....
}

that way we also fail for arrays etc.

throw new MapperParsingException("Content id cannot be an object.");
} else {
// we are in the pre/post parse phase
if (!fieldType.indexed() && !fieldType.stored()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.test.unit.index.mapper.id;

import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
Expand Down Expand Up @@ -114,4 +115,22 @@ public void testIdPath() throws Exception {

assertThat(serialized_id_mapping, equalTo(expected_id_mapping));
}

@Test
public void testObjectId() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.endObject().endObject().string();
DocumentMapper docMapper = MapperTestUtils.newParser().parse(mapping);

BytesReference source = XContentFactory.jsonBuilder().startObject()
.startObject("_id").field("name", "test").endObject()
.endObject().bytes();
try {
ParsedDocument doc = docMapper.parse("type", "1", source);
assert false;
} catch (MapperParsingException e) {
assert true;
}

}
}