spring-cloud-function icon indicating copy to clipboard operation
spring-cloud-function copied to clipboard

Jackson 2 KotlinModule cannot be cast to Jackson 3 JacksonModule

Open goerge opened this issue 1 month ago • 2 comments

Describe the bug Spring Cloud Function Context 5.0.0:

ContextFunctionCatalogAutoConfiguration tries to cast Jackson 2's com.fasterxml.jackson.module.kotlin.KotlinModule" to Jackson 3's tools.jackson.databind.JacksonModule in ContextFunctionCatalogAutoConfiguration:268 resulting in

…
Caused by: java.lang.ClassCastException: class com.fasterxml.jackson.module.kotlin.KotlinModule cannot be cast to class tools.jackson.databind.JacksonModule (com.fasterxml.jackson.module.kotlin.KotlinModule and tools.jackson.databind.JacksonModule are in unnamed module of loader 'app')
	at org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration$JsonMapperConfiguration.jackson(ContextFunctionCatalogAutoConfiguration.java:270)
	at org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration$JsonMapperConfiguration.jsonMapper(ContextFunctionCatalogAutoConfiguration.java:229)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:155)
	... 51 more

Sample Any Kotlin application using Spring Cloud Function Context 5.0.0 should fail.

goerge avatar Nov 25 '25 17:11 goerge

I have the same issue, is it as simple as changing the package here to tools.jackson ?

This blocks upgrading to Spring Boot 4 unfortunately

palmski avatar Nov 27 '25 11:11 palmski

FWIW I forked the repo and made the suggested change and it works locally in my Kotlin project I'll raise a PR - just jumping through necessary hoops internally

palmski avatar Nov 27 '25 15:11 palmski

We ran into the same issue. Any workarounds or plans to be fixed without forking the codebase?

gstanchev avatar Dec 22 '25 21:12 gstanchev

try to remove the jackson 2 kotlin module from your classpath and add the jackson 3 kotlin module manually for now.

If that's not possible for whatever reason you can override completely the JacksonMapper by redoing what they are configuring:

import org.springframework.cloud.function.json.JacksonMapper
import org.springframework.cloud.function.json.JsonMapper
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import tools.jackson.core.StreamReadFeature
import tools.jackson.databind.DeserializationFeature
import tools.jackson.databind.SerializationFeature

@Configuration
class JacksonMapperConfiguration {
    // TODO: to remove when this issue is fixed
    // https://github.com/spring-cloud/spring-cloud-function/issues/1319
    @Bean
    fun cloudFunctionJsonMapper(builder: tools.jackson.databind.json.JsonMapper.Builder): JsonMapper {
        val jsonMapper =
            builder
                .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false)
                .configure(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION, false)
                .build()
        return JacksonMapper(jsonMapper)
    }
}

CharlyRien avatar Dec 23 '25 09:12 CharlyRien