Fix output format of jsonrpc receive notification

Fixes #1313
This commit is contained in:
AsamK 2023-08-26 17:17:25 +02:00
parent bc3bdbbf21
commit 87e79bceaa

View file

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ContainerNode;
import com.fasterxml.jackson.databind.node.IntNode; import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
@ -54,8 +55,8 @@ public class SignalJsonRpcDispatcherHandler {
this.commandHandler = new SignalJsonRpcCommandHandler(c, this::getCommand); this.commandHandler = new SignalJsonRpcCommandHandler(c, this::getCommand);
if (!noReceiveOnStart) { if (!noReceiveOnStart) {
this.subscribeReceive(c.getManagers()); this.subscribeReceive(c.getManagers(), true);
c.addOnManagerAddedHandler(this::subscribeReceive); c.addOnManagerAddedHandler(m -> subscribeReceive(m, true));
c.addOnManagerRemovedHandler(this::unsubscribeReceive); c.addOnManagerRemovedHandler(this::unsubscribeReceive);
} }
@ -66,7 +67,7 @@ public class SignalJsonRpcDispatcherHandler {
this.commandHandler = new SignalJsonRpcCommandHandler(m, this::getCommand); this.commandHandler = new SignalJsonRpcCommandHandler(m, this::getCommand);
if (!noReceiveOnStart) { if (!noReceiveOnStart) {
subscribeReceive(m); subscribeReceive(m, true);
} }
final var currentThread = Thread.currentThread(); final var currentThread = Thread.currentThread();
@ -77,17 +78,23 @@ public class SignalJsonRpcDispatcherHandler {
private static final AtomicInteger nextSubscriptionId = new AtomicInteger(0); private static final AtomicInteger nextSubscriptionId = new AtomicInteger(0);
private int subscribeReceive(final Manager manager) { private int subscribeReceive(final Manager manager, boolean internalSubscription) {
return subscribeReceive(List.of(manager)); return subscribeReceive(List.of(manager), internalSubscription);
} }
private int subscribeReceive(final List<Manager> managers) { private int subscribeReceive(final List<Manager> managers, boolean internalSubscription) {
final var subscriptionId = nextSubscriptionId.getAndIncrement(); final var subscriptionId = nextSubscriptionId.getAndIncrement();
final var handlers = managers.stream().map(m -> { final var handlers = managers.stream().map(m -> {
final var receiveMessageHandler = new JsonReceiveMessageHandler(m, s -> { final var receiveMessageHandler = new JsonReceiveMessageHandler(m, s -> {
final var params = new ObjectNode(objectMapper.getNodeFactory()); ContainerNode<?> params;
params.set("subscription", IntNode.valueOf(subscriptionId)); if (internalSubscription) {
params.set("result", objectMapper.valueToTree(s)); params = objectMapper.valueToTree(s);
} else {
final var paramsNode = new ObjectNode(objectMapper.getNodeFactory());
paramsNode.set("subscription", IntNode.valueOf(subscriptionId));
paramsNode.set("result", objectMapper.valueToTree(s));
params = paramsNode;
}
final var jsonRpcRequest = JsonRpcRequest.forNotification("receive", params, null); final var jsonRpcRequest = JsonRpcRequest.forNotification("receive", params, null);
try { try {
jsonRpcSender.sendRequest(jsonRpcRequest); jsonRpcSender.sendRequest(jsonRpcRequest);
@ -162,7 +169,7 @@ public class SignalJsonRpcDispatcherHandler {
public void handleCommand( public void handleCommand(
final Void request, final Manager m, final JsonWriter jsonWriter final Void request, final Manager m, final JsonWriter jsonWriter
) throws CommandException { ) throws CommandException {
final var subscriptionId = subscribeReceive(m); final var subscriptionId = subscribeReceive(m, false);
jsonWriter.write(subscriptionId); jsonWriter.write(subscriptionId);
} }
@ -170,7 +177,7 @@ public class SignalJsonRpcDispatcherHandler {
public void handleCommand( public void handleCommand(
final Void request, final MultiAccountManager c, final JsonWriter jsonWriter final Void request, final MultiAccountManager c, final JsonWriter jsonWriter
) throws CommandException { ) throws CommandException {
final var subscriptionId = subscribeReceive(c.getManagers()); final var subscriptionId = subscribeReceive(c.getManagers(), false);
jsonWriter.write(subscriptionId); jsonWriter.write(subscriptionId);
} }
} }