mirror of https://github.com/dapr/java-sdk.git
This reverts commit1eff655d82
, reversing changes made toa5051e07a8
.
This commit is contained in:
parent
589a352450
commit
c9ab3a6390
|
@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import io.dapr.actors.ActorId;
|
||||
import io.dapr.config.Properties;
|
||||
import io.dapr.serializer.DaprObjectSerializer;
|
||||
import io.dapr.serializer.DefaultObjectSerializer;
|
||||
import io.dapr.utils.TypeRef;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
|
@ -26,11 +27,6 @@ class DaprStateAsyncProvider {
|
|||
*/
|
||||
private static final Charset CHARSET = Properties.STRING_CHARSET.get();
|
||||
|
||||
/**
|
||||
* Marker to identify Json serializers.
|
||||
*/
|
||||
public static final String JSON_CONTENT_TYPE = "application/json";
|
||||
|
||||
/**
|
||||
* Handles special serialization cases.
|
||||
*/
|
||||
|
@ -49,7 +45,7 @@ class DaprStateAsyncProvider {
|
|||
/**
|
||||
* Flag determining if state serializer is the default serializer instead of user provided.
|
||||
*/
|
||||
private final boolean isStateSerializerJson;
|
||||
private final boolean isStateSerializerDefault;
|
||||
|
||||
/**
|
||||
* Instantiates a new Actor's state provider.
|
||||
|
@ -60,7 +56,7 @@ class DaprStateAsyncProvider {
|
|||
DaprStateAsyncProvider(DaprClient daprClient, DaprObjectSerializer stateSerializer) {
|
||||
this.daprClient = daprClient;
|
||||
this.stateSerializer = stateSerializer;
|
||||
this.isStateSerializerJson = JSON_CONTENT_TYPE.equals(stateSerializer.getContentType());
|
||||
this.isStateSerializerDefault = stateSerializer.getClass() == DefaultObjectSerializer.class;
|
||||
}
|
||||
|
||||
<T> Mono<T> load(String actorType, ActorId actorId, String stateName, TypeRef<T> type) {
|
||||
|
@ -73,7 +69,7 @@ class DaprStateAsyncProvider {
|
|||
}
|
||||
|
||||
T response = this.stateSerializer.deserialize(s, type);
|
||||
if (this.isStateSerializerJson && (response instanceof byte[])) {
|
||||
if (this.isStateSerializerDefault && (response instanceof byte[])) {
|
||||
if (s.length == 0) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
@ -142,7 +138,7 @@ class DaprStateAsyncProvider {
|
|||
try {
|
||||
byte[] data = this.stateSerializer.serialize(stateChange.getValue());
|
||||
if (data != null) {
|
||||
if (this.isStateSerializerJson && !(stateChange.getValue() instanceof byte[])) {
|
||||
if (this.isStateSerializerDefault && !(stateChange.getValue() instanceof byte[])) {
|
||||
// DefaultObjectSerializer is a JSON serializer, so we just pass it on.
|
||||
value = new String(data, CHARSET);
|
||||
} else {
|
||||
|
|
|
@ -7,7 +7,6 @@ package io.dapr.actors.runtime;
|
|||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.dapr.actors.ActorId;
|
||||
import io.dapr.client.ObjectSerializer;
|
||||
import io.dapr.serializer.DaprObjectSerializer;
|
||||
import io.dapr.serializer.DefaultObjectSerializer;
|
||||
import io.dapr.utils.TypeRef;
|
||||
|
@ -15,7 +14,6 @@ import org.junit.Assert;
|
|||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -29,29 +27,10 @@ public class DaprStateAsyncProviderTest {
|
|||
|
||||
private static final DaprObjectSerializer SERIALIZER = new DefaultObjectSerializer();
|
||||
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
private static final double EPSILON = 1e-10;
|
||||
|
||||
class CustomJsonSerializer implements DaprObjectSerializer{
|
||||
private final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Object o) throws IOException {
|
||||
return OBJECT_MAPPER.writeValueAsBytes(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T deserialize(byte[] data, TypeRef<T> type) throws IOException {
|
||||
return OBJECT_MAPPER.readValue(data, OBJECT_MAPPER.constructType(type.getType()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "application/json";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class used to test JSON serialization.
|
||||
*/
|
||||
|
@ -157,49 +136,6 @@ public class DaprStateAsyncProviderTest {
|
|||
verify(daprClient).saveStateTransactionally(eq("MyActor"), eq("123"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void happyCaseApplyWithCustomJsonSerializer() {
|
||||
DaprClient daprClient = mock(DaprClient.class);
|
||||
when(daprClient
|
||||
.saveStateTransactionally(
|
||||
eq("MyActor"),
|
||||
eq("123"),
|
||||
argThat(operations -> {
|
||||
if (operations == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (operations.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
ActorStateOperation operation = operations.get(0);
|
||||
if (operation.getOperationType() == null) {
|
||||
return false;
|
||||
}
|
||||
if (operation.getKey() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String opName = operation.getOperationType();
|
||||
String key = operation.getKey();
|
||||
Object value = operation.getValue();
|
||||
|
||||
return "upsert".equals(opName) &&
|
||||
"object".equals(key) &&
|
||||
"{\"id\":1000,\"name\":\"Roxane\"}".equals(value);
|
||||
})))
|
||||
.thenReturn(Mono.empty());
|
||||
|
||||
DaprStateAsyncProvider provider = new DaprStateAsyncProvider(daprClient, new CustomJsonSerializer());
|
||||
provider.apply("MyActor",
|
||||
new ActorId("123"),
|
||||
createInsertChange("object", new Customer().setId(1000).setName("Roxane")))
|
||||
.block();
|
||||
|
||||
verify(daprClient).saveStateTransactionally(eq("MyActor"), eq("123"), any());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void happyCaseLoad() throws Exception {
|
||||
DaprClient daprClient = mock(DaprClient.class);
|
||||
|
|
Loading…
Reference in New Issue