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

Unwrap single array parameter wrapped by JsonRPC 2.0 #714

Merged
merged 2 commits into from
Mar 22, 2023
Merged
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 @@ -246,6 +246,20 @@ protected Object parseParams(JsonReader in, String method) throws IOException, J
}
Type[] parameterTypes = getParameterTypes(method);
if (parameterTypes.length == 1) {
if (next == JsonToken.BEGIN_ARRAY) {
/* JsonRPC 2.0: §4.2 Parameter Structures.
Wrapping:
"some-value" -> ["some-value"]
["some-array"] -> [["some-array"]]
By-pass:
{type: "some_obj"} -> {type: "some_obj"}
*/
// Unwrap by removing the the outermost array.
in.beginArray();
var singleParameter = fromJson(in, parameterTypes[0]);
in.endArray();
return singleParameter;
}
return fromJson(in, parameterTypes[0]);
}
if (parameterTypes.length > 1 && next == JsonToken.BEGIN_ARRAY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
******************************************************************************/
package org.eclipse.lsp4j.jsonrpc.test.json;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -780,4 +783,66 @@ public void testNotification_AllOrders() {
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)params).uri);
});
}


@Test
public void testParamUnwrap_JsonRpc2_0() {
MessageJsonHandler handler = createSimpleRequestHandler(String.class, Boolean.class);

var request = new RequestMessage();
request.setId(1);
request.setMethod(handler.getMethodProvider().resolveMethod(null));
request.setParams(new boolean[] { true }); // fake wrapped primitive [true] for JsonRpc 2.0

RequestMessage message = (RequestMessage) handler.parseMessage(request.toString());

// Check parse - unwrap primitive
assertEquals(true, message.getParams());

}

@Test
public void testArrayParamUnwrap_JsonRpc2_0() {
MessageJsonHandler handler = createSimpleRequestHandler(String.class, new TypeToken<List<Boolean>>() {
}.getType());

var request = "{\n"
+ " \"jsonrpc\": \"2.0\",\n"
+ " \"id\": 1,\n"
+ " \"method\": \"testMethod\",\n"
+ " \"params\": [\n"
+ " [\n"
+ " true\n"
+ " ]\n"
+ " ]\n"
+ "}";
RequestMessage message = (RequestMessage) handler.parseMessage(request);

// Check parse - unwrap array
assertEquals(List.of(true), message.getParams());

}

@Test
public void testMultiParamUnwrap_JsonRpc2_0() {
MessageJsonHandler handler = createSimpleRequestHandler(String.class, Boolean.class, String.class);

var request = new RequestMessage();
request.setId(1);
request.setMethod(handler.getMethodProvider().resolveMethod(null));
request.setParams(List.of(true, "string")); // fake wrapped array [true, string] for JsonRpc 2.0

RequestMessage message = (RequestMessage) handler.parseMessage(request.toString());

// Check parse - unwrap array
assertEquals(request.getParams(), message.getParams());

}

private static MessageJsonHandler createSimpleRequestHandler(Class<?> returnType, Type... paramType) {
JsonRpcMethod requestMethod = JsonRpcMethod.request("testMethod", returnType, paramType);
MessageJsonHandler handler = new MessageJsonHandler(Map.of(requestMethod.getMethodName(), requestMethod));
handler.setMethodProvider((id) -> requestMethod.getMethodName());
return handler;
}
}