Search in sources :

Example 6 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testEmptyObject.

public void testEmptyObject() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object\" : {}}");
    ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser<>("foo", StaticTestStruct::new);
    objectParser.declareObject(StaticTestStruct::setObject, objectParser, new ParseField("object"));
    StaticTestStruct s = objectParser.parse(parser, null);
    assertNotNull(s.object);
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 7 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class BaseXContentTestCase method testNamedObject.

public void testNamedObject() throws IOException {
    Object test1 = new Object();
    Object test2 = new Object();
    NamedXContentRegistry registry = new NamedXContentRegistry(Arrays.asList(new NamedXContentRegistry.Entry(Object.class, new ParseField("test1"), p -> test1), new NamedXContentRegistry.Entry(Object.class, new ParseField("test2", "deprecated"), p -> test2), new NamedXContentRegistry.Entry(Object.class, new ParseField("str"), p -> p.text())));
    XContentBuilder b = XContentBuilder.builder(xcontentType().xContent());
    b.value("test");
    XContentParser p = xcontentType().xContent().createParser(registry, b.bytes());
    assertEquals(test1, p.namedObject(Object.class, "test1", null));
    assertEquals(test2, p.namedObject(Object.class, "test2", null));
    assertEquals(test2, p.namedObject(Object.class, "deprecated", null));
    assertWarnings("Deprecated field [deprecated] used, expected [test2] instead");
    {
        p.nextToken();
        assertEquals("test", p.namedObject(Object.class, "str", null));
        NamedXContentRegistry.UnknownNamedObjectException e = expectThrows(NamedXContentRegistry.UnknownNamedObjectException.class, () -> p.namedObject(Object.class, "unknown", null));
        assertEquals("Unknown Object [unknown]", e.getMessage());
        assertEquals("java.lang.Object", e.getCategoryClass());
        assertEquals("unknown", e.getName());
    }
    {
        Exception e = expectThrows(ElasticsearchException.class, () -> p.namedObject(String.class, "doesn't matter", null));
        assertEquals("Unknown namedObject category [java.lang.String]", e.getMessage());
    }
    {
        XContentParser emptyRegistryParser = xcontentType().xContent().createParser(NamedXContentRegistry.EMPTY, new byte[] {});
        Exception e = expectThrows(ElasticsearchException.class, () -> emptyRegistryParser.namedObject(String.class, "doesn't matter", null));
        assertEquals("namedObject is not supported for this parser", e.getMessage());
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) ParseField(org.elasticsearch.common.ParseField) ElasticsearchException(org.elasticsearch.ElasticsearchException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException)

Example 8 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ConstructingObjectParserTests method testNullDeclares.

public void testNullDeclares() {
    ConstructingObjectParser<Void, Void> objectParser = new ConstructingObjectParser<>("foo", a -> null);
    Exception e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[consumer] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (ContextParser<Void, Object>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (CheckedFunction<XContentParser, Object, IOException>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, null, ObjectParser.ValueType.STRING));
    assertEquals("[parseField] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, new ParseField("test"), null));
    assertEquals("[type] is required", e.getMessage());
}
Also used : Arrays(java.util.Arrays) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ConstructingObjectParser.constructorArg(org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg) Nullable(org.elasticsearch.common.Nullable) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CheckedFunction(org.elasticsearch.common.CheckedFunction) List(java.util.List) Matcher(org.hamcrest.Matcher) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ConstructingObjectParser.optionalConstructorArg(org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg) ESTestCase(org.elasticsearch.test.ESTestCase) ParseField(org.elasticsearch.common.ParseField) Matchers.anyOf(org.hamcrest.Matchers.anyOf) IOException(java.io.IOException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) ParseField(org.elasticsearch.common.ParseField)

Example 9 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testNullDeclares.

public void testNullDeclares() {
    ObjectParser<Void, Void> objectParser = new ObjectParser<>("foo");
    Exception e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[consumer] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (ContextParser<Void, Object>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (CheckedFunction<XContentParser, Object, IOException>) null, new ParseField("test"), ObjectParser.ValueType.STRING));
    assertEquals("[parser] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, null, ObjectParser.ValueType.STRING));
    assertEquals("[parseField] is required", e.getMessage());
    e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField((o, v) -> {
    }, (r, c) -> null, new ParseField("test"), null));
    assertEquals("[type] is required", e.getMessage());
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) Arrays(java.util.Arrays) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) CheckedFunction(org.elasticsearch.common.CheckedFunction) NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) List(java.util.List) ValueType(org.elasticsearch.common.xcontent.ObjectParser.ValueType) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) URI(java.net.URI) ParseField(org.elasticsearch.common.ParseField) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ParseField(org.elasticsearch.common.ParseField)

Example 10 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testEmptyObjectInArray.

public void testEmptyObjectInArray() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object_array\" : [{}]}");
    ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser<>("foo", StaticTestStruct::new);
    objectParser.declareObjectArray(StaticTestStruct::setObjectArray, objectParser, new ParseField("object_array"));
    StaticTestStruct s = objectParser.parse(parser, null);
    assertNotNull(s.objectArray);
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Aggregations

ParseField (org.elasticsearch.common.ParseField)31 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)6 List (java.util.List)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 ArrayList (java.util.ArrayList)4 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)4 UncheckedIOException (java.io.UncheckedIOException)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 Map (java.util.Map)2 CheckedFunction (org.elasticsearch.common.CheckedFunction)2 Nullable (org.elasticsearch.common.Nullable)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 ConstructingObjectParser.constructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg)2 ConstructingObjectParser.optionalConstructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg)2 ValueType (org.elasticsearch.common.xcontent.ObjectParser.ValueType)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2