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

scalapb-json4s: add support for ScalaPB generated code compiled with Scala 3 #1174

Closed
bjaglin opened this issue Jun 7, 2021 · 3 comments
Closed

Comments

@bjaglin
Copy link
Contributor

bjaglin commented Jun 7, 2021

Betting on Scala 2.13/3 compatibility, I tried to use scalapb-json4s_2.13 0.11.0 against classes compiled with Scala 3 from code generated by ScalaPB 0.11.3, and got the following errors

NoSuchFieldException

Caused by: java.lang.NoSuchFieldException: MODULE$
    at java.lang.Class.getField(Class.java:1999)
    at scalapb.json4s.FormatRegistry.companionForClass(JsonFormat.scala:52)
    at scalapb.json4s.FormatRegistry.registerMessageFormatter(JsonFormat.scala:63)
    at scalapb.json4s.FormatRegistry.registerWriter(JsonFormat.scala:86)
    at scalapb.json4s.JsonFormat$.<clinit>(JsonFormat.scala:711)

Looking at the classpath with sbt, it's unclear which binary version of scalapb-runtime is used (dependencyTree shows both but fullClasspath shows only 2.13?), but I assume the problem comes from the WKT classes included in scalapb-runtime_3, as we can see that the returned type inferred for companion() is different:

--- Timestamp_2_13.class.javap
+++ Timestamp_3.class.javap
-  public com.google.protobuf.timestamp.Timestamp$ companion();
+  public scalapb.GeneratedMessageCompanion<?> companion();

IncompatibleClassChangeError

java.lang.IncompatibleClassChangeError: Class scalapb.descriptors.PEnum$ does not implement the requested interface scala.Function1
    at scala.Option.fold(Option.scala:263)
    at scalapb.json4s.Parser.parseSingleValue(JsonFormat.scala:684)
    at scalapb.json4s.Parser.$anonfun$fromJsonToPMessage$2(JsonFormat.scala:580)
    at scala.collection.immutable.List.map(List.scala:246)
    at scalapb.json4s.Parser.parseValue$1(JsonFormat.scala:580)
    at scalapb.json4s.Parser.$anonfun$fromJsonToPMessage$3(JsonFormat.scala:602)
    at scala.collection.immutable.List.foreach(List.scala:333)
    at scalapb.json4s.Parser.fromJsonToPMessage(JsonFormat.scala:598)
    at scalapb.json4s.Parser.fromJson(JsonFormat.scala:530)
    at scalapb.json4s.Parser.fromJson(JsonFormat.scala:523)
    at scalapb.json4s.Parser.fromJsonString(JsonFormat.scala:517)
    at scalapb.json4s.JsonFormat$.fromJsonString(JsonFormat.scala:843)

Synthetic bytecode emitted for case classes has changed. For example, for case class Foo(a: Int)

--- Foo$_2_13.class.javap
+++ Foo$_3.class.javap
-public final class Foo$ extends scala.runtime.AbstractFunction1<java.lang.Object, Foo> implements java.io.Serializable {
+public final class Foo$ implements scala.deriving.Mirror$Product,java.io.Serializable {
   public static final Foo$ MODULE$;
   public static {};
-  public final java.lang.String toString();
   public Foo apply(int);
-  public scala.Option<java.lang.Object> unapply(Foo);
-  public java.lang.Object apply(java.lang.Object);
+  public Foo unapply(Foo);
+  public java.lang.String toString();
+  public Foo fromProduct(scala.Product);
+  public java.lang.Object fromProduct(scala.Product);
 }
@thesamet
Copy link
Contributor

@bjaglin Is there additional work needed to resolve this issue?

@bjaglin
Copy link
Contributor Author

bjaglin commented Jun 28, 2021

@bjaglin Is there additional work needed to resolve this issue?

Not to my knowledge. Thanks for the ping, I just spent some time finishing up scalapb/scalapb-validate#123 which was the original driver for this, and it's green 👍

@bjaglin bjaglin closed this as completed Jun 28, 2021
@bjaglin
Copy link
Contributor Author

bjaglin commented Jun 28, 2021

(I know it's not ideal and it would better be tested in ScalaPB itself, but at least if something regress we'll see it upon bumping https://github.com/scalapb/scalapb-validate)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants