Uploaded image for project: 'Scala'
  1. Scala
  2. SCALA-447

Can't deserialize case class extending AnyVal

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 2.4.1
    • Fix Version/s: 2.4.2
    • Component/s: bson
    • Labels:
      None
    • Environment:
      scala 2.12.6

      Description

      I have a custom codec for a simple case class defined like this:
       

      case class ID[T](id: Int) extends AnyVal
      case class TestItem1(id: ID[TestItem1], name: Option[String])
      case class TestItem2(id: ID[TestItem2], name: Option[String])
       
      class IDCodec[T] extends Codec[ID[T]] {
        override def encode(writer: BsonWriter, value: ID[T], encoderContext: EncoderContext): Unit = writer.writeInt32(value.id)
        override def getEncoderClass: Class[ID[T]] = classOf[ID[T]]
        override def decode(reader: BsonReader, decoderContext: DecoderContext): ID[T] = ID[T](reader.readInt32())
      }
      

       

      The codec can serialize the case class into "{id: 1, name: "whatever}". If I comment out // extends AnyVal in the ID implementation, then it just works in both ways. But if ID extends AnyVal I get following exception:

      org.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException: ClassCastException was thrown during property evaluation.
       Message: java.lang.Integer cannot be cast to my.namespace.TestRegistry$ID
       Occurred when passed generated values (
       arg0 = TestItem1(ID(1),Some())
      )
      

       
      Can be wrong, but I think it worked in 2.3.0. We held it back because of index creation error. Now 2.4.1 has another bug

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: