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

invesigate issue with defaulted params in case classes when value in json is null #453

Closed
pjfanning opened this issue Jul 8, 2020 · 4 comments

Comments

@pjfanning
Copy link
Member

#87 (comment)

@pjfanning
Copy link
Member Author

@mley I reread the issue you raised and the new behaviour seems correct to me. There is no easy way to make the BeanIntrospector parameterisable to allow it to optionally behave the old way. My recommendation would be that you create a DTO class that has no defaults. I use https://scalalandio.github.io/chimney/ in my day job a lot to do low boiler plate transforms between DTOs and internal model classes.

@pjfanning
Copy link
Member Author

You can also use jackson mixins.

  val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)
  mapper.addMixin[TestObject2, TestObject2Mixin]()

@mley
Copy link

mley commented Jul 12, 2020

Thanks for taking time to look into it, @pjfanning . In my opinion the behaviour is not correct, when the JSON value is set explicitly to null and the field is an Option type with a default value. In this case it never can get None when it's deserialized:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.scalatest.{FlatSpec, Matchers}

case class X(value: Option[String] = Some("value"))

class JacksonSpec extends FlatSpec with Matchers {

  val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule)

  "X" should " be serialised and deserialised correctly" in {
    val x = X(None)
    objectMapper.readValue(objectMapper.writeValueAsString(x), classOf[X]) shouldBe x
  }
}

Just for the record if other people stumble up on this: I could restore the old behaviour by creating my own custom Scala Jackson module with a custom ScalaAnnotationIntrospectorModule that does not register the ValueInstantiator. (removed this line:

)

But we are not going that way. We will go with the new behaviour and try to avoid of default values.

@pjfanning
Copy link
Member Author

pjfanning commented Jul 12, 2020

You can also register a variant of DefaultScalaModule that doesn't include ScalaAnnotationIntrospectorModule

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