dapr / dapr/java-sdk

differentiate between pass-through and byte[] for DaprObjectSerializer

Open
#500 17 comments 0 reactions 0 assignees View on GitHub
kind/bug question
Dominant language
Java
Stars
300
Forks
230
Avg merge
1d 20h
Merged PRs (30d)
4

Description

## Expected Behavior

byte[] can means 2 things.
* pass-through: do not serialize/deserialize the values. Sent/return the raw data. Other parts of the system will deal with raw data.
* normal byte[] values: serialize/deserialize them normally.

## Actual Behavior

Current [DaprObjectSerializer](https://github.com/dapr/java-sdk/blob/9296a8332336333ead172f460fb3b2209c446acb/sdk/src/main/java/io/dapr/serializer/DaprObjectSerializer.java#L15) does not differentiate between pass-through byte[] and normal byte[] values. The [DefaultObjectSerializer](https://github.com/dapr/java-sdk/blob/9296a8332336333ead172f460fb3b2209c446acb/sdk/src/main/java/io/dapr/serializer/DefaultObjectSerializer.java#L16) treats byte[] as always pass-through.

There are cases when serializing/deserializing byte[] as normal values is desired. For example, for json, it's desired to serialize byte[] as base64-encoded String because there's no other way to represent byte[] as legal JSON values.

I suggest to change the [DaprObjectSerializer](https://github.com/dapr/java-sdk/blob/9296a8332336333ead172f460fb3b2209c446acb/sdk/src/main/java/io/dapr/serializer/DaprObjectSerializer.java#L15) to follows:
```java
public interface DaprObjectSerializer {
default byte[] serialize(Object o) throws IOException {
// passThrough by default true, to make this not a breaking change.
return serialize(o, true);
}

byte[] serialize(Object o, boolean passThrough) throws IOException;

default T deserialize(byte[] data, TypeRef type) throws IOException {
return deserialize(data, type, true);
}

T deserialize(byte[] data, TypeRef type, boolean passThrough) throws IOException;

String getContentType();
}
```
and [DefaultObjectSerializer](https://github.com/dapr/java-sdk/blob/9296a8332336333ead172f460fb3b2209c446acb/sdk/src/main/java/io/dapr/serializer/DefaultObjectSerializer.java#L16) should be changed to honor explicit passThrough param.

## Steps to Reproduce the Problem

## Release Note

RELEASE NOTE:

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.