diff --git a/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java b/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java index 92cb523f69..426856ff10 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientGrpc.java @@ -283,7 +283,7 @@ public Mono> getState(GetStateRequest request, TypeRef type) { this.createMono( it -> intercept(context, asyncStub).getState(envelope, it) ) - ).map( + ).flatMap( it -> { try { return buildStateKeyValue(it, key, options, type); @@ -369,19 +369,22 @@ private State buildStateKeyValue( return new State<>(key, value, etag, item.getMetadataMap(), null); } - private State buildStateKeyValue( + private Mono> buildStateKeyValue( DaprProtos.GetStateResponse response, String requestedKey, StateOptions stateOptions, TypeRef type) throws IOException { ByteString payload = response.getData(); byte[] data = payload == null ? null : payload.toByteArray(); + if (data == null || data.length == 0) { + return Mono.empty(); + } T value = stateSerializer.deserialize(data, type); String etag = response.getEtag(); if (etag.equals("")) { etag = null; } - return new State<>(requestedKey, value, etag, response.getMetadataMap(), stateOptions); + return Mono.just(new State<>(requestedKey, value, etag, response.getMetadataMap(), stateOptions)); } /** diff --git a/sdk/src/main/java/io/dapr/client/DaprClientHttp.java b/sdk/src/main/java/io/dapr/client/DaprClientHttp.java index 76a8c6c058..11ca07e6a8 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientHttp.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientHttp.java @@ -350,7 +350,7 @@ public Mono> getState(GetStateRequest request, TypeRef type) { .invokeApi(DaprHttp.HttpMethods.GET.name(), pathSegments, queryParams, null, context) ).flatMap(s -> { try { - return Mono.justOrEmpty(buildState(s, key, options, type)); + return buildState(s, key, options, type); } catch (Exception ex) { return DaprException.wrapMono(ex); } @@ -513,15 +513,18 @@ public Mono deleteState(DeleteStateRequest request) { * @return A State instance * @throws IOException If there's a issue deserializing the response. */ - private State buildState( + private Mono> buildState( DaprHttp.Response response, String requestedKey, StateOptions stateOptions, TypeRef type) throws IOException { // The state is in the body directly, so we use the state serializer here. + if (response.getBody() == null || response.getBody().length == 0) { + return Mono.empty(); + } T value = stateSerializer.deserialize(response.getBody(), type); String etag = null; if (response.getHeaders() != null && response.getHeaders().containsKey("Etag")) { etag = response.getHeaders().get("Etag"); } - return new State<>(requestedKey, value, etag, stateOptions); + return Mono.just(new State<>(requestedKey, value, etag, stateOptions)); } /**