Skip to content

Commit a55c73f

Browse files
Polishing.
Reduce constructor visibility and add tests with user provided ObjectMapper. Original Pull Request: #2332
1 parent c013a63 commit a55c73f

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ protected JavaType resolveType(byte[] source, Class<?> type) throws IOException
242242
return typeResolver.resolveType(source, type);
243243
}
244244

245+
/**
246+
* @since 3.0
247+
*/
245248
static class TypeResolver {
246249

247250
// need a separate instance to bypass class hint checks
@@ -250,7 +253,7 @@ static class TypeResolver {
250253
private final Supplier<TypeFactory> typeFactory;
251254
private final Supplier<String> hintName;
252255

253-
public TypeResolver(Supplier<TypeFactory> typeFactory, Supplier<String> hintName) {
256+
TypeResolver(Supplier<TypeFactory> typeFactory, Supplier<String> hintName) {
254257

255258
this.typeFactory = typeFactory;
256259
this.hintName = hintName;
@@ -271,7 +274,6 @@ protected JavaType resolveType(byte[] source, Class<?> type) throws IOException
271274

272275
return constructType(type);
273276
}
274-
275277
}
276278

277279
/**
@@ -309,5 +311,4 @@ public void serializeWithType(NullValue value, JsonGenerator gen, SerializerProv
309311
serialize(value, gen, serializers);
310312
}
311313
}
312-
313314
}

src/test/java/org/springframework/data/redis/serializer/GenericJackson2JsonRedisSerializerUnitTests.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import lombok.ToString;
2525

2626
import java.io.IOException;
27+
import java.util.Map;
2728

2829
import org.junit.jupiter.api.Test;
2930
import org.mockito.Mockito;
@@ -164,6 +165,22 @@ void deserializeShouldBeAbleToRestoreFinalObjectAfterSerialization() {
164165
assertThat(serializer.deserialize(serializer.serialize(source))).isEqualTo(source);
165166
}
166167

168+
@Test // GH-2322
169+
void readsToMapForNonDefaultTyping() {
170+
171+
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(new ObjectMapper());
172+
173+
User user = new User();
174+
user.email = "[email protected]";
175+
user.id = 42;
176+
user.name = "Walter White";
177+
178+
byte[] serializedValue = serializer.serialize(user);
179+
180+
Object deserializedValue = serializer.deserialize(serializedValue, Object.class);
181+
assertThat(deserializedValue).isInstanceOf(Map.class);
182+
}
183+
167184
@Test // GH-2322
168185
void shouldConsiderWriter() {
169186

@@ -182,6 +199,24 @@ void shouldConsiderWriter() {
182199
assertThat(new String(result)).contains("id").contains("name").doesNotContain("email");
183200
}
184201

202+
@Test // GH-2322
203+
void considersWriterForCustomObjectMapper() {
204+
205+
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(new ObjectMapper(),
206+
JacksonObjectReader.create(), (mapper, source) -> {
207+
return mapper.writerWithView(Views.Basic.class).writeValueAsBytes(source);
208+
});
209+
210+
User user = new User();
211+
user.email = "[email protected]";
212+
user.id = 42;
213+
user.name = "Walter White";
214+
215+
byte[] serializedValue = serializer.serialize(user);
216+
217+
assertThat(new String(serializedValue)).contains("id").contains("name").doesNotContain("email");
218+
}
219+
185220
@Test // GH-2322
186221
void shouldConsiderReader() {
187222

@@ -309,10 +344,9 @@ static class User {
309344
}
310345

311346
static class Views {
347+
312348
interface Basic {}
313349

314350
interface Detailed {}
315-
316351
}
317-
318352
}

0 commit comments

Comments
 (0)