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

ScalaObjectMapper serializes twice #554

Closed
mosesn opened this issue Nov 8, 2021 · 2 comments
Closed

ScalaObjectMapper serializes twice #554

mosesn opened this issue Nov 8, 2021 · 2 comments

Comments

@mosesn
Copy link

mosesn commented Nov 8, 2021

We just moved from jackson 2.11.4 to 2.13.0, and we found a performance bug. It seems like ScalaObjectMapper is calling the serialize method twice. We'd like to switch to ClassTagExtensions, but we depend heavily on ScalaObjectMapper, and want to be on a stable version that has both ScalaObjectMapper as well as ClassTagExtensions before we migrate.

Can you please fix this regression in a patch release?

Reproduction case:

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.ScalaObjectMapper
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.Checkers
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

final case class DerpWrapper(derps: Seq[Derp])

final case class Derp(var one: Long) {
  var serializationCounter = 0
}

class JacksonUpgradeTest
    extends AnyFunSuite
    with ScalaCheckDrivenPropertyChecks
    with Checkers
    with Matchers {

  private object DerpModule extends SimpleModule {
    private object DerpResponseSerializer extends StdSerializer[Derp](classOf[Derp]) {
      override def serialize(
        value: Derp,
        gen: JsonGenerator,
        provider: SerializerProvider
      ): Unit = {
        value.serializationCounter = value.serializationCounter + 1
      }
    }
    addSerializer(DerpResponseSerializer)
  }

  val mapper: ObjectMapper with ScalaObjectMapper = {
    val m = new ObjectMapper with ScalaObjectMapper
    m.registerModule(DefaultScalaModule)
      .registerModule(DerpModule)
    m
  }

  test("jackson test") {
    val derps = DerpWrapper(Seq(Derp(1)))
    mapper.valueToTree[JsonNode](derps)
    // the serializer should only be called once, but is getting called twice
    derps.derps.head.serializationCounter should equal(1)
  }
}
@mosesn
Copy link
Author

mosesn commented Nov 8, 2021

Actually, looks like this might be a databind issue, not module-scala. FasterXML/jackson-databind#3308 seems to be the root cause!

@mosesn
Copy link
Author

mosesn commented Nov 8, 2021

Closing for now, will reopen if we're mistaken. Sorry for the noise!

@mosesn mosesn closed this as completed Nov 8, 2021
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

1 participant