Scalapb: Akka ProtobufSerializer does not play nicely with nested generated classes

Created on 20 Oct 2016  路  5Comments  路  Source: scalapb/ScalaPB

Relates to #27

When using a nested message, Java reflection fails to see the parseFrom method but has no trouble with toByteArray.

Most helpful comment

The culprit is that from some reason the scala compiler does not generate static methods for methods defined in a companion object of a nested class (but does so for companion objects of top level classes). This is why parseFrom isn't found.

I wrote a quick implementation Akka serializer that works with ScalaPB. Can you give it a try?

https://gist.github.com/thesamet/5d0349b40d3dc92859a1a2eafba448d5

All 5 comments

Reproduction steps

Created a nested message

message OuterMessage {
    message InnerMessage {
        string hiField = 1;
    }
    string helloField = 1;
   ...
}

Try to send just the inner message in a remote actor setting, i.e. through the PubSubMediator to a remote actor

mediator ! Publish(InnerMessage("hello"))

Using these settings:

actor {
    provider = "akka.cluster.ClusterActorRefProvider"
    serializers {
      proto = "akka.remote.serialization.ProtobufSerializer"
    }

    serialization-bindings {
      "com.trueaccord.scalapb.GeneratedMessage" = proto
    }
  }

The serializer throws an NoSuchMethodException from not being able to find the parseFrom method. This is coming from this line -> akka.remote.serialization.ProtobufSerializer.scalaLL69:

                if (method eq null) clazz.getDeclaredMethod("parseFrom", ProtobufSerializer.ARRAY_OF_BYTE_ARRAY: _*)

@nadavsr will decide, but I think that compatibility with akka's default protobuf serializer is out of scope of ScalaPB. Its messages even does not implement protobuf's message interface.

Gotcha. Yes, from what I can tell, the issue is on the akka protobuf side. The nested class that is generated by ScalaPB looks consistent.

The culprit is that from some reason the scala compiler does not generate static methods for methods defined in a companion object of a nested class (but does so for companion objects of top level classes). This is why parseFrom isn't found.

I wrote a quick implementation Akka serializer that works with ScalaPB. Can you give it a try?

https://gist.github.com/thesamet/5d0349b40d3dc92859a1a2eafba448d5

Thanks for sending, very helpful!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mroth picture mroth  路  10Comments

jportway picture jportway  路  13Comments

taeguk picture taeguk  路  4Comments

timo-schmid picture timo-schmid  路  38Comments

jon-morra-zefr picture jon-morra-zefr  路  14Comments