From 77ffa9b4c60dcd7f31d38ee73de38257c24c6e51 Mon Sep 17 00:00:00 2001 From: John Mazanec Date: Tue, 5 Apr 2022 10:26:48 -0700 Subject: [PATCH] Parse null parameters value in method parsers Allow user to input null value for parameters field for KNNMethodContext and MethodComponentContext. Signed-off-by: John Mazanec --- .../org/opensearch/knn/index/KNNMethodContext.java | 6 +++++- .../opensearch/knn/index/MethodComponentContext.java | 10 +++++++--- .../opensearch/knn/index/KNNMethodContextTests.java | 8 ++++---- .../knn/index/MethodComponentContextTests.java | 8 ++++---- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/opensearch/knn/index/KNNMethodContext.java b/src/main/java/org/opensearch/knn/index/KNNMethodContext.java index 4667640bf..0f4281e21 100644 --- a/src/main/java/org/opensearch/knn/index/KNNMethodContext.java +++ b/src/main/java/org/opensearch/knn/index/KNNMethodContext.java @@ -42,7 +42,7 @@ */ public class KNNMethodContext implements ToXContentFragment, Writeable { - private static Logger logger = LogManager.getLogger(KNNMethodContext.class); + private static final Logger logger = LogManager.getLogger(KNNMethodContext.class); private static KNNMethodContext defaultInstance = null; @@ -194,6 +194,10 @@ public static KNNMethodContext parse(Object in) { name = (String) value; } else if (PARAMETERS.equals(key)) { + if (value == null) { + continue; + } + if (!(value instanceof Map)) { throw new MapperParsingException("Unable to parse parameters for main method component"); } diff --git a/src/main/java/org/opensearch/knn/index/MethodComponentContext.java b/src/main/java/org/opensearch/knn/index/MethodComponentContext.java index a8c93fe4c..73ffcae6e 100644 --- a/src/main/java/org/opensearch/knn/index/MethodComponentContext.java +++ b/src/main/java/org/opensearch/knn/index/MethodComponentContext.java @@ -37,10 +37,10 @@ */ public class MethodComponentContext implements ToXContentFragment, Writeable { - private static Logger logger = LogManager.getLogger(MethodComponentContext.class); + private static final Logger logger = LogManager.getLogger(MethodComponentContext.class); - private String name; - private Map parameters; + private final String name; + private final Map parameters; /** * Constructor @@ -93,6 +93,10 @@ public static MethodComponentContext parse(Object in) { } name = (String) value; } else if (PARAMETERS.equals(key)) { + if (value == null) { + continue; + } + if (!(value instanceof Map)) { throw new MapperParsingException("Unable to parse parameters for method component"); } diff --git a/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java b/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java index 30503d7ed..176029ad6 100644 --- a/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java +++ b/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java @@ -245,10 +245,10 @@ public void testParse_invalid() throws IOException { public void testParse_nullParameters() throws IOException { String methodName = "test-method"; XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() - .startObject() - .field(NAME, methodName) - .field(PARAMETERS, (String) null) - .endObject(); + .startObject() + .field(NAME, methodName) + .field(PARAMETERS, (String) null) + .endObject(); Map in = xContentBuilderToMap(xContentBuilder); KNNMethodContext knnMethodContext = KNNMethodContext.parse(in); assertTrue(knnMethodContext.getMethodComponent().getParameters().isEmpty()); diff --git a/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java b/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java index 14b8bf4d0..826bf3c83 100644 --- a/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java +++ b/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java @@ -113,10 +113,10 @@ public void testGetParameters() throws IOException { public void testParse_nullParameters() throws IOException { String name = "test-name"; XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() - .startObject() - .field(NAME, name) - .field(PARAMETERS, (String) null) - .endObject(); + .startObject() + .field(NAME, name) + .field(PARAMETERS, (String) null) + .endObject(); Map in = xContentBuilderToMap(xContentBuilder); MethodComponentContext methodComponentContext = MethodComponentContext.parse(in); assertTrue(methodComponentContext.getParameters().isEmpty());