Search in sources :

Example 26 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ObjectParserTests method testExclusiveFieldSet.

public void testExclusiveFieldSet() throws IOException {
    XContentParser goodA = createParser(JsonXContent.jsonXContent, "{\"a\" : 1, \"c\" : 4}");
    XContentParser bad = createParser(JsonXContent.jsonXContent, "{\"a\" : 1, \"b\" : 2}");
    XContentParser badmulti = createParser(JsonXContent.jsonXContent, "{\"a\" : 1, \"b\" : 2, \"c\" : 3, \"d\" : 4 }");
    ObjectParser<TestStruct, Void> parser = new ObjectParser<>("foo", TestStruct::new);
    parser.declareLong(TestStruct::setA, new ParseField("a"));
    parser.declareLong(TestStruct::setB, new ParseField("b"));
    parser.declareLong(TestStruct::setC, new ParseField("c"));
    parser.declareLong(TestStruct::setD, new ParseField("d"));
    parser.declareExclusiveFieldSet("a", "b");
    parser.declareExclusiveFieldSet("c", "d");
    TestStruct actualA = parser.parse(goodA, null);
    assertThat(actualA.a, equalTo(1L));
    assertThat(actualA.c, equalTo(4L));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse(bad, null));
    assertThat(e.getMessage(), containsString("The following fields are not allowed together: [a, b]"));
    e = expectThrows(IllegalArgumentException.class, () -> parser.parse(badmulti, null));
    assertThat(e.getMessage(), containsString("allowed together: [a, b] The following fields are not allowed together: [c, d]"));
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.opensearch.common.ParseField)

Example 27 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ObjectParserTests method testConsumeUnknownFields.

public void testConsumeUnknownFields() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + "  \"test\" : \"foo\",\n" + "  \"test_number\" : 2,\n" + "  \"name\" : \"geoff\",\n" + "  \"test_boolean\" : true,\n" + "  \"test_null\" : null,\n" + "  \"test_array\":  [1,2,3,4],\n" + "  \"test_nested\": { \"field\" : \"value\", \"field2\" : [ \"list1\", \"list2\" ] }\n" + "}");
    ObjectParser<ObjectWithArbitraryFields, Void> op = new ObjectParser<>("unknown", ObjectWithArbitraryFields::setField, ObjectWithArbitraryFields::new);
    op.declareString(ObjectWithArbitraryFields::setName, new ParseField("name"));
    ObjectWithArbitraryFields o = op.parse(parser, null);
    assertEquals("geoff", o.name);
    assertEquals(6, o.fields.size());
    assertEquals("foo", o.fields.get("test"));
    assertEquals(2, o.fields.get("test_number"));
    assertEquals(true, o.fields.get("test_boolean"));
    assertNull(o.fields.get("test_null"));
    assertThat(o.fields.get("test_array"), instanceOf(List.class));
    assertThat(o.fields.get("test_nested"), instanceOf(Map.class));
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ParseField(org.opensearch.common.ParseField)

Example 28 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

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.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.opensearch.common.ParseField)

Example 29 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ObjectParserTests method testIgnoreUnknownFields.

public void testIgnoreUnknownFields() throws IOException {
    XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
    b.startObject();
    {
        b.field("test", "foo");
        b.field("junk", 2);
    }
    b.endObject();
    b = shuffleXContent(b);
    b.flush();
    byte[] bytes = ((ByteArrayOutputStream) b.getOutputStream()).toByteArray();
    XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, null);
    objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
    TestStruct s = objectParser.parse(parser, new TestStruct(), null);
    assertEquals(s.test, "foo");
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ParseField(org.opensearch.common.ParseField)

Example 30 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ObjectParserTests method testIgnoreUnknownObjects.

public void testIgnoreUnknownObjects() throws IOException {
    XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
    b.startObject();
    {
        b.field("test", "foo");
        b.startObject("junk");
        {
            b.field("really", "junk");
        }
        b.endObject();
    }
    b.endObject();
    b = shuffleXContent(b);
    b.flush();
    byte[] bytes = ((ByteArrayOutputStream) b.getOutputStream()).toByteArray();
    XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, null);
    objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
    TestStruct s = objectParser.parse(parser, new TestStruct(), null);
    assertEquals(s.test, "foo");
}
Also used : NamedObjectParser(org.opensearch.common.xcontent.ObjectParser.NamedObjectParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ParseField(org.opensearch.common.ParseField)

Aggregations

ParseField (org.opensearch.common.ParseField)64 NamedObjectParser (org.opensearch.common.xcontent.ObjectParser.NamedObjectParser)26 Matchers.containsString (org.hamcrest.Matchers.containsString)15 List (java.util.List)13 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)9 Map (java.util.Map)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 UncheckedIOException (java.io.UncheckedIOException)5 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)5 Aggregation (org.opensearch.search.aggregations.Aggregation)5 HashMap (java.util.HashMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Arrays (java.util.Arrays)3 CheckedFunction (org.opensearch.common.CheckedFunction)3 ConstructingObjectParser (org.opensearch.common.xcontent.ConstructingObjectParser)3 NamedObject (org.opensearch.common.xcontent.ObjectParserTests.NamedObject)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 JsonXContent (org.opensearch.common.xcontent.json.JsonXContent)3 URI (java.net.URI)2