Polishing.
Reduce constructor visibility and add tests with user provided ObjectMapper. Original Pull Request: #2332
This commit is contained in:
@@ -242,6 +242,9 @@ public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Objec
|
||||
return typeResolver.resolveType(source, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0
|
||||
*/
|
||||
static class TypeResolver {
|
||||
|
||||
// need a separate instance to bypass class hint checks
|
||||
@@ -250,7 +253,7 @@ public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Objec
|
||||
private final Supplier<TypeFactory> typeFactory;
|
||||
private final Supplier<String> hintName;
|
||||
|
||||
public TypeResolver(Supplier<TypeFactory> typeFactory, Supplier<String> hintName) {
|
||||
TypeResolver(Supplier<TypeFactory> typeFactory, Supplier<String> hintName) {
|
||||
|
||||
this.typeFactory = typeFactory;
|
||||
this.hintName = hintName;
|
||||
@@ -271,7 +274,6 @@ public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Objec
|
||||
|
||||
return constructType(type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,5 +311,4 @@ public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Objec
|
||||
serialize(value, gen, serializers);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
@@ -164,6 +165,22 @@ class GenericJackson2JsonRedisSerializerUnitTests {
|
||||
assertThat(serializer.deserialize(serializer.serialize(source))).isEqualTo(source);
|
||||
}
|
||||
|
||||
@Test // GH-2322
|
||||
void readsToMapForNonDefaultTyping() {
|
||||
|
||||
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(new ObjectMapper());
|
||||
|
||||
User user = new User();
|
||||
user.email = "walter@heisenberg.com";
|
||||
user.id = 42;
|
||||
user.name = "Walter White";
|
||||
|
||||
byte[] serializedValue = serializer.serialize(user);
|
||||
|
||||
Object deserializedValue = serializer.deserialize(serializedValue, Object.class);
|
||||
assertThat(deserializedValue).isInstanceOf(Map.class);
|
||||
}
|
||||
|
||||
@Test // GH-2322
|
||||
void shouldConsiderWriter() {
|
||||
|
||||
@@ -182,6 +199,24 @@ class GenericJackson2JsonRedisSerializerUnitTests {
|
||||
assertThat(new String(result)).contains("id").contains("name").doesNotContain("email");
|
||||
}
|
||||
|
||||
@Test // GH-2322
|
||||
void considersWriterForCustomObjectMapper() {
|
||||
|
||||
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(new ObjectMapper(),
|
||||
JacksonObjectReader.create(), (mapper, source) -> {
|
||||
return mapper.writerWithView(Views.Basic.class).writeValueAsBytes(source);
|
||||
});
|
||||
|
||||
User user = new User();
|
||||
user.email = "walter@heisenberg.com";
|
||||
user.id = 42;
|
||||
user.name = "Walter White";
|
||||
|
||||
byte[] serializedValue = serializer.serialize(user);
|
||||
|
||||
assertThat(new String(serializedValue)).contains("id").contains("name").doesNotContain("email");
|
||||
}
|
||||
|
||||
@Test // GH-2322
|
||||
void shouldConsiderReader() {
|
||||
|
||||
@@ -309,10 +344,9 @@ class GenericJackson2JsonRedisSerializerUnitTests {
|
||||
}
|
||||
|
||||
static class Views {
|
||||
|
||||
interface Basic {}
|
||||
|
||||
interface Detailed {}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user