Skip to content

Serialization behavior of case objects is different when using scala 2.13 #429

@brharrington

Description

@brharrington

When using scala 2.12.10 it will serialize the fields of the case object. Using scala 2.13.1 it creates an empty object. Test case:

package com.fasterxml.jackson.module.scala.ser

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatestplus.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class CaseObjectSerializerTest extends FunSuite {
  test("serialize case object") {
    val mapper = new ObjectMapper()
    mapper.registerModule(DefaultScalaModule)
    val json = mapper.writeValueAsString(CaseObjectExample)
    assert(json === """{"field1":"test","field2":42}""")
  }
}

case object CaseObjectExample {
  val field1: String = "test"
  val field2: Int = 42
}

Results:

$ sbt ++2.12.10 "testOnly *CaseObjectSerializerTest*"
[info] CaseObjectSerializerTest:
[info] - serialize case object
[info] Run completed in 450 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 23 s, completed Sep 19, 2019 11:13:53 AM

$ sbt ++2.13.1 "testOnly *CaseObjectSerializerTest*"
[info] CaseObjectSerializerTest:
[info] - serialize case object *** FAILED ***
[info]   "{[]}" did not equal "{["field1":"test","field2":42]}" (CaseObjectSerializerTest.scala:15)
[info] Run completed in 370 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0
[info] *** 1 TEST FAILED ***
[error] Failed tests:
[error] 	com.fasterxml.jackson.module.scala.ser.CaseObjectSerializerTest
[error] (test:testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed Sep 19, 2019 11:16:50 AM

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions