handle EOF when handling arrays as well

This commit is contained in:
Shay Banon 2012-09-05 11:40:39 +02:00
parent 26feea8a58
commit 24ce2ef537
3 changed files with 75 additions and 4 deletions

View File

@ -384,6 +384,10 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
return this.dynamic; return this.dynamic;
} }
protected boolean allowValue() {
return true;
}
public void parse(ParseContext context) throws IOException { public void parse(ParseContext context) throws IOException {
if (!enabled) { if (!enabled) {
context.parser().skipChildren(); context.parser().skipChildren();
@ -398,9 +402,10 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
return; return;
} }
if (token.isValue()) { if (token.isValue() && !allowValue()) {
// if we are parsing an object but it is just a value // if we are parsing an object but it is just a value, its only allowed on root level parsers with there
throw new MapperParsingException("object mapping for [" + name + "] tried to parse as object, but found a concrete value"); // is a field name with the same name as the type
throw new MapperParsingException("object mapping for [" + name + "] tried to parse as object, but found a concrete value");
} }
Document restoreDoc = null; Document restoreDoc = null;
@ -559,6 +564,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
} }
private void serializeArray(ParseContext context, String lastFieldName) throws IOException { private void serializeArray(ParseContext context, String lastFieldName) throws IOException {
String arrayFieldName = lastFieldName;
Mapper mapper = mappers.get(lastFieldName); Mapper mapper = mappers.get(lastFieldName);
if (mapper != null && mapper instanceof ArrayValueMapperParser) { if (mapper != null && mapper instanceof ArrayValueMapperParser) {
mapper.parse(context); mapper.parse(context);
@ -574,6 +580,8 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
lastFieldName = parser.currentName(); lastFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_NULL) { } else if (token == XContentParser.Token.VALUE_NULL) {
serializeNullValue(context, lastFieldName); serializeNullValue(context, lastFieldName);
} else if (token == null) {
throw new MapperParsingException("object mapping for [" + name + "] with array for [" + arrayFieldName + "] tried to parse as array, but got EOF, is there a mismatch in types for the same field?");
} else { } else {
serializeValue(context, lastFieldName, token); serializeValue(context, lastFieldName, token);
} }

View File

@ -227,6 +227,11 @@ public class RootObjectMapper extends ObjectMapper {
return null; return null;
} }
@Override
protected boolean allowValue() {
return true;
}
@Override @Override
protected void doMerge(ObjectMapper mergeWith, MergeContext mergeContext) { protected void doMerge(ObjectMapper mergeWith, MergeContext mergeContext) {
RootObjectMapper mergeWithObject = (RootObjectMapper) mergeWith; RootObjectMapper mergeWithObject = (RootObjectMapper) mergeWith;

View File

@ -0,0 +1,58 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.test.unit.index.mapper.object;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;
/**
*/
@Test
public class SimpleObjectMappingTests {
public void testDifferentInnerObjectTokenFailure() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.endObject().endObject().string();
DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
try {
defaultMapper.parse("type", "1", new BytesArray(" {\n" +
" \"object\": {\n" +
" \"array\":[\n" +
" {\n" +
" \"object\": { \"value\": \"value\" }\n" +
" },\n" +
" {\n" +
" \"object\":\"value\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"value\":\"value\"\n" +
" }"));
assert false;
} catch (MapperParsingException e) {
// all is well
}
}
}