-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Duplicate fields with ScalaObjectMapper #267
Comments
Some strange code that may be related in
|
Can you provide an example of the case class that is causing problems? |
@christophercurrie I better provide a complete example. import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import scala.beans.BeanProperty
case class Test(@BeanProperty pId: Long)
val mapper = {
val objectMapper = new ObjectMapper with ScalaObjectMapper
objectMapper.registerModule(DefaultScalaModule)
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
objectMapper
}
println(mapper.writeValueAsString(Test(1L))) Result is
|
This appears to happen only when scala.beans.BeanProperty annotation is added - ScalaObjectMapper is not the cause (a plain ObjectMapper has same result) -- nor does setSerializationInclusion have any effect |
@cowtowncoder the effect of the Scala Is there a reason why legacyManglePropertyName in DefaultAccessorNamingStrategy lowercases the 'I' in getPId()? Enabling MapperFeature.USE_STD_BEAN_NAMING has the opposite problem - getPId() evaluates to PId instead. |
So: name mangling is to do what Java Bean specification does, to derive property name from getter/setter.
but what Bean naming actually does is that lower-casing is only applied if there is one upper-case character; otherwise name is to be left as-is. Hence
However: field names are never mangled in any way. At least they should not. The fundamental issue is deeper however: Scala (and Kotlin) start with property as a concept, matching field; then create accessors. But name mapping cannot be guaranteed as bijective (so that you can losslessly translate in either direction). It should not be impossible to reconcile these but... it has been tricky so far. |
When there is a
case class
withBeanProperty
-annotated field, the following code sometimes generates JSON with duplicate fields. For example,pId
field gets serialized twice, aspId
andpid
.It's impossible to remove
BeanProperty
annotation, because it's needed by another library. Also it's impossible to setBeanProperty
on every field and removeScalaObjectMapper
, as such changes are numerous and would likely result in other compatibility issues. What's the proper way to fix this problem?The text was updated successfully, but these errors were encountered: