Skip to content

Commit

Permalink
Undo: throw an error when an unknown field is provided in CreateConve…
Browse files Browse the repository at this point in the history
…rsation or CreateInteraction.

Signed-off-by: rithin-pullela-aws <[email protected]>
  • Loading branch information
rithin-pullela-aws committed Dec 19, 2024
1 parent 41325c4 commit e577f61
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public static CreateConversationRequest fromRestRequest(RestRequest restRequest)
additionalInfo = (Map<String, String>) body.get(META_ADDITIONAL_INFO_FIELD);
break;
default:
throw new IllegalArgumentException("Invalid field [" + key + "] found in request body");
parser.skipChildren();
break;
}
}
if (body.get(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public static CreateInteractionRequest fromRestRequest(RestRequest request) thro
tracenum = parser.intValue(false);
break;
default:
throw new IllegalArgumentException("Invalid field [" + fieldName + "] found in request body");
parser.skipChildren();
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void initInteractionsIndexIfAbsent(ActionListener<Boolean> listener) {
* @param origin the origin of the response for this interaction
* @param additionalInfo additional information used for constructing the LLM prompt
* @param timestamp when this interaction happened
* @param parintid the parent interactionId of this interaction
* @param parentId the parent interactionId of this interaction
* @param traceNumber the trace number for a parent interaction
* @param listener gets the id of the newly created interaction record
*/
Expand All @@ -150,7 +150,7 @@ public void createInteraction(
Map<String, String> additionalInfo,
Instant timestamp,
ActionListener<String> listener,
String parintid,
String parentId,
Integer traceNumber
) {
initInteractionsIndexIfAbsent(ActionListener.wrap(indexExists -> {
Expand All @@ -165,7 +165,7 @@ public void createInteraction(
Map<String, Object> sourceMap = new HashMap<>();
sourceMap.put(ConversationalIndexConstants.INTERACTIONS_CONVERSATION_ID_FIELD, conversationId);
sourceMap.put(ConversationalIndexConstants.INTERACTIONS_CREATE_TIME_FIELD, timestamp);
sourceMap.put(ConversationalIndexConstants.PARENT_INTERACTIONS_ID_FIELD, parintid);
sourceMap.put(ConversationalIndexConstants.PARENT_INTERACTIONS_ID_FIELD, parentId);
sourceMap.put(ConversationalIndexConstants.INTERACTIONS_TRACE_NUMBER_FIELD, traceNumber);

if (input != null && !input.trim().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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 Down Expand Up @@ -134,21 +133,4 @@ public void testRestRequest_WithAdditionalInfo() throws IOException {
Assert.assertEquals(123, request.getAdditionalInfos().get("key2"));
}

public void testRestRequest_WithUnknownFields_Fails() throws IOException {
String name = "test-name";
RestRequest req = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withContent(
new BytesArray(gson.toJson(Map.of(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD, name, "unknown_field", "some value"))),
MediaTypeRegistry.JSON
)
.build();

OpenSearchParseException exception = assertThrows(
"Expected OpenSearchParseException due to unknown field",
OpenSearchParseException.class,
() -> CreateConversationRequest.fromRestRequest(req)
);

assertEquals(exception.getMessage(), "Invalid field [unknown_field] found in request body");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,37 +155,6 @@ public void testFromRestRequest_Trace() throws IOException {
assert (request.getTraceNumber().equals(1));
}

public void testRestRequest_WithUnknownFields_Fails() throws IOException {
Map<String, Object> params = Map
.of(
ActionConstants.INPUT_FIELD,
"input",
ActionConstants.PROMPT_TEMPLATE_FIELD,
"pt",
ActionConstants.AI_RESPONSE_FIELD,
"response",
ActionConstants.RESPONSE_ORIGIN_FIELD,
"origin",
ActionConstants.ADDITIONAL_INFO_FIELD,
Collections.singletonMap("metadata", "some meta"),
"unknown_field",
"some value"
);

RestRequest rrequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withParams(Map.of(ActionConstants.MEMORY_ID, "cid"))
.withContent(new BytesArray(gson.toJson(params)), MediaTypeRegistry.JSON)
.build();

IllegalArgumentException exception = assertThrows(
"Expected IllegalArgumentException due to unknown field",
IllegalArgumentException.class,
() -> CreateInteractionRequest.fromRestRequest(rrequest)
);

assertEquals(exception.getMessage(), "Invalid field [unknown_field] found in request body");
}

public void testFromRestRequest_WithAllFieldsEmpty_Fails() throws IOException {
Map<String, Object> params = new HashMap<>();

Expand Down

0 comments on commit e577f61

Please sign in to comment.