From eb1a72f93e62ed89ea99b83bb3e61faefc10ec9b Mon Sep 17 00:00:00 2001 From: Yaliang Wu Date: Thu, 3 Aug 2023 23:36:50 -0700 Subject: [PATCH] fix parameter parsing bug for create connector input (#1185) Signed-off-by: Yaliang Wu --- .../connector/MLCreateConnectorInput.java | 3 ++- .../MLCreateConnectorInputTests.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInput.java b/common/src/main/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInput.java index 43568afb05..a4e9f83b40 100644 --- a/common/src/main/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInput.java +++ b/common/src/main/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInput.java @@ -24,6 +24,7 @@ import java.util.Map; import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; +import static org.opensearch.ml.common.utils.StringUtils.getParameterMap; @Data public class MLCreateConnectorInput implements ToXContentObject, Writeable { @@ -125,7 +126,7 @@ public static MLCreateConnectorInput parse(XContentParser parser) throws IOExcep protocol = parser.text(); break; case CONNECTOR_PARAMETERS_FIELD: - parameters = parser.mapStrings(); + parameters = getParameterMap(parser.map()); break; case CONNECTOR_CREDENTIAL_FIELD: credential = parser.mapStrings(); diff --git a/common/src/test/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInputTests.java b/common/src/test/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInputTests.java index c037da1529..86b330dc00 100644 --- a/common/src/test/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInputTests.java +++ b/common/src/test/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorInputTests.java @@ -171,6 +171,25 @@ public void testParse() throws Exception { }); } + @Test + public void testParse_ArrayParameter() throws Exception { + String expectedInputStr = "{\"name\":\"test_connector_name\"," + + "\"description\":\"this is a test connector\",\"version\":\"1\",\"protocol\":\"http\"," + + "\"parameters\":{\"input\":[\"test input value\"]},\"credential\":{\"key\":\"test_key_value\"}," + + "\"actions\":[{\"action_type\":\"PREDICT\",\"method\":\"POST\",\"url\":\"https://test.com\"," + + "\"headers\":{\"api_key\":\"${credential.key}\"}," + + "\"request_body\":\"{\\\"input\\\": \\\"${parameters.input}\\\"}\"," + + "\"pre_process_function\":\"connector.pre_process.openai.embedding\"," + + "\"post_process_function\":\"connector.post_process.openai.embedding\"}]," + + "\"backend_roles\":[\"role1\",\"role2\"],\"add_all_backend_roles\":false," + + "\"access_mode\":\"PUBLIC\"}"; + testParseFromJsonString(expectedInputStr, parsedInput -> { + assertEquals("test_connector_name", parsedInput.getName()); + assertEquals(1, parsedInput.getParameters().size()); + assertEquals("[\"test input value\"]", parsedInput.getParameters().get("input")); + }); + } + @Test public void testParseWithDryRun() throws Exception { String expectedInputStrWithDryRun = "{\"dry_run\":true}";