Skip to content

Commit

Permalink
adds inputs validation for create memory
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Zhang <[email protected]>
  • Loading branch information
Zhangxunmt committed Feb 6, 2024
1 parent bf02c3a commit 24e30fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.util.Map;

import org.opensearch.OpenSearchParseException;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;
Expand Down Expand Up @@ -100,15 +101,18 @@ public static CreateConversationRequest fromRestRequest(RestRequest restRequest)
if (!restRequest.hasContent()) {
return new CreateConversationRequest();
}
Map<String, String> body = restRequest.contentParser().mapStrings();
if (body.containsKey(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD)) {
return new CreateConversationRequest(
body.get(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD),
body.get(APPLICATION_TYPE_FIELD)
);
} else {
return new CreateConversationRequest();
try {
Map<String, String> body = restRequest.contentParser().mapStrings();
if (body.containsKey(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD)) {
return new CreateConversationRequest(
body.get(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD),
body.get(APPLICATION_TYPE_FIELD)
);
} else {
return new CreateConversationRequest();

Check warning on line 112 in memory/src/main/java/org/opensearch/ml/memory/action/conversation/CreateConversationRequest.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L112 was not covered by tests
}
} catch (Exception exception) {
throw new OpenSearchParseException(exception.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.opensearch.OpenSearchParseException;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
Expand All @@ -41,6 +44,8 @@

public class CreateConversationRequestTests extends OpenSearchTestCase {

@Rule
public ExpectedException exceptionRule = ExpectedException.none();
Gson gson;

@Before
Expand Down Expand Up @@ -100,4 +105,13 @@ public void testNamedRestRequest_WithAppType() throws IOException {
assert (request.getName().equals(name));
assert (request.getApplicationType().equals(appType));
}

public void testRestRequest_NullName() throws IOException {
exceptionRule.expect(OpenSearchParseException.class);
exceptionRule.expectMessage("Can't get text on a VALUE_NULL");
RestRequest req = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withContent(new BytesArray("{\"name\":null}"), MediaTypeRegistry.JSON)
.build();
CreateConversationRequest.fromRestRequest(req);
}
}

0 comments on commit 24e30fb

Please sign in to comment.