Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis ObjectMapper with kotlin data class don't work without DefaultTyping.EVERYTHING #4427

Closed
florianLaforest opened this issue Mar 13, 2024 · 2 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@florianLaforest
Copy link

florianLaforest commented Mar 13, 2024

Describe your Issue

In version 2.17, DefaultTyping.EVERYTHING has been deprecated.
I'm trying to change my redis mapper but without success.
I can't get it to work without DefaultTyping.EVERYTHING.

Here's my ObjectMapper:

    @Bean("redisObjectMapper")
    fun objectMapper(): ObjectMapper {
        val objectMapper = ObjectMapper().registerKotlinModule()
        return objectMapper.activateDefaultTyping(
            BasicPolymorphicTypeValidator.builder()
                .allowIfBaseType(Any::class.java)
                .build(),
            ObjectMapper.DefaultTyping.EVERYTHING,
        )
    }

And my RedisTemplate :

    @Bean
    fun redisTemplate(
        factory: RedisConnectionFactory,
        @Qualifier("redisObjectMapper") objectMapper: ObjectMapper,
    ): RedisTemplate<String, Any> {
        val template = RedisTemplate<String, Any>()
        template.setDefaultSerializer(GenericJackson2JsonRedisSerializer(objectMapper))
        template.keySerializer = StringRedisSerializer()
        template.hashKeySerializer = StringRedisSerializer()
        template.connectionFactory = factory
        return template
    }

There was already an issue about support for the kotlin data class: #2349

@florianLaforest florianLaforest added the to-evaluate Issue that has been received but not yet evaluated label Mar 13, 2024
@cowtowncoder
Copy link
Member

I really wish Redis mapper (and similar use cases) used plain old @JsonTypeInfo and wrapper class like so:

class CacheValue {
   @JsonTypeInfo(.... use class name etc ...)
   public Object value;
}

which should work the way to add type information for all values, without resorting to default typing.

Default typing and direct root value without wrapper is next to impossible to make work; and DefaultTyping.EVERYTHING is a nasty work-around.

Having said that, while deprecated, this functionality WILL REMAIN for all of 2.x, only to be removed from Jackson 3.0. And there's time to work through the issue for that transition.

@florianLaforest
Copy link
Author

All right, I've replaced EVERYTHING by JAVA_LANG_OBJECT and JsonTypeInfo as PROPERTY

    @Bean("redisObjectMapper")
    fun objectMapper(): ObjectMapper {
        val objectMapper = ObjectMapper().registerKotlinModule()
        return objectMapper.activateDefaultTyping(
            BasicPolymorphicTypeValidator.builder()
                .allowIfBaseType(Any::class.java)
                .build(),
            ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT,
            JsonTypeInfo.As.PROPERTY,
        )
    }

Then I added the JsonTypeInfo to all my classes that needed it

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)

I'm going to miss the EVERYTHING, it made things so much simpler. ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants