Skip to content

ClassCastException when serializing Kotlin object with inline value class property to JSON #1065

@joaodias14

Description

@joaodias14

Search before asking

  • I searched in the issues and found nothing similar.
  • I have confirmed that the same problem is not reproduced if I exclude the KotlinModule.
  • I searched in the issues of databind and other modules used and found nothing similar.
  • I have confirmed that the problem does not reproduce in Java and only occurs when using Kotlin and KotlinModule.

Describe the bug

While migrating from Spring Boot 3.5.8 to 4.0.0 (including the migration to Jackson 3), I started facing the following error when serializing a Kotlin object with an inline value class property (backed by a String property) to JSON:

Caused by: java.lang.ClassCastException: class com.example.demo.VehicleId cannot be cast to class java.lang.String (com.example.demo.VehicleId is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at tools.jackson.databind.ser.jdk.StringSerializer.isEmpty(StringSerializer.java:27) ~[jackson-databind-3.0.2.jar:3.0.2]
	at tools.jackson.databind.ser.std.StdDelegatingSerializer.isEmpty(StdDelegatingSerializer.java:211) ~[jackson-databind-3.0.2.jar:3.0.2]
	at tools.jackson.databind.ser.BeanPropertyWriter.serializeAsProperty(BeanPropertyWriter.java:592) ~[jackson-databind-3.0.2.jar:3.0.2]
	at tools.jackson.databind.ser.UnrolledBeanSerializer.serializeNonFiltered(UnrolledBeanSerializer.java:203) ~[jackson-databind-3.0.2.jar:3.0.2]

This happens with the following Jackson configuration:

jacksonMapperBuilder()
    .apply {
        changeDefaultPropertyInclusion { it.withValueInclusion(JsonInclude.Include.NON_EMPTY) }
        changeDefaultPropertyInclusion { it.withContentInclusion(JsonInclude.Include.NON_EMPTY) }
    }.build()

And with an object of the following Vehicle class including a non-null VehicleId:

data class Vehicle(
    val id: VehicleId?,
    val name: String,
    val transmissions: Map<VehicleTransmission, String>,
)

@JvmInline
value class VehicleId(
    val value: String,
)

@JvmInline
value class VehicleTransmission(
    val value: String,
)

E.g.:

Vehicle(
    id = VehicleId("vehicle-1"),
    name = "Vehicle #1",
    transmissions = mapOf(VehicleTransmission("automatic") to "Automatic"),
)

The same does not happen the same property includes no VehicleId (notice that there is no issue with an inline value class backedn by a String property being used as a Map key):

Vehicle(
    id = null,
    name = "Vehicle #2",
    transmissions = mapOf(VehicleTransmission("automatic") to "Automatic"),
)

If the following Jackson configuration is used instead, there is no error:

jacksonMapperBuilder()
    .apply {
        changeDefaultPropertyInclusion { it.withValueInclusion(JsonInclude.Include.NON_NULL) }
        changeDefaultPropertyInclusion { it.withContentInclusion(JsonInclude.Include.NON_EMPTY) }
    }.build()

The reason is that there is no check for the property VehicleId being empty, just null.

The exception is thrown by a Jackson-databind class, but given that this is a specific Kotlin behaviour I decided to open this issue in this repository. Thanks!

To Reproduce

There is a working example of this at https://github.com/joaodias14/demo-value-classes-serialization.
Just run it and then execute a GET request to http://localhost:8080/demo/v1/vehicles/error. This will trigger the error.
A GET request to http://localhost:8080/demo/v1/vehicles/ok will trigger no error, as the serialized Vehicle response does not include the VehicleId property.

Expected behavior

No response

Versions

Kotlin: 2.2.21
Jackson-module-kotlin: 3.0.2
Jackson-databind: 3.0.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions