Skip to content

Commit 1d0f95b

Browse files
committed
adds inputs validation for create memory
Signed-off-by: Xun Zhang <xunzh@amazon.com>
1 parent bf02c3a commit 1d0f95b

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

memory/src/main/java/org/opensearch/ml/memory/action/conversation/CreateConversationRequest.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.util.Map;
2424

25+
import org.opensearch.OpenSearchParseException;
2526
import org.opensearch.action.ActionRequest;
2627
import org.opensearch.action.ActionRequestValidationException;
2728
import org.opensearch.core.common.io.stream.StreamInput;
@@ -100,15 +101,20 @@ public static CreateConversationRequest fromRestRequest(RestRequest restRequest)
100101
if (!restRequest.hasContent()) {
101102
return new CreateConversationRequest();
102103
}
103-
Map<String, String> body = restRequest.contentParser().mapStrings();
104-
if (body.containsKey(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD)) {
105-
return new CreateConversationRequest(
106-
body.get(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD),
107-
body.get(APPLICATION_TYPE_FIELD)
108-
);
109-
} else {
110-
return new CreateConversationRequest();
104+
try {
105+
Map<String, String> body = restRequest.contentParser().mapStrings();
106+
if (body.containsKey(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD)) {
107+
return new CreateConversationRequest(
108+
body.get(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD),
109+
body.get(APPLICATION_TYPE_FIELD)
110+
);
111+
} else {
112+
return new CreateConversationRequest();
113+
}
114+
} catch (IllegalStateException illegalStateException) {
115+
throw new OpenSearchParseException(illegalStateException.getMessage());
116+
} catch (Exception exception) {
117+
throw new OpenSearchParseException(exception.getMessage());
111118
}
112119
}
113-
114120
}

memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateConversationRequestTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.util.Map;
2424

2525
import org.junit.Before;
26+
import org.junit.Rule;
27+
import org.junit.rules.ExpectedException;
28+
import org.opensearch.OpenSearchParseException;
2629
import org.opensearch.common.io.stream.BytesStreamOutput;
2730
import org.opensearch.core.common.bytes.BytesArray;
2831
import org.opensearch.core.common.bytes.BytesReference;
@@ -41,6 +44,8 @@
4144

4245
public class CreateConversationRequestTests extends OpenSearchTestCase {
4346

47+
@Rule
48+
public ExpectedException exceptionRule = ExpectedException.none();
4449
Gson gson;
4550

4651
@Before
@@ -100,4 +105,13 @@ public void testNamedRestRequest_WithAppType() throws IOException {
100105
assert (request.getName().equals(name));
101106
assert (request.getApplicationType().equals(appType));
102107
}
108+
109+
public void testRestRequest_NullName() throws IOException {
110+
exceptionRule.expect(OpenSearchParseException.class);
111+
exceptionRule.expectMessage("Can't get text on a VALUE_NULL");
112+
RestRequest req = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
113+
.withContent(new BytesArray("{\"name\":null}"), MediaTypeRegistry.JSON)
114+
.build();
115+
CreateConversationRequest.fromRestRequest(req);
116+
}
103117
}

0 commit comments

Comments
 (0)