Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deserializing json.akka.io #2239

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Dependencies {
val RuntimeVersion = System.getProperty(
"kalix-runtime.version",
// temporarily accept the old system property name
System.getProperty("kalix-proxy.version", "1.1.46"))
System.getProperty("kalix-proxy.version", "1.2.0"))
}

// changing the Scala version of the Java SDK affects end users
Expand Down
13 changes: 11 additions & 2 deletions sdk/java-sdk-protobuf/src/main/java/kalix/javasdk/JsonSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
public final class JsonSupport {

public static final String KALIX_JSON = "json.kalix.io/";
public static final String AKKA_JSON = "json.akka.io/";

private static final ObjectMapper objectMapper = new ObjectMapper();

Expand Down Expand Up @@ -121,6 +122,10 @@ public static <T> ByteString encodeToBytes(T value) throws JsonProcessingExcepti
objectMapper.writerFor(value.getClass()).writeValueAsBytes(value));
}

private static boolean isKalixOrAkkaJson(String typeUrl) {
return typeUrl.startsWith(KALIX_JSON) || typeUrl.startsWith(AKKA_JSON);
}

/**
* Decode the given protobuf Any object to an instance of T using Jackson. The object must have
* the JSON string as bytes as value and a type URL starting with "json.kalix.io/".
Expand All @@ -132,12 +137,14 @@ public static <T> ByteString encodeToBytes(T value) throws JsonProcessingExcepti
* @throws IllegalArgumentException if the given value cannot be decoded to a T
*/
public static <T> T decodeJson(Class<T> valueClass, Any any) {
if (!any.getTypeUrl().startsWith(KALIX_JSON)) {
if (!(isKalixOrAkkaJson(any.getTypeUrl()))) {
throw new IllegalArgumentException(
"Protobuf bytes with type url ["
+ any.getTypeUrl()
+ "] cannot be decoded as JSON, must start with ["
+ KALIX_JSON
+ "] or ["
+ AKKA_JSON
+ "]");
} else {
try {
Expand Down Expand Up @@ -213,12 +220,14 @@ private static int parseVersion(String typeUrl) {
}

public static <T, C extends Collection<T>> C decodeJsonCollection(Class<T> valueClass, Class<C> collectionType, Any any) {
if (!any.getTypeUrl().startsWith(KALIX_JSON)) {
if (!(isKalixOrAkkaJson(any.getTypeUrl()))) {
throw new IllegalArgumentException(
"Protobuf bytes with type url ["
+ any.getTypeUrl()
+ "] cannot be decoded as JSON, must start with ["
+ KALIX_JSON
+ "] or ["
+ AKKA_JSON
+ "]");
} else {
try {
Expand Down
Loading