Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -198,6 +199,15 @@ public List<Task> getRelatedTasks() {
return params != null ? params.configuration() : null;
}

/**
* Returns the request metadata.
*
* @return the metadata, or null if not available
*/
public @Nullable Map<String, Object> getMetadata() {
return (params != null && params.metadata() != null) ? Collections.unmodifiableMap(params.metadata()) : null;
}
Comment on lines 207 to 209
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To maintain the immutability of the RequestContext and ensure consistency with other methods like getRelatedTasks(), it is best practice to return an unmodifiable view of the metadata map. This prevents external callers from modifying the internal state of the request parameters.

Suggested change
public @Nullable Map<String, Object> getMetadata() {
return params != null ? params.metadata() : null;
}
public @Nullable Map<String, Object> getMetadata() {
return (params != null && params.metadata() != null) ? Collections.unmodifiableMap(params.metadata()) : null;
}


/**
* Returns the server call context.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.mockito.Mockito.mockStatic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

Expand All @@ -32,6 +33,7 @@ public void testInitWithoutParams() {
assertNull(context.getTaskId());
assertNull(context.getContextId());
assertNull(context.getTask());
assertNull(context.getMetadata());
assertTrue(context.getRelatedTasks().isEmpty());
}

Expand Down Expand Up @@ -59,6 +61,22 @@ public void testInitWithParamsNoIds() {
}
}

@Test
public void testInitWithParamsMetadata() {
var message = Message.builder().role(Message.Role.USER).parts(List.of(new TextPart(""))).build();
var metadata = new HashMap<String, Object>();
metadata.put("key", "value");

var params = MessageSendParams.builder()
.message(message)
.metadata(metadata)
.build();

RequestContext context = new RequestContext(params, null, null, null, null, null);

assertEquals(metadata, context.getMetadata());
}

@Test
public void testInitWithTaskId() {
String taskId = "task-123";
Expand Down
Loading