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

Using internals which may not be visible #738 #739

Merged
merged 1 commit into from
Aug 3, 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 @@ -43,7 +43,6 @@
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.Primitives;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
Expand Down Expand Up @@ -456,8 +455,8 @@ public void write(JsonWriter out, Message message) throws IOException {
protected void handleParameter(JsonWriter out, Object params, String method) {
boolean isSingleArray = (getParameterTypes(method).length == 1 && Collection.class.isInstance(params)
|| params.getClass().isArray());
boolean needsWrap = isSingleArray || params instanceof String || Primitives.isPrimitive(getClass())
|| Primitives.isWrapperType(params.getClass());
boolean needsWrap = isSingleArray || params instanceof String || isWrapperType(params.getClass())
|| isPrimitive(getClass());
if (needsWrap) {
gson.toJson(List.of(params), List.class, out);
} else {
Expand All @@ -484,4 +483,22 @@ protected void writeNullValue(JsonWriter out) throws IOException {
out.setSerializeNulls(previousSerializeNulls);
}

/**
* Returns true if this type is a primitive.
*/
private static boolean isPrimitive(Type type) {
return type instanceof Class<?> && ((Class<?>) type).isPrimitive();
}

/**
* Returns {@code true} if {@code type} is one of the nine primitive-wrapper
* types, such as {@link Integer}.
*
* @see Class#isPrimitive
*/
private static boolean isWrapperType(Type type) {
return type == Integer.class || type == Float.class || type == Byte.class || type == Double.class
|| type == Long.class || type == Character.class || type == Boolean.class || type == Short.class
|| type == Void.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,34 @@ public void testNotification_AllOrders() {

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

var request = new RequestMessage();
request.setId(1);
request.setMethod(handler.getMethodProvider().resolveMethod(null));
request.setParams(3);

// check primitive was wrapped into array
assertEquals("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"testMethod\",\"params\":[3]}", handler.serialize(request));
}

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

var request = new RequestMessage();
request.setId(1);
request.setMethod(handler.getMethodProvider().resolveMethod(null));
request.setParams('X');

// check primitive was wrapped into array
assertEquals("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"testMethod\",\"params\":[\"X\"]}", handler.serialize(request));
}

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

var request = new RequestMessage();
request.setId(1);
request.setMethod(handler.getMethodProvider().resolveMethod(null));
Expand All @@ -798,6 +824,7 @@ public void testWrapPrimitive_JsonRpc2_0() {
assertEquals("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"testMethod\",\"params\":[\"param\"]}", handler.serialize(request));
}


@Test
public void testUnwrapPrimitive_JsonRpc2_0() {
MessageJsonHandler handler = createSimpleRequestHandler(String.class, String.class);
Expand Down