This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new a2fea1bee4 fix(grpc): support empty request parameters (#7206)
a2fea1bee4 is described below
commit a2fea1bee4b3a6c8fb1e127974355d6ac67f1004
Author: Liming Deng <[email protected]>
AuthorDate: Thu Sep 24 16:36:58 2026 +0800
fix(grpc): support empty request parameters (#7206)
Co-authored-by: aias00 <[email protected]>
---
.../plugin/grpc/client/ShenyuGrpcClient.java | 10 +++-
.../plugin/grpc/client/ShenyuGrpcClientTest.java | 64 ++++++++++++++++++++++
2 files changed, 73 insertions(+), 1 deletion(-)
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/ShenyuGrpcClient.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/ShenyuGrpcClient.java
index 96d8409d4b..15bd89763b 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/ShenyuGrpcClient.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/ShenyuGrpcClient.java
@@ -24,6 +24,8 @@ import io.grpc.ClientCall;
import io.grpc.ManagedChannel;
import io.grpc.MethodDescriptor;
import io.grpc.stub.StreamObserver;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.plugin.grpc.exception.ShenyuGrpcException;
@@ -37,6 +39,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
+import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@@ -72,7 +75,12 @@ public class ShenyuGrpcClient implements Closeable {
final CallOptions
callOptions,
final String
requestJsons,
final
MethodDescriptor.MethodType methodType) {
- List<DynamicMessage> jsonRequestList =
JsonMessage.buildJsonMessageList(GsonUtils.getInstance().toObjectMap(requestJsons));
+ List<DynamicMessage> jsonRequestList =
StringUtils.isBlank(requestJsons)
+ ? Collections.singletonList(JsonMessage.buildJsonMessage())
+ :
JsonMessage.buildJsonMessageList(GsonUtils.getInstance().toObjectMap(requestJsons));
+ if (CollectionUtils.isEmpty(jsonRequestList)) {
+ jsonRequestList =
Collections.singletonList(JsonMessage.buildJsonMessage());
+ }
DynamicMessage jsonResponse = JsonMessage.buildJsonMessage();
MethodDescriptor<DynamicMessage, DynamicMessage>
jsonMarshallerMethodDescriptor =
JsonMessage.createJsonMarshallerMethodDescriptor(metaData.getServiceName(),
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/test/java/org/apache/shenyu/plugin/grpc/client/ShenyuGrpcClientTest.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/test/java/org/apache/shenyu/plugin/grpc/client/ShenyuGrpcClientTest.java
new file mode 100644
index 0000000000..3ece0d3d36
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-grpc/src/test/java/org/apache/shenyu/plugin/grpc/client/ShenyuGrpcClientTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.grpc.client;
+
+import com.google.common.util.concurrent.Futures;
+import io.grpc.CallOptions;
+import io.grpc.ManagedChannel;
+import io.grpc.MethodDescriptor;
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.plugin.grpc.proto.ShenyuGrpcCallRequest;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Test cases for {@link ShenyuGrpcClient}.
+ */
+public final class ShenyuGrpcClientTest {
+
+ @Test
+ public void testCallWithNullRequestCreatesDefaultMessage() {
+ assertDefaultRequest(null);
+ }
+
+ @Test
+ public void testCallWithEmptyRequestListCreatesDefaultMessage() {
+ assertDefaultRequest("{\"data\":[]}");
+ }
+
+ private void assertDefaultRequest(final String requestJsons) {
+ ShenyuGrpcClient client = spy(new
ShenyuGrpcClient(mock(ManagedChannel.class)));
+
doReturn(Futures.immediateVoidFuture()).when(client).invoke(any(ShenyuGrpcCallRequest.class));
+ MetaData metaData = new MetaData();
+ metaData.setServiceName("service");
+ metaData.setMethodName("method");
+
+ client.call(metaData, CallOptions.DEFAULT, requestJsons,
MethodDescriptor.MethodType.UNARY).join();
+
+ ArgumentCaptor<ShenyuGrpcCallRequest> requestCaptor =
ArgumentCaptor.forClass(ShenyuGrpcCallRequest.class);
+ verify(client).invoke(requestCaptor.capture());
+ assertEquals(1, requestCaptor.getValue().getRequests().size());
+ }
+}