pjfanning commented on code in PR #3665:
URL: https://github.com/apache/pekko/pull/3665#discussion_r4357657779


##########
remote/src/test/scala/org/apache/pekko/remote/serialization/ScalaPbSerializationSpec.scala:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pekko.remote.serialization
+
+import java.io.File
+import java.nio.file.Files
+import java.util.concurrent.TimeUnit
+
+import scala.concurrent.Await
+import scala.concurrent.duration._
+
+import com.google.protobuf.timestamp.Timestamp
+
+import org.apache.pekko
+import pekko.actor.{ ActorSystem, ExtendedActorSystem }
+import pekko.serialization.{ SerializationExtension, Serializers }
+import pekko.testkit.PekkoSpec
+
+import com.typesafe.config.ConfigFactory
+
+class ScalaPbOverrideSerializer(system: ExtendedActorSystem) extends 
ProtobufSerializer(system)
+
+object ScalaPbAbsentClasspath {
+  def main(args: Array[String]): Unit = {
+    require(args.isEmpty)
+    
require(scala.util.Try(Class.forName("scalapb.GeneratedMessage")).isFailure, 
"ScalaPB must be absent")
+    val system = ActorSystem("without-scalapb")
+    try {
+      val serialization = SerializationExtension(system)
+      require(serialization.deserialize(serialization.serialize("still 
works").get, classOf[String]).get ==
+        "still works")
+      println("ActorSystem and serialization started without ScalaPB")
+    } finally Await.result(system.terminate(), 15.seconds)
+  }
+}
+
+class ScalaPbSerializationSpec extends PekkoSpec {
+  private val serialization = SerializationExtension(system)
+  private val message = Timestamp(seconds = 123456789L, nanos = 123456789)
+
+  "Optional ScalaPB serialization" must {
+    "select the default binding and round-trip an actual generated message" in 
{
+      serialization.findSerializerFor(message).getClass shouldBe 
classOf[ProtobufSerializer]
+      val bytes = serialization.serialize(message).get
+      serialization.deserialize(bytes, classOf[Timestamp]).get shouldBe message
+      serialization.deserialize(serialization.serialize(message).get, 
classOf[Timestamp]).get shouldBe message

Review Comment:
   Nit: this repeats the round trip on the line above (`bytes` already holds 
`serialization.serialize(message).get`).
   
   ```suggestion
   ```



##########
remote/src/main/scala/org/apache/pekko/remote/serialization/ProtobufSerializer.scala:
##########
@@ -39,7 +39,14 @@ object ProtobufSerializer {
       MethodType.methodType(Void.TYPE, classOf[Throwable]))
 
   private def parsingHandle(clazz: Class[?]): MethodHandle = {
-    val methodType = MethodType.methodType(clazz, classOf[Array[Byte]])
+    // ScalaPB's static forwarder can return GeneratedMessage rather than the 
concrete message class.
+    val method =
+      try clazz.getDeclaredMethod("parseFrom", classOf[Array[Byte]])
+      catch {
+        case _: NoSuchMethodException => clazz.getMethod("parseFrom", 
classOf[Array[Byte]])
+      }
+    val returnType = method.getReturnType
+    val methodType = MethodType.methodType(returnType, classOf[Array[Byte]])

Review Comment:
   The old `methodType(clazz, ...)` lookup implicitly guaranteed that 
`parseFrom` returns the manifest class. With the declared return type, any 
static `parseFrom(byte[])` is now accepted whatever it returns. Since #3651 
every `scalapb.GeneratedMessage` subtype is on the allow list by default, so I 
think it is worth keeping that guarantee:
   
   ```suggestion
       val returnType = method.getReturnType
       require(
         returnType.isAssignableFrom(clazz),
         s"parseFrom of [${clazz.getName}] returns unrelated type 
[${returnType.getName}]")
       val methodType = MethodType.methodType(returnType, classOf[Array[Byte]])
   ```
   
   For the runtime result as well, the handle can be narrowed back to the 
manifest class before it is wrapped (line 55, outside this hunk). `asType` 
inserts a cast for a reference return type, so a wrongly typed result fails 
here with a `ClassCastException` instead of reaching user code:
   
   ```scala
       val invoker = 
wrapTargetException(handle.asType(MethodType.methodType(clazz, 
classOf[Array[Byte]])))
         .asType(parsingInvokerType)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to