Accept a list with one entry for single value JSON-RPC params (#2169)

JSON-RPC params that take a single value were read with
Namespace.getString, which turns a list into its toString form. A
recipient passed as ["<ACI>"], like for the send commands, became
"[<ACI>]", was no longer recognized as an ACI, and its digits were
used as a phone number.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ryan SandClaude Opus 5.5 authored and GitHub committed 2026-11-13 18:35:44 +01:00
1 parent e75ed24b29
commit b1389cca4f
2 files changed
+64 -1

No files matched your search

@@ -8,7 +8,8 @@ import java.util.List;
import java.util.Map;
/**
* Namespace implementation, that has plural handling for list arguments and converts camel case keys to dashed strings
* Namespace implementation, that has plural handling for list arguments and converts camel case keys to dashed strings.
* A list with a single entry is also accepted for a single value argument.
*/
final class JsonRpcNamespace extends Namespace {
@@ -27,6 +28,16 @@ final class JsonRpcNamespace extends Namespace {
return super.get(camelCaseString);
}
@Override
public String getString(final String dest) {
final Object value = get(dest);
if (value instanceof List<?> list && list.size() == 1) {
final var entry = list.getFirst();
return entry == null ? null : entry.toString();
}
return value == null ? null : value.toString();
}
@Override
public <E> List<E> getList(final String dest) {
try {
@@ -0,0 +1,52 @@
package org.asamk.signal.commands;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.testutil.ManagerMock;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SendReceiptCommandTest {
private static final String ACI = "84edbab3-8d1f-48f1-8bae-debb7bde3c6e";
@Test
void jsonRpcAcceptsRecipientAsSingleValue() throws Exception {
assertEquals(new RecipientIdentifier.Uuid(UUID.fromString(ACI)), sendReceipt(ACI));
}
@Test
void jsonRpcAcceptsRecipientAsListWithOneEntry() throws Exception {
// Like the recipient of the other send commands
assertEquals(new RecipientIdentifier.Uuid(UUID.fromString(ACI)), sendReceipt(List.of(ACI)));
assertEquals(new RecipientIdentifier.Number("+4915187654321"), sendReceipt(List.of("+4915187654321")));
}
private RecipientIdentifier.Single sendReceipt(final Object recipient) throws Exception {
final var captured = new AtomicReference<RecipientIdentifier.Single>();
final var delegate = ManagerMock.create("+4915112345678");
final var manager = (Manager) Proxy.newProxyInstance(Manager.class.getClassLoader(),
new Class<?>[]{Manager.class},
(proxy, method, args) -> {
if (method.getName().equals("sendReadReceipt")) {
captured.set((RecipientIdentifier.Single) args[0]);
return new SendMessageResults(1, Map.of());
}
return method.invoke(delegate, args);
});
new SendReceiptCommand().handleCommand(Map.of("recipient", recipient, "targetTimestamp", 1700000000000L),
manager,
(JsonWriter) ignored -> {
});
return captured.get();
}
}