use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
the class NodesResponse method declareCommonNodesResponseParsing.
public static <T extends NodesResponse> void declareCommonNodesResponseParsing(ConstructingObjectParser<T, Void> parser) {
parser.declareObject(ConstructingObjectParser.constructorArg(), NodesResponseHeader::fromXContent, new ParseField("_nodes"));
parser.declareString(ConstructingObjectParser.constructorArg(), new ParseField("cluster_name"));
}
use of org.opensearch.common.ParseField 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());
}
use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
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());
}
use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.
the class ConstructingObjectParserTests method testRequiredAndExclusiveFields.
public void testRequiredAndExclusiveFields() throws IOException {
class TestStruct {
final String a;
final long b;
TestStruct(String a) {
this.a = a;
this.b = 0;
}
TestStruct(long b) {
this.a = null;
this.b = b;
}
}
XContentParser ok = createParser(JsonXContent.jsonXContent, "{ \"a\" : \"a\" }");
XContentParser toomany = createParser(JsonXContent.jsonXContent, "{ \"a\" : \"a\", \"b\" : 1 }");
XContentParser notenough = createParser(JsonXContent.jsonXContent, "{ }");
ConstructingObjectParser<TestStruct, Void> parser = new ConstructingObjectParser<>("teststruct", args -> {
if (args[0] != null) {
return new TestStruct((String) args[0]);
}
return new TestStruct((Long) args[1]);
});
parser.declareString(optionalConstructorArg(), new ParseField("a"));
parser.declareLong(optionalConstructorArg(), new ParseField("b"));
parser.declareExclusiveFieldSet("a", "b");
parser.declareRequiredFieldSet("a", "b");
TestStruct actual = parser.parse(ok, null);
assertThat(actual.a, equalTo("a"));
assertThat(actual.b, equalTo(0L));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse(toomany, null));
assertThat(e.getMessage(), containsString("allowed together: [a, b]"));
e = expectThrows(IllegalArgumentException.class, () -> parser.parse(notenough, null));
assertThat(e.getMessage(), containsString("Required one of fields [a, b], but none were specified."));
}
use of org.opensearch.common.ParseField 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."));
}
Aggregations