Skip to content

Commit fb5747a

Browse files
Use Jackson default factory to create JavaType.
1 parent 5fdce84 commit fb5747a

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public T deserialize(byte @Nullable [] bytes) throws SerializationException {
164164
* @return the java type
165165
*/
166166
protected JavaType getJavaType(Class<?> clazz) {
167-
return TypeFactory.unsafeSimpleType(clazz);
167+
return TypeFactory.createDefaultInstance().constructType(clazz);
168168
}
169169

170170
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2014-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.serializer;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20+
21+
import tools.jackson.databind.json.JsonMapper;
22+
import tools.jackson.databind.type.TypeFactory;
23+
24+
import java.io.Serializable;
25+
import java.util.Arrays;
26+
import java.util.Objects;
27+
import java.util.UUID;
28+
29+
import org.junit.jupiter.api.Test;
30+
import org.springframework.data.redis.Person;
31+
import org.springframework.data.redis.PersonObjectFactory;
32+
33+
/**
34+
* Unit tests for {@link JacksonJsonRedisSerializer}.
35+
*
36+
* @author Thomas Darimont
37+
* @author Christoph Strobl
38+
* @author Mark Paluch
39+
*/
40+
class JacksonJsonRedisSerializerTests {
41+
42+
private JacksonJsonRedisSerializer<Person> serializer = new JacksonJsonRedisSerializer<>(Person.class);
43+
44+
@Test // GH-3271
45+
void canDeserializeSerialized() {
46+
47+
JacksonJsonRedisSerializer<SessionToken> redisSerializer = new JacksonJsonRedisSerializer<>(SessionToken.class);
48+
49+
SessionToken source = new SessionToken(UUID.randomUUID().toString());
50+
51+
byte[] serialized = redisSerializer.serialize(source);
52+
assertThat(redisSerializer.deserialize(serialized)).isEqualTo(source);
53+
}
54+
55+
@Test // DATAREDIS-241
56+
void testJacksonJsonSerializerShouldReturnEmptyByteArrayWhenSerializingNull() {
57+
assertThat(serializer.serialize(null)).isEqualTo(new byte[0]);
58+
}
59+
60+
@Test // DTATREDIS-241
61+
void testJacksonJsonSerializerShouldReturnNullWhenDerserializingEmptyByteArray() {
62+
assertThat(serializer.deserialize(new byte[0])).isNull();
63+
}
64+
65+
@Test // DTATREDIS-241
66+
void testJacksonJsonSerializerShouldThrowExceptionWhenDeserializingInvalidByteArray() {
67+
68+
Person person = new PersonObjectFactory().instance();
69+
byte[] serializedValue = serializer.serialize(person);
70+
Arrays.sort(serializedValue); // corrupt serialization result
71+
72+
assertThatExceptionOfType(SerializationException.class).isThrownBy(() -> serializer.deserialize(serializedValue));
73+
}
74+
75+
@Test // GH-2322
76+
void shouldConsiderWriter() {
77+
78+
serializer = new JacksonJsonRedisSerializer<>(new JsonMapper(),
79+
TypeFactory.createDefaultInstance().constructType(Person.class), JacksonObjectReader.create(),
80+
(mapper, source) -> "foo".getBytes());
81+
Person person = new PersonObjectFactory().instance();
82+
assertThat(serializer.serialize(person)).isEqualTo("foo".getBytes());
83+
}
84+
85+
static class SessionToken implements Serializable {
86+
87+
private String userUuid;
88+
89+
private SessionToken() {
90+
// why jackson?
91+
}
92+
93+
public SessionToken(String userUuid) {
94+
this.userUuid = userUuid;
95+
}
96+
97+
public String getUserUuid() {
98+
return userUuid;
99+
}
100+
101+
public void setUserUuid(String userUuid) {
102+
this.userUuid = userUuid;
103+
}
104+
105+
@Override
106+
public boolean equals(Object o) {
107+
if (o == this) {
108+
return true;
109+
}
110+
if (o == null || getClass() != o.getClass()) {
111+
return false;
112+
}
113+
SessionToken token = (SessionToken) o;
114+
return Objects.equals(userUuid, token.userUuid);
115+
}
116+
117+
@Override
118+
public int hashCode() {
119+
return Objects.hash(userUuid);
120+
}
121+
122+
@Override
123+
public String toString() {
124+
return "SessionToken{" + "userUuid='" + userUuid + '\'' + '}';
125+
}
126+
127+
}
128+
129+
}

0 commit comments

Comments
 (0)