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

chore: Cleanup JsonSupport #105

Merged
merged 6 commits into from
Dec 19, 2024
Merged

chore: Cleanup JsonSupport #105

merged 6 commits into from
Dec 19, 2024

Conversation

patriknw
Copy link
Member

  • deprecate json in pb Any
  • add encode/decode of json bytes

* deprecate json in pb Any
* add encode/decode of json bytes
@github-actions github-actions bot added documentation documentation related java-sdk labels Dec 18, 2024
@@ -96,7 +98,10 @@ private JsonSupport() {
* for the JSON type instead.
*
* @see {{encodeJson(T, String}}
*
* @deprecated Protobuf Any with JSON is not supported
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it still make sense for message broker messages?
I'd say that then it's better to use a more explicit protobuf message that carry the raw bytes and type hint.

When we add "real" protobuf serialization support I think we should revisit this. Would be better to have the serialization support as something that is injected rather than static methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we should add protobuf as internal serialization format at all, we can trigger it by the fact that there is a Java protobuf message base types, that way we wont need explicit user serialization like this.

BTW I think we can drop these completely, as there is no way to use them that makes sense in the new SDK, they exist because of basing off of old SDK but they could never really be used here.

The only use case where this class can be used for anything useful right now, afaics, is to do streaming JSON encoding for results in a query in a HTTP endpoint.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think protobuf can make sense when we add gRCP endpoints. Since that will happen later I think we can leave these as deprecated now and introduce proper serialization support at that point (and then remove these here).

@@ -117,7 +117,7 @@ private[akka] final case class RequestBuilderImpl[R](
override def withRequestBody(`object`: AnyRef): RequestBuilder[R] = {
if (`object` eq null) throw new IllegalArgumentException("object must not be null")
try {
val body = JsonSupport.encodeToBytes(`object`).toByteArray
val body = JsonSupport.encodeToAkkaByteString(`object`)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't this even wrong? encodeToBytes expected it to be in protobuf encoding?

public static <T> akka.util.ByteString encodeToAkkaByteString(T value) {
try {
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writerFor(value.getClass()).writeValueAsBytes(value));
} catch (JsonProcessingException ex) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapping the JsonProcessingException is not fully compatible change, but very inconvenient to have that as checked exception, and it's not like that in other places

@@ -15,9 +14,9 @@
class CustomerEventSerializationTest {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is used in docs

Copy link
Member

@johanandren johanandren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I think we can drop/hide more stuff.

@@ -96,7 +98,10 @@ private JsonSupport() {
* for the JSON type instead.
*
* @see {{encodeJson(T, String}}
*
* @deprecated Protobuf Any with JSON is not supported
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we should add protobuf as internal serialization format at all, we can trigger it by the fact that there is a Java protobuf message base types, that way we wont need explicit user serialization like this.

BTW I think we can drop these completely, as there is no way to use them that makes sense in the new SDK, they exist because of basing off of old SDK but they could never really be used here.

The only use case where this class can be used for anything useful right now, afaics, is to do streaming JSON encoding for results in a query in a HTTP endpoint.

ArrayNode dynamicJson = objectNode.putArray(key);
values.forEach(v -> dynamicJson.add(v.toString()));
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writeValueAsBytes(objectNode));
public static akka.util.ByteString encodeDynamicCollectionToAkkaByteString(String key, Collection<?> values) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite strange that this ever was public API I think it was mostly methods we needed ourselves internally that accidentally became public API. We should reconsider that and limit the surface to intentionally public API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deprecated those, and changed to internal JsonSerializer: c9f7b3b

@@ -17,7 +15,7 @@ class CustomerEventSerializationTest {
@Test
public void shouldDeserializeWithMandatoryField() {
//given
Any serialized = JsonSupport.encodeJson(new CustomerEvent.NameChanged("andre"));
ByteString serialized = JsonSupport.encodeToAkkaByteString(new CustomerEvent.NameChanged("andre"));
Copy link
Member Author

@patriknw patriknw Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work, because I think we are loosing some migration. Somewhat strange for this test case, but understand that it will be a problem for the other migration cases.

Not sure what we shall do? Using pb Any in our docs doesn't make sense. BytesPayload could be a replacement but that is not intended to be user facing. What do you think @aludwiko ?

@johanandren johanandren merged commit b292090 into java-spi Dec 19, 2024
22 checks passed
@johanandren johanandren deleted the wip-JsonSupport-patriknw branch December 19, 2024 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation documentation related java-sdk
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants