Skip to content

Question: DefaultTyping and java records/kotlin data classes #4636

@ThanksForAllTheFish

Description

@ThanksForAllTheFish

Describe your Issue

Java records are final, as well as Kotlin data classes; in version 2.17, DefaultTyping deprecates the EVERYTHING option (from #4160), however there are legit use cases to keep it, ie: when generic deserialization is needed for framework integration.

Is it possible to reverse the decision an "undeprecate" DefaultTyping.EVERYTHING?

For instance, in the following example (Kotlin because I got used to Kotlin, but I think the same applies to Java records too), using Redis in combination spring-data-redis as a cache, I could achieve proper generic deserialization only by using DefaultTyping.EVERYTHING.

@Caching(
    Cacheable(
      DISTRIBUTED_REDIS_CACHE,
      cacheManager = DISTRIBUTED_CACHE_MANAGER
    ),
  ]
)
open fun getPolicy(objectId: ObjectId): SomeDataClass { ... }

data class SomeDataClass(val value: String)

@Bean(DISTRIBUTED_CACHE_MANAGER)
  fun distributedCacheManager(
    redisConnectionFactory: RedisConnectionFactory,
    objectMapper: ObjectMapper
  ): RedisCacheManager = RedisCacheManager.builder(redisConnectionFactory)
    .withCacheConfiguration(
      DISTRIBUTED_REDIS_CACHE,
      RedisCacheConfiguration.defaultCacheConfig()
        .serializeValuesWith(
          fromSerializer(
            GenericJackson2JsonRedisSerializer(
              objectMapper.copy().activateDefaultTyping(objectMapper.polymorphicTypeValidator, ObjectMapper.DefaultTyping.EVERYTHING) //activating EVERYTHING on default typing, otherwise generic deser results in `LinkedHashMap
            )
          )
        )
    )
    .build()

Disclaimer: there might another way to get generic deserialization working, I experimented for a while and could only come up with this, but in no way I claim that this is the only solution.

Please also let me know if you think this is better addressed in Spring/Redis, to be honest I think something might be improved there to achieve the same result

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions