diff --git a/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java b/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java index 902046983..30503d7ed 100644 --- a/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java +++ b/src/test/java/org/opensearch/knn/index/KNNMethodContextTests.java @@ -239,6 +239,21 @@ public void testParse_invalid() throws IOException { expectThrows(MapperParsingException.class, () -> MethodComponentContext.parse(in7)); } + /** + * Test context method parsing when parameters are set to null + */ + public void testParse_nullParameters() throws IOException { + String methodName = "test-method"; + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .startObject() + .field(NAME, methodName) + .field(PARAMETERS, (String) null) + .endObject(); + Map in = xContentBuilderToMap(xContentBuilder); + KNNMethodContext knnMethodContext = KNNMethodContext.parse(in); + assertTrue(knnMethodContext.getMethodComponent().getParameters().isEmpty()); + } + /** * Test context method parsing when input is valid */ diff --git a/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java b/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java index cf65dd285..14b8bf4d0 100644 --- a/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java +++ b/src/test/java/org/opensearch/knn/index/MethodComponentContextTests.java @@ -107,6 +107,21 @@ public void testGetParameters() throws IOException { assertEquals(paramVal2, methodContext.getParameters().get(paramKey2)); } + /** + * Test method component context parsing when parameters are set to null + */ + public void testParse_nullParameters() throws IOException { + String name = "test-name"; + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .startObject() + .field(NAME, name) + .field(PARAMETERS, (String) null) + .endObject(); + Map in = xContentBuilderToMap(xContentBuilder); + MethodComponentContext methodComponentContext = MethodComponentContext.parse(in); + assertTrue(methodComponentContext.getParameters().isEmpty()); + } + /** * Test parse where input is valid */