Allow creation of empty docs, closes #1195.

This commit is contained in:
Shay Banon 2011-08-03 12:36:05 +03:00
parent 50ccb665a0
commit 598370b6c7
1 changed files with 9 additions and 3 deletions

View File

@ -519,11 +519,15 @@ public class DocumentMapper implements ToXContent {
if (token != XContentParser.Token.START_OBJECT) { if (token != XContentParser.Token.START_OBJECT) {
throw new MapperParsingException("Malformed content, must start with an object"); throw new MapperParsingException("Malformed content, must start with an object");
} }
boolean emptyDoc = false;
token = parser.nextToken(); token = parser.nextToken();
if (token != XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.END_OBJECT) {
// empty doc, we can handle it...
emptyDoc = true;
} else if (token != XContentParser.Token.FIELD_NAME) {
throw new MapperParsingException("Malformed content, after first object, either the type field or the actual properties should exist"); throw new MapperParsingException("Malformed content, after first object, either the type field or the actual properties should exist");
} }
if (parser.currentName().equals(type)) { if (type.equals(parser.currentName())) {
// first field is the same as the type, this might be because the type is provided, and the object exists within it // first field is the same as the type, this might be because the type is provided, and the object exists within it
// or because there is a valid field that by chance is named as the type // or because there is a valid field that by chance is named as the type
@ -558,7 +562,9 @@ public class DocumentMapper implements ToXContent {
indexFieldMapper.parse(context); indexFieldMapper.parse(context);
if (!emptyDoc) {
rootObjectMapper.parse(context); rootObjectMapper.parse(context);
}
for (int i = 0; i < countDownTokens; i++) { for (int i = 0; i < countDownTokens; i++) {
parser.nextToken(); parser.nextToken();