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

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

Closed
brharrington opened this issue Sep 19, 2019 · 5 comments
Closed
Milestone

Comments

@brharrington
Copy link
Contributor

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
pjfanning pushed a commit that referenced this issue Sep 20, 2019
@pjfanning
Copy link
Member

pjfanning commented Sep 20, 2019

I'm not really sure what is wrong. The difference I can see is at https://github.com/FasterXML/jackson-module-scala/blob/master/src/main/scala/com/fasterxml/jackson/module/scala/introspect/BeanIntrospector.scala#L170 - the CaseObjectExample$ class has fields in scala 2.12.10 but in scala 2.13, while getDeclaredFields returns the fields, they get filtered out in the for comprehension.

@pjfanning
Copy link
Member

the diff I think is in https://github.com/FasterXML/jackson-module-scala/blob/master/src/main/scala/com/fasterxml/jackson/module/scala/introspect/BeanIntrospector.scala#L102 - the fields are static in scala 2.13 and non-static in scala 2.12

@cowtowncoder
Copy link
Member

Lovely.

pjfanning pushed a commit that referenced this issue Sep 20, 2019
@pjfanning
Copy link
Member

Fix in 2.9.10 and will be in 2.10.0 - a bit of a hack but it will have to do for now

@brharrington
Copy link
Contributor Author

Confirmed it fixed the issues I was seeing. Thanks for the quick follow up.

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

3 participants