diff --git a/sdk/src/main/java/io/dapr/client/AbstractDaprClient.java b/sdk/src/main/java/io/dapr/client/AbstractDaprClient.java index 61a7ad5c1..628a4c5ab 100644 --- a/sdk/src/main/java/io/dapr/client/AbstractDaprClient.java +++ b/sdk/src/main/java/io/dapr/client/AbstractDaprClient.java @@ -270,6 +270,14 @@ public Mono invokeBinding( return this.invokeBinding(bindingName, operation, data, metadata, TypeRef.get(clazz)); } + /** + * {@inheritDoc} + */ + @Override + public Mono invokeBinding(InvokeBindingRequest request) { + return this.invokeBinding(request, TypeRef.VOID); + } + /** * {@inheritDoc} */ diff --git a/sdk/src/main/java/io/dapr/client/DaprClient.java b/sdk/src/main/java/io/dapr/client/DaprClient.java index 5a3fa0a79..f51a6d0ff 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClient.java +++ b/sdk/src/main/java/io/dapr/client/DaprClient.java @@ -314,6 +314,14 @@ Mono invokeBinding(String bindingName, String operation, Object data, Map Mono invokeBinding(String bindingName, String operation, Object data, Map metadata, Class clazz); + /** + * Invokes a Binding operation. + * + * @param request The binding invocation request. + * @return a Mono with void. + */ + Mono invokeBinding(InvokeBindingRequest request); + /** * Invokes a Binding operation. * diff --git a/sdk/src/main/java/io/dapr/client/DaprClientImpl.java b/sdk/src/main/java/io/dapr/client/DaprClientImpl.java index bea0bc217..1f39d7aad 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientImpl.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientImpl.java @@ -459,8 +459,10 @@ private Subscription buildSubscription( try { CloudEvent cloudEvent = new CloudEvent<>(); - var object = - DaprClientImpl.this.objectSerializer.deserialize(message.getData().toByteArray(), type); + T object = null; + if (type != null) { + object = DaprClientImpl.this.objectSerializer.deserialize(message.getData().toByteArray(), type); + } cloudEvent.setData(object); cloudEvent.setDatacontenttype(message.getDataContentType()); cloudEvent.setId(message.getId()); @@ -528,6 +530,10 @@ public Mono invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef private Mono getMonoForHttpResponse(TypeRef type, DaprHttp.Response r) { try { + if (type == null) { + return Mono.empty(); + } + T object = objectSerializer.deserialize(r.getBody(), type); if (object == null) { return Mono.empty(); @@ -585,6 +591,9 @@ public Mono invokeBinding(InvokeBindingRequest request, TypeRef type) } try { + if (type == null) { + return Mono.empty(); + } return Mono.justOrEmpty(objectSerializer.deserialize(it.getData().toByteArray(), type)); } catch (IOException e) { throw DaprException.propagate(e); @@ -706,13 +715,18 @@ private State buildStateKeyValue( return new State<>(key, error); } - ByteString payload = item.getData(); - byte[] data = payload == null ? null : payload.toByteArray(); - T value = stateSerializer.deserialize(data, type); String etag = item.getEtag(); if (etag.equals("")) { etag = null; } + + T value = null; + if (type != null) { + ByteString payload = item.getData(); + byte[] data = payload == null ? null : payload.toByteArray(); + value = stateSerializer.deserialize(data, type); + } + return new State<>(key, value, etag, item.getMetadataMap(), null); } @@ -723,7 +737,11 @@ private State buildStateKeyValue( TypeRef type) throws IOException { ByteString payload = response.getData(); byte[] data = payload == null ? null : payload.toByteArray(); - T value = stateSerializer.deserialize(data, type); + T value = null; + if (type != null) { + value = stateSerializer.deserialize(data, type); + } + String etag = response.getEtag(); if (etag.equals("")) { etag = null; @@ -1108,7 +1126,11 @@ private QueryStateItem buildQueryStateKeyValue( } ByteString payload = item.getData(); byte[] data = payload == null ? null : payload.toByteArray(); - T value = stateSerializer.deserialize(data, type); + T value = null; + if (type != null) { + value = stateSerializer.deserialize(data, type); + } + String etag = item.getEtag(); if (etag.equals("")) { etag = null; diff --git a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java index 1081220a9..336f20535 100644 --- a/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java @@ -25,6 +25,7 @@ import io.dapr.client.domain.ExecuteStateTransactionRequest; import io.dapr.client.domain.GetBulkStateRequest; import io.dapr.client.domain.GetStateRequest; +import io.dapr.client.domain.InvokeBindingRequest; import io.dapr.client.domain.PublishEventRequest; import io.dapr.client.domain.RuleMetadata; import io.dapr.client.domain.State; @@ -327,7 +328,7 @@ public void invokeBindingCallbackExceptionThrownTest() { @Test public void invokeBindingTest() throws IOException { DaprProtos.InvokeBindingResponse.Builder responseBuilder = - DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK")); + DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK")); doAnswer((Answer) invocation -> { StreamObserver observer = (StreamObserver) invocation.getArguments()[1]; observer.onNext(responseBuilder.build()); @@ -339,6 +340,23 @@ public void invokeBindingTest() throws IOException { result.block(); } + @Test + public void invokeBindingVoidReturnTest() throws IOException { + DaprProtos.InvokeBindingResponse.Builder responseBuilder = + DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK")); + doAnswer((Answer) invocation -> { + StreamObserver observer = (StreamObserver) invocation.getArguments()[1]; + observer.onNext(responseBuilder.build()); + observer.onCompleted(); + return null; + }).when(daprStub).invokeBinding(any(DaprProtos.InvokeBindingRequest.class), any()); + + var request = new InvokeBindingRequest("BindingName", "MyOperation"); + request.setData("request"); + Mono result = client.invokeBinding(request, null); + result.block(); + } + @Test public void invokeBindingByteArrayTest() { DaprProtos.InvokeBindingResponse.Builder responseBuilder =