Search in sources :

Example 1 with NamedObject

use of org.opensearch.common.xcontent.ObjectParserTests.NamedObject in project OpenSearch by opensearch-project.

the class ConstructingObjectParserTests method testParseNamedObjectInOrderNotSupported.

public void testParseNamedObjectInOrderNotSupported() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + "  {\"a\": {}}" + "],\"named_in_constructor\": {\"b\": {}}" + "}");
    // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
    @SuppressWarnings("unchecked") ConstructingObjectParser<NamedObjectHolder, Void> objectParser = new ConstructingObjectParser<>("named_object_holder", a -> new NamedObjectHolder(((List<NamedObject>) a[0])));
    objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, new ParseField("named_in_constructor"));
    objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
    // Now firing the xml through it fails
    XContentParseException e = expectThrows(XContentParseException.class, () -> objectParser.apply(parser, null));
    assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]"));
    assertEquals("[named] doesn't support arrays. Use a single object with multiple fields.", e.getCause().getMessage());
}
Also used : NamedObject(org.opensearch.common.xcontent.ObjectParserTests.NamedObject) ParseField(org.opensearch.common.ParseField)

Example 2 with NamedObject

use of org.opensearch.common.xcontent.ObjectParserTests.NamedObject in project OpenSearch by opensearch-project.

the class ConstructingObjectParserTests method testParseNamedObjectInOrderNotSupportedConstructorArg.

public void testParseNamedObjectInOrderNotSupportedConstructorArg() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": {\"a\": {}}, \"named_in_constructor\": [ {\"b\": {}} ]}");
    // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
    @SuppressWarnings("unchecked") ConstructingObjectParser<NamedObjectHolder, Void> objectParser = new ConstructingObjectParser<>("named_object_holder", a -> new NamedObjectHolder(((List<NamedObject>) a[0])));
    objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, new ParseField("named_in_constructor"));
    objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
    // Now firing the xml through it fails
    XContentParseException e = expectThrows(XContentParseException.class, () -> objectParser.apply(parser, null));
    assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]"));
    assertThat(e.getCause().getMessage(), containsString("[named_in_constructor] doesn't support arrays. Use a single object with multiple fields."));
}
Also used : NamedObject(org.opensearch.common.xcontent.ObjectParserTests.NamedObject) ParseField(org.opensearch.common.ParseField)

Aggregations

ParseField (org.opensearch.common.ParseField)2 NamedObject (org.opensearch.common.xcontent.ObjectParserTests.NamedObject)2