jackson-module-kotlin icon indicating copy to clipboard operation
jackson-module-kotlin copied to clipboard

InvalidDefinitionExceptions using kotlin 1.6.0

Open saulmm opened this issue 2 years ago • 7 comments

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)

saulmm avatar Nov 25 '21 09:11 saulmm

Is minifyEnabled a new feature in 1.6.0? Does it work if you turn that off?

A minimal reproduction would be helpful.

dinomite avatar Nov 25 '21 12:11 dinomite

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

saulmm avatar Nov 25 '21 21:11 saulmm

Having same issue, works with kotlin version 1.5.31, but throws these errors on 1.6.0 and forwards

laurynas-agmis avatar Jan 12 '22 13:01 laurynas-agmis

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.

RabieJradi avatar Jan 12 '22 13:01 RabieJradi

~~I was able to fix this issue by adding @JsonCreator constructor to the data class definition.~~ Nevermind, that actually didn't work.

C2H6O avatar Jan 14 '22 01:01 C2H6O

@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.

k163377 avatar Jan 21 '22 20:01 k163377

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.

C2H6O avatar Jan 21 '22 21:01 C2H6O

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 avatar Feb 15 '23 13:02 grote

@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).

cowtowncoder avatar Feb 17 '23 22:02 cowtowncoder

Closed as resolved.

k163377 avatar Feb 19 '23 10:02 k163377