Skip to content

Commit 68f514b

Browse files
committed
Refine initialization of the TypeResolver in GenericJackson2JsonRedisSerializer.
Closes #2750
1 parent 12f5fad commit 68f514b

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

src/main/java/org/springframework/data/redis/serializer/GenericJackson2JsonRedisSerializer.java

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.cache.support.NullValue;
2525
import org.springframework.core.KotlinDetector;
26-
import org.springframework.data.redis.util.RedisAssertions;
2726
import org.springframework.data.util.Lazy;
2827
import org.springframework.lang.Nullable;
2928
import org.springframework.util.Assert;
@@ -34,12 +33,14 @@
3433
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
3534
import com.fasterxml.jackson.core.JsonGenerator;
3635
import com.fasterxml.jackson.core.TreeNode;
36+
import com.fasterxml.jackson.databind.DeserializationConfig;
3737
import com.fasterxml.jackson.databind.JavaType;
3838
import com.fasterxml.jackson.databind.JsonNode;
3939
import com.fasterxml.jackson.databind.ObjectMapper;
4040
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
4141
import com.fasterxml.jackson.databind.SerializerProvider;
4242
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
43+
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
4344
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
4445
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
4546
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -163,27 +164,55 @@ public GenericJackson2JsonRedisSerializer(ObjectMapper mapper, JacksonObjectRead
163164
private GenericJackson2JsonRedisSerializer(ObjectMapper mapper, JacksonObjectReader reader,
164165
JacksonObjectWriter writer, @Nullable String typeHintPropertyName) {
165166

166-
this.mapper = RedisAssertions.requireNonNull(mapper, "ObjectMapper must not be null");
167-
this.reader = RedisAssertions.requireNonNull(reader, "Reader must not be null");
168-
this.writer = RedisAssertions.requireNonNull(writer, "Writer must not be null");
167+
Assert.notNull(mapper, "ObjectMapper must not be null");
168+
Assert.notNull(reader, "Reader must not be null");
169+
Assert.notNull(writer, "Writer must not be null");
170+
171+
this.mapper = mapper;
172+
this.reader = reader;
173+
this.writer = writer;
169174

170175
this.defaultTypingEnabled = Lazy.of(() -> mapper.getSerializationConfig().getDefaultTyper(null) != null);
171176

172-
this.typeResolver = new TypeResolver(Lazy.of(mapper::getTypeFactory),
173-
newTypeHintPropertyNameSupplier(mapper, typeHintPropertyName, this.defaultTypingEnabled));
177+
this.typeResolver = newTypeResolver(mapper, typeHintPropertyName, this.defaultTypingEnabled);
174178
}
175179

176-
private Supplier<String> newTypeHintPropertyNameSupplier(ObjectMapper mapper, @Nullable String typeHintPropertyName,
180+
private TypeResolver newTypeResolver(ObjectMapper mapper, @Nullable String typeHintPropertyName,
177181
Lazy<Boolean> defaultTypingEnabled) {
178182

179-
return typeHintPropertyName != null ? () -> typeHintPropertyName
180-
: Lazy
181-
.of(() -> defaultTypingEnabled.get() ? null
182-
: mapper.getDeserializationConfig().getDefaultTyper(null)
183-
.buildTypeDeserializer(mapper.getDeserializationConfig(),
184-
mapper.getTypeFactory().constructType(Object.class), Collections.emptyList())
185-
.getPropertyName())
186-
.or("@class");
183+
Lazy<TypeFactory> lazyTypeFactory = Lazy.of(mapper::getTypeFactory);
184+
185+
Lazy<String> lazyTypeHintPropertyName = typeHintPropertyName != null ? Lazy.of(typeHintPropertyName)
186+
: newLazyTypeHintPropertyName(mapper, defaultTypingEnabled);
187+
188+
return new TypeResolver(lazyTypeFactory, lazyTypeHintPropertyName);
189+
}
190+
191+
private Lazy<String> newLazyTypeHintPropertyName(ObjectMapper mapper, Lazy<Boolean> defaultTypingEnabled) {
192+
193+
Lazy<String> configuredTypeDeserializationPropertyName = getConfiguredTypeDeserializationPropertyName(mapper);
194+
195+
Lazy<String> resolvedLazyTypeHintPropertyName = Lazy.of(() -> defaultTypingEnabled.get() ? null
196+
: configuredTypeDeserializationPropertyName.get());
197+
198+
resolvedLazyTypeHintPropertyName = resolvedLazyTypeHintPropertyName.or("@class");
199+
200+
return resolvedLazyTypeHintPropertyName;
201+
}
202+
203+
private Lazy<String> getConfiguredTypeDeserializationPropertyName(ObjectMapper mapper) {
204+
205+
return Lazy.of(() -> {
206+
207+
DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
208+
209+
JavaType objectType = mapper.getTypeFactory().constructType(Object.class);
210+
211+
TypeDeserializer typeDeserializer = deserializationConfig.getDefaultTyper(null)
212+
.buildTypeDeserializer(deserializationConfig, objectType, Collections.emptyList());
213+
214+
return typeDeserializer.getPropertyName();
215+
});
187216
}
188217

189218
/**
@@ -336,7 +365,8 @@ protected JavaType resolveType(byte[] source, Class<?> type) throws IOException
336365
*/
337366
private static class NullValueSerializer extends StdSerializer<NullValue> {
338367

339-
@Serial private static final long serialVersionUID = 1999052150548658808L;
368+
@Serial
369+
private static final long serialVersionUID = 1999052150548658808L;
340370

341371
private final String classIdentifier;
342372

0 commit comments

Comments
 (0)