microsoft / microsoft/durabletask-java
Customizable Serialization for Activity Return Values in Durable Functions
未關閉
還沒有人認領這個 Issue。
P2
- 主要語言
- Java
- 星號
- 29
- 分支
- 18
- 平均合併
- 1 天 10 小時
- 30 天內合併 PR
- 2
描述
What’s the problem?
Hey👋,
I'm working with Durable Functions in Java, and I’ve encountered an issue with how activity return values are serialized. Specifically:
- Activity functions return POJOs annotated with Jackson and Gson and having subTypes, which is deserialized using Gson when entering the activity but none is used for serializing return value.
- This creates inconsistencies.
- There's no way to customize the serialization mechanism for Durable Functions activities.
- Raises an Exception of InvalidTypeIdException because the type is missing at com.microsoft.durabletask.JacksonDataConverter#deserialize.
How to reproduce it?
Here’s a minimal Durable Function example where an Activity returns a POJO, but the Azure Functions Java Worker does not deserialize it properly:
1️⃣ POJO with Jackson & Gson annotations and Subtypes
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.*;
import java.io.IOException;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = AdminUser.class, name = "admin"),
@JsonSubTypes.Type(value = RegularUser.class, name = "regular")
})
@JsonAdapter(UserTypeAdapter.class)
public abstract class User {
private String id;
private String name;
public User() {}
public User(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() { return id; }
public String getName() { return name; }
}
public class AdminUser extends User {
private int adminLevel;
public AdminUser() {}
public AdminUser(String id, String name, int adminLevel) {
super(id, name);
this.adminLevel = adminLevel;
}
}
public class RegularUser extends User {
private String membershipStatus;
public RegularUser() {}
public RegularUser(String id, String name, String membershipStatus) {
super(id, name);
this.membershipStatus = membershipStatus;
}
}
public class UserTypeAdapter extends TypeAdapter<User> {
@Override
public void write(JsonWriter out, User user) throws IOException {
out.beginObject();
out.name("type").value(user instanceof AdminUser ? "admin" : "regular");
out.name("id").value(user.getId());
out.name("name").value(user.getName());
if (user instanceof AdminUser) {
out.name("adminLevel").value(((AdminUser) user).adminLevel);
} else if (user instanceof RegularUser) {
out.name("membershipStatus").value(((RegularUser) user).membershipStatus);
}
out.endObject();
}
@Override
public User read(JsonReader in) throws IOException {
in.beginObject();
String type = "";
String id = "";
String name = "";
int adminLevel = 0;
String membershipStatus = "";
while (in.hasNext()) {
String fieldName = in.nextName();
switch (fieldName) {
case "type":
type = in.nextString();
break;
case "id":
id = in.nextString();
break;
case "name":
name = in.nextString();
break;
case "adminLevel":
adminLevel = in.nextInt();
break;
case "membershipStatus":
membershipStatus = in.nextString();
break;
default:
in.skipValue();
break;
}
}
in.endObject();
return "admin".equals(type) ? new AdminUser(id, name, adminLevel) : new RegularUser(id, name, membershipStatus);
}
}
What do you want to happen?
✅ Supporting GSON for serialization.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 com.microsoft.durabletask.JacksonDataConverter 開始,追蹤 activity 回傳值如何被序列化以及之後如何被反序列化。使用提供的多型 User 範例來了解失敗原因,接著判斷可設定的 Gson serializer 應如何整合。完成的定義是:使用 Gson 註解的 activity POJO 能夠完成往返處理,且不會發生因缺少型別而導致的 InvalidTypeIdException。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- java
- 領域
- backend
- Issue 類型
- 功能
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100