FasterXML / FasterXML/jackson-module-kotlin
If the value wrapped in `value class` is `null`, it is not excluded from serialization
- Dominant language
- Kotlin
- Stars
- 1.2k
- Forks
- 187
- Avg merge
- 9h 48m
- Merged PRs (30d)
- 16
Description
**Describe the bug**
If a Dto contains fields of value class type and the value is null, it is not excluded from serialization with @JsonInclude(JsonInclude.Include.NON_NULL).
The only working workaround I've found is to use JsonInclude.Include.CUSTOM with valueFilter { object == null }
**Version information**
2.14.2
**To Reproduce**
```
@JvmInline
value class Wrapped(val rawValue: String)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class NonNullDto (
val string: String? = null,
val wrapped: Wrapped? = null,
)
@JsonInclude(JsonInclude.Include.CUSTOM, valueFilter = NullFilter::class)
data class CustomDto (
val string: String? = null,
val wrapped: Wrapped? = null,
)
class NullFilter {
override fun equals(other: Any?) = other == null
}
@Test
fun `should not serialize null values with NonNull`() {
println(jackson.writeValueAsString(NonNullDto()))
println(jackson.writeValueAsString(CustomDto()))
}
```
Result for NonNullDto:
`{"wrapped":null}`
Result for CustomDto:
`{}`
**Expected behavior**
JsonInclude.Include.NON_NULL should exclude null values for kotlin value classes.
**Additional context**
Kotlin version: 1.8.10
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.