|
@@ -34,6 +34,7 @@ import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
|
@@ -650,6 +651,49 @@ public class ObjectParserTests extends ESTestCase {
|
|
|
assertThat(ex.getMessage(), containsString("[foo] failed to parse field [int_array]"));
|
|
|
}
|
|
|
|
|
|
+ public void testNoopDeclareObject() throws IOException {
|
|
|
+ ObjectParser<AtomicReference<String>, Void> parser = new ObjectParser<>("noopy", AtomicReference::new);
|
|
|
+ parser.declareString(AtomicReference::set, new ParseField("body"));
|
|
|
+ parser.declareObject((a,b) -> {}, (p, c) -> null, new ParseField("noop"));
|
|
|
+
|
|
|
+ assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get());
|
|
|
+ Exception garbageException = expectThrows(IllegalStateException.class, () -> parser.parse(
|
|
|
+ createParser(JsonXContent.jsonXContent, "{\"noop\": {\"garbage\": \"shouldn't\"}}"),
|
|
|
+ null));
|
|
|
+ assertEquals("parser for [noop] did not end on END_OBJECT", garbageException.getMessage());
|
|
|
+ Exception sneakyException = expectThrows(IllegalStateException.class, () -> parser.parse(
|
|
|
+ createParser(JsonXContent.jsonXContent, "{\"noop\": {\"body\": \"shouldn't\"}}"),
|
|
|
+ null));
|
|
|
+ assertEquals("parser for [noop] did not end on END_OBJECT", sneakyException.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNoopDeclareField() throws IOException {
|
|
|
+ ObjectParser<AtomicReference<String>, Void> parser = new ObjectParser<>("noopy", AtomicReference::new);
|
|
|
+ parser.declareString(AtomicReference::set, new ParseField("body"));
|
|
|
+ parser.declareField((a,b) -> {}, (p, c) -> null, new ParseField("noop"), ValueType.STRING_ARRAY);
|
|
|
+
|
|
|
+ assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get());
|
|
|
+ Exception e = expectThrows(IllegalStateException.class, () -> parser.parse(
|
|
|
+ createParser(JsonXContent.jsonXContent, "{\"noop\": [\"ignored\"]}"),
|
|
|
+ null));
|
|
|
+ assertEquals("parser for [noop] did not end on END_ARRAY", e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNoopDeclareObjectArray() throws IOException {
|
|
|
+ ObjectParser<AtomicReference<String>, Void> parser = new ObjectParser<>("noopy", AtomicReference::new);
|
|
|
+ parser.declareString(AtomicReference::set, new ParseField("body"));
|
|
|
+ parser.declareObjectArray((a,b) -> {}, (p, c) -> null, new ParseField("noop"));
|
|
|
+
|
|
|
+ XContentParseException garbageError = expectThrows(XContentParseException.class, () -> parser.parse(
|
|
|
+ createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"garbage\": \"shouldn't\"}}]"),
|
|
|
+ null));
|
|
|
+ assertEquals("expected value but got [FIELD_NAME]", garbageError.getCause().getMessage());
|
|
|
+ XContentParseException sneakyError = expectThrows(XContentParseException.class, () -> parser.parse(
|
|
|
+ createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"body\": \"shouldn't\"}}]"),
|
|
|
+ null));
|
|
|
+ assertEquals("expected value but got [FIELD_NAME]", sneakyError.getCause().getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
static class NamedObjectHolder {
|
|
|
public static final ObjectParser<NamedObjectHolder, Void> PARSER = new ObjectParser<>("named_object_holder",
|
|
|
NamedObjectHolder::new);
|