jackson-module-kotlin
jackson-module-kotlin copied to clipboard
InvalidDefinitionExceptions using kotlin 1.6.0
Describe the bug Hello! First, thanks for maintaining the project and all the work involved.
Since we updated Kotlin (kgp) in our project to 1.6.0 we started to see issues with r8 related with Jackson and Kotlin. Suddenly, the library is unable to de-serialize responses, mapped as data classes from our backend with the following errors
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of [...any dto class...] (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
To Reproduce
Set minifyEnabled
to true in your build script using kgp 1.6.0
Expected behavior A clear and concise description of what you expected to happen.
Versions
Kotlin: 1.6.0
Jackson-module-kotlin: 2.13.0
Jackson-databind: 2.13.0
Additional context
Also using:
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
(1.6.0)
Is minifyEnabled
a new feature in 1.6.0? Does it work if you turn that off?
A minimal reproduction would be helpful.
Thanks for your reply
minifyEnabled
is related to an Android project, not a kotlin feature.
I have created an android project reproducing the problem with the minimum complexity I could. Changing the kotlin version in the root build.gradle
to an earlier version than 1.6.0 'solves' the problem.
https://www.dropbox.com/s/46sj9is65om1fog/JacksonIssue.zip?dl=0
Having same issue, works with kotlin version 1.5.31, but throws these errors on 1.6.0 and forwards
I've been having the same issue as well. I worked around the issue by marking all my classes with a no arg annotation using this compiler plugin https://kotlinlang.org/docs/no-arg-plugin.html#gradle. Definitely not an ideal solution, but I couldn't get any added proguard exemption rules to have an effect. I've tried many different rules, including keeping entire packages both from fasterxml paths and my own code with no luck. I can confirm that the issue disappears if proguard is disabled, but that is not a feasible solution for me unfortunately.
~~I was able to fix this issue by adding @JsonCreator constructor
to the data class definition.~~ Nevermind, that actually didn't work.
@saulmm
I checked and this doesn't seem to be a problem with jackson-module-kotlin
.
When I run the following code, I cannot get the kotlin.Metadata
annotation in 1.6.0
, but I can get it in 1.5.31
.
override fun onCreate(savedInstanceState: Bundle?) {
println("######################################################")
println(MyEntity::class.java.annotations.asList())
println("######################################################")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
handler.post(longTaskThread)
}
This result suggests that the kotlin.Metadata
annotation has been removed by minify
.
Preventing kotlin.Metadata
from being removed will probably solve the problem(Sorry, I am not familiar with Android
, so I can't show you the specific method).
As a side note, if kotlin.Metadata
is removed, kotlin-reflect
will not be able to read the information, so there is no way to do anything about this on the Jackson
side.
Thanks @k163377, you pointed me in the right direction!
Apparently this is an R8 bug: https://issuetracker.google.com/issues/206855609
Following https://issuetracker.google.com/issues/206855609#comment9 solved the issue.
While solved in the meantime in R8, the issue is back after upgrading to Kotlin 1.8
. No proguard rules in the world are helping, only downgrading Kotlin again does.
@grote I would suggest filing a new issue, with reference to this as background. The reason is that we usually do not re-open issues that have been closed for a while (i.e. if something is caught immediately before new releases, then, but not if issue may have been mentioned in release notes).
Closed as resolved.