Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Void return types need to always be null value #728

Closed
wants to merge 1 commit into from
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 @@ -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