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

CanBuildFrom/GenTraversableFactory causing serialization issue #579

Open
nevillelyh opened this issue Oct 24, 2019 · 4 comments
Open

CanBuildFrom/GenTraversableFactory causing serialization issue #579

nevillelyh opened this issue Oct 24, 2019 · 4 comments

Comments

@nevillelyh
Copy link
Contributor

I can reproduce with the following code on scala 2.12.

import java.io.{ByteArrayOutputStream, ObjectOutputStream}

import org.scalacheck._

object SerDe {
  def main(args: Array[String]): Unit = {
    val oos = new ObjectOutputStream(new ByteArrayOutputStream)

    val arbInt = implicitly[Arbitrary[Int]]
    oos.writeObject(arbInt)

    val arbList = implicitly[Arbitrary[List[Int]]]
    oos.writeObject(arbList)
  }
}

Looks like it's summoning the following implicit in BuildableVersionSpecific.

implicit def buildableCanBuildFrom[T,F,C](implicit c: CanBuildFrom[F,T,C]): Buildable[T,C]

And I can see one for list in SerializableCanBuildFroms

implicit def listCanBuildFrom[T]: CanBuildFrom[List[T], T, List[T]]

But somehow it's not summoned.

Stack trace:

[error] (run-main-2) java.io.NotSerializableException: scala.collection.generic.GenTraversableFactory$$anon$1
[error] java.io.NotSerializableException: scala.collection.generic.GenTraversableFactory$$anon$1
[error]         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
[error]         at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
[error]         at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
[error]         at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
[error]         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) scalacheck / Test / runMain 0s
[error]         at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1378)
[error]         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
[error]         at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
[error]         at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
[error]         at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
[error]         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
[error]         at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
[error]         at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
[error]         at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
[error]         at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
[error]         at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
[error]         at magnolia.scalacheck.test.SerDe$.main(SerDe.scala:15)
[error]         at magnolia.scalacheck.test.SerDe.main(SerDe.scala)
[error]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.lang.reflect.Method.invoke(Method.java:498)
@nevillelyh
Copy link
Contributor Author

Adding import org.scalacheck.util.SerializableCanBuildFroms._ fixes the issue. Is it by design that they're not in scope?

@ashawley
Copy link
Contributor

ashawley commented Oct 25, 2019

There's a serializability test in ScalaCheck that works. Ironically, the import you needed was removed from that test in d8f1322. I know forking the JVM was needed for the test to work in Scala 2.13, but you're asking about 2.12, so I'm not sure why you need the import.

@nevillelyh
Copy link
Contributor Author

That test you linked doesn't cover Arbitrary[List[T]] though, which summons CanBuildFrom transitively. So it was probably an unused import. Forking for 2.13 might be a different issue since CanBuildFrom was replaced with Factory which is serializable.

So it does look like these implicits need to be manually imported.

@ashawley
Copy link
Contributor

Yes, a test of Arbitrary[List[T]] is missing. Good catch. For that test, it would require that import statement.

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