Skip to content

Commit

Permalink
Add JavaTimeModule to ObjectSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mathecruz committed Jul 2, 2024
1 parent a0ee8d1 commit fa1d4c0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.mockito.Mockito;
import reactor.core.publisher.Mono;

import java.time.ZonedDateTime;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -435,6 +437,27 @@ public void invokeActorMethodWithoutDataWithVoidReturnType() {
Assertions.assertNull(emptyResponse);
}

@Test
public void invokeMethodWithZonedDateTime() {
MyData saveData = new MyData();
saveData.setPropertyA("valueA");
saveData.setPropertyB("valueB");
saveData.setMyData(saveData);
saveData.setDate(ZonedDateTime.now());

final ActorClient daprClient = mock(ActorClient.class);
when(daprClient.invoke(anyString(), anyString(), anyString(), Mockito.isNotNull()))
.thenReturn(Mono.empty());

final ActorProxy actorProxy = new ActorProxyImpl(
"myActorType",
new ActorId("100"),
new DefaultObjectSerializer(),
daprClient);

assertThrows(DaprException.class, () -> actorProxy.invokeMethod("getDate", saveData));
}

interface Actor {
MyData getData();

Expand Down Expand Up @@ -467,6 +490,8 @@ static class MyData {

private MyData myData;

private ZonedDateTime date;


public String getPropertyB() {
return propertyB;
Expand Down Expand Up @@ -499,6 +524,14 @@ public MyData getMyData() {
public void setMyData(MyData myData) {
this.myData = myData;
}

public ZonedDateTime getDate() {
return date;
}

public void setDate(ZonedDateTime date) {
this.date = date;
}
}

}
5 changes: 5 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
<artifactId>grpc-inprocess</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/main/java/io/dapr/client/ObjectSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.protobuf.MessageLite;
import io.dapr.client.domain.CloudEvent;
import io.dapr.utils.TypeRef;
Expand All @@ -35,6 +36,7 @@ public class ObjectSerializer {
*/
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(new JavaTimeModule())
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

/**
Expand Down

0 comments on commit fa1d4c0

Please sign in to comment.