Skip to content

Commit

Permalink
!34 fix(RedisSerializer): 适配kotlin类型 (data class 是 final修饰的)
Browse files Browse the repository at this point in the history
* 适配kotlin类型
  • Loading branch information
mymx2 authored and ChunMengLu committed Apr 12, 2024
1 parent 82fde77 commit ecb4652
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import net.dreamlu.mica.core.utils.JsonUtil;
import net.dreamlu.mica.redis.cache.MicaRedisCache;
import net.dreamlu.mica.redis.resolver.DefaultRedisKeyResolver;
Expand All @@ -31,6 +30,7 @@
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.KotlinDetector;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
Expand Down Expand Up @@ -78,7 +78,11 @@ public RedisSerializer<Object> redisSerializer(MicaRedisProperties properties) {
objectMapper.findAndRegisterModules();
// class type info to json
GenericJackson2JsonRedisSerializer.registerNullValueSerializer(objectMapper, null);
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), DefaultTyping.NON_FINAL, As.PROPERTY);
if (KotlinDetector.isKotlinPresent()) {
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING, As.PROPERTY);
} else {
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, As.PROPERTY);
}
return new GenericJackson2JsonRedisSerializer(objectMapper);
}

Expand Down

0 comments on commit ecb4652

Please sign in to comment.