FasterXML / FasterXML/jackson-module-kotlin

Annotation given to constructor parameters containing `value class` as argument does not work

Open
#651 13 comments 1 reaction 0 assignees View on GitHub
bug enhancement value class
Dominant language
Kotlin
Stars
1.2k
Forks
187
Avg merge
9h 48m
Merged PRs (30d)
16

Description

Currently, annotations given to constructor parameters containing `value class` as an argument are ignored.

```kotlin
data class Data(@JsonProperty("prop") val ui: UInt = UInt.MAX_VALUE)

println(jacksonObjectMapper().writeValueAsString(Data())) // -> {"ui":4294967295}
```

The cause is that annotations are only given to parameters of the synthetic constructor (ignored by `Jackson`).

```java
// Excerpts from the decompile results
public final class Data {
private Data(int ui) {
this.ui = ui;
}

// $FF: synthetic method
public Data(@JsonProperty("prop") int ui, DefaultConstructorMarker $constructor_marker) {
this(ui);
}
}
```

## About workaround
Please change to give annotations to `getter`, `field`, etc.

```kotlin
data class Data(@get:JsonProperty("prop") val ui: UInt = UInt.MAX_VALUE) // -> {"prop":4294967295}
```

## About fix
The following issue in `Kotlin` needs to be resolved in order to be handled without bad hacks.
https://youtrack.jetbrains.com/issue/KT-30063

An experimental project, `jackson-module-kogera`, fixed this problem by introducing the following

https://github.com/ProjectMapK/jackson-module-kogera/blob/develop/src/main/kotlin/io/github/projectmapk/jackson/module/kogera/KotlinClassIntrospector.kt

Please note that this code appears to be unreviewed and fragile.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.