Skip to content

Commit

Permalink
Void return types need to always be null value
Browse files Browse the repository at this point in the history
Void type in Java cannot be instantiated, therefore if a
type is declared as Void the fromJson should always set that
value to null.

If you want to have a generic return type that allows non-null
and null results, use a type other than Void as the return type.

Fixes #721
  • Loading branch information
jonahgraham committed May 9, 2023
1 parent 3d55fa0 commit 79df5e2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public static interface MyServer {
CompletableFuture<MyParam> askServer(MyParam param);
}


public static interface MyVoidServer {
@JsonRequest
CompletableFuture<Void> askServer(MyParam param);
}

public static interface MyClient {
@JsonRequest
CompletableFuture<MyParam> askClient(MyParam param);
Expand Down Expand Up @@ -99,6 +105,45 @@ public CompletableFuture<MyParam> askServer(MyParam param) {
Assert.assertEquals("BAR", barFuture.get(TIMEOUT, TimeUnit.MILLISECONDS).value);
}

@Test
public void testVoidResponse() throws Exception {
// create client side
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in2 = new PipedInputStream();
PipedOutputStream out2 = new PipedOutputStream();

in.connect(out2);
out.connect(in2);

MyClient client = new MyClient() {
@Override
public CompletableFuture<MyParam> askClient(MyParam param) {
throw new UnsupportedOperationException("Unused by this test");
}
};
Launcher<MyVoidServer> clientSideLauncher = DebugLauncher.createLauncher(client, MyVoidServer.class, in, out);

// create server side
MyServer server = new MyServer() {
@Override
public CompletableFuture<MyParam> askServer(MyParam param) {
return CompletableFuture.completedFuture(param);
}
};
Launcher<MyClient> serverSideLauncher = DebugLauncher.createLauncher(server, MyClient.class, in2, out2);

clientSideLauncher.startListening();
serverSideLauncher.startListening();

// We call a method that is declared as returning Void, but the other end returns a non-null value
// make sure that the json parsing discards that result
CompletableFuture<Void> fooFuture = clientSideLauncher.getRemoteProxy().askServer(new MyParam("FOO"));

Void void1 = fooFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNull(void1);
}

@Test
public void testCancellation() throws Exception {
// create client side
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ public void testParseSyntaxErrorRequest() {
}
}

private Object voidResponse(String body) {
private void voidResponse(String body) {
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Void>() {
}.getType(), new TypeToken<Void>() {
Expand All @@ -870,56 +870,42 @@ private Object voidResponse(String body) {
+ "\"success\":true"//
+ bodyField //
+ "}\n");
return ((ResponseMessage) message).getResult();
Object result = ((ResponseMessage) message).getResult();
Assert.assertNull(result);
}

@Test
public void testVoidResponse_noBody() {
Assert.assertNull(voidResponse(null));
voidResponse(null);
}

@Test
public void testVoidResponse_null() {
Assert.assertNull(voidResponse("null"));

voidResponse("null");
}

@Test
public void testVoidResponse_primitive() {
Object result = voidResponse("true");
JsonPrimitive jsonPrimitive = (JsonPrimitive) result;
Assert.assertTrue(jsonPrimitive.getAsBoolean());
voidResponse("true");
}

@Test
public void testVoidResponse_emptyArray() {
Object result = voidResponse("[]");
JsonArray jsonArray = (JsonArray) result;
Assert.assertTrue(jsonArray.isEmpty());
voidResponse("[]");
}

@Test
public void testVoidResponse_array() {
Object result = voidResponse("[1,2,3]");
JsonArray jsonArray = (JsonArray) result;
Assert.assertEquals(1, jsonArray.get(0).getAsInt());
Assert.assertEquals(2, jsonArray.get(1).getAsInt());
Assert.assertEquals(3, jsonArray.get(2).getAsInt());
voidResponse("[1,2,3]");
}

@Test
public void testVoidResponse_emptyObject() {
Object result = voidResponse("{}");
Assert.assertTrue(result instanceof JsonObject);
JsonObject jsonObject = (JsonObject) result;
Assert.assertTrue(jsonObject.entrySet().isEmpty());
voidResponse("{}");
}

@Test
public void testVoidResponse_object() {
Object result = voidResponse("{\"allThreadsContinued\":false}");
Assert.assertTrue(result instanceof JsonObject);
JsonObject jsonObject = (JsonObject) result;
Assert.assertFalse(jsonObject.getAsJsonPrimitive("allThreadsContinued").getAsBoolean());
voidResponse("{\"allThreadsContinued\":false}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ protected Object fromJson(JsonElement element, Type type) {
return null;
}
if (isNullOrVoidType(type)) {
return element;
return null;
}
Object value = gson.fromJson(element, type);
if (isNull(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.jsonrpc.RemoteEndpoint;
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;

import org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint;
Expand Down Expand Up @@ -88,6 +89,11 @@ public static interface MyServer {
@JsonRequest
CompletableFuture<MyParam> askServer(MyParam param);
}

public static interface MyVoidServer {
@JsonRequest
CompletableFuture<Void> askServer(MyParam param);
}

public static class MyServerImpl implements MyServer {
@Override
Expand Down Expand Up @@ -224,6 +230,45 @@ public void testEitherNull() throws Exception {
out.toString());
}

@Test
public void testVoidResponse() throws Exception {
// create client side
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in2 = new PipedInputStream();
PipedOutputStream out2 = new PipedOutputStream();

in.connect(out2);
out.connect(in2);

MyClient client = new MyClient() {
@Override
public CompletableFuture<MyParam> askClient(MyParam param) {
throw new UnsupportedOperationException("Unused by this test");
}
};
Launcher<MyVoidServer> clientSideLauncher = Launcher.createLauncher(client, MyVoidServer.class, in, out);

// create server side
MyServer server = new MyServer() {
@Override
public CompletableFuture<MyParam> askServer(MyParam param) {
return CompletableFuture.completedFuture(param);
}
};
Launcher<MyClient> serverSideLauncher = Launcher.createLauncher(server, MyClient.class, in2, out2);

clientSideLauncher.startListening();
serverSideLauncher.startListening();

// We call a method that is declared as returning Void, but the other end returns a non-null value
// make sure that the json parsing discards that result
CompletableFuture<Void> fooFuture = clientSideLauncher.getRemoteProxy().askServer(new MyParam("FOO"));

Void void1 = fooFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNull(void1);
}


@Test
public void testCancellation() throws Exception {
Expand Down

0 comments on commit 79df5e2

Please sign in to comment.