23
23
import lombok .Data ;
24
24
25
25
import java .io .IOException ;
26
+ import java .util .concurrent .atomic .AtomicReference ;
26
27
27
28
import org .junit .jupiter .api .Test ;
28
29
import org .mockito .Mockito ;
@@ -168,7 +169,7 @@ void deserializeShouldBeAbleToRestoreFinalObjectAfterSerialization() {
168
169
}
169
170
170
171
@ Test // GH-2361
171
- void shouldDeserializeArrayWithoutTypeHint () {
172
+ void shouldDeserializePrimitiveArrayWithoutTypeHint () {
172
173
173
174
GenericJackson2JsonRedisSerializer gs = new GenericJackson2JsonRedisSerializer ();
174
175
CountAndArray result = (CountAndArray ) gs .deserialize (
@@ -179,6 +180,87 @@ void shouldDeserializeArrayWithoutTypeHint() {
179
180
assertThat (result .getAvailable ()).containsExactly (0 , 1 );
180
181
}
181
182
183
+ @ Test // GH-2361
184
+ void shouldDeserializePrimitiveWrapperArrayWithoutTypeHint () {
185
+
186
+ GenericJackson2JsonRedisSerializer gs = new GenericJackson2JsonRedisSerializer ();
187
+ CountAndArray result = (CountAndArray ) gs .deserialize (
188
+ ("{\" @class\" :\" org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$CountAndArray\" , \" count\" :1, \" arrayOfPrimitiveWrapper\" :[0,1]}" )
189
+ .getBytes ());
190
+
191
+ assertThat (result .getCount ()).isEqualTo (1 );
192
+ assertThat (result .getArrayOfPrimitiveWrapper ()).containsExactly (0L , 1L );
193
+ }
194
+
195
+ @ Test // GH-2361
196
+ void doesNotIncludeTypingForPrimitiveArrayWrappers () {
197
+
198
+ GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer ();
199
+
200
+ WithWrapperTypes source = new WithWrapperTypes ();
201
+ source .primitiveWrapper = new AtomicReference <>();
202
+ source .primitiveArrayWrapper = new AtomicReference <>(new Integer [] { 200 , 300 });
203
+ source .simpleObjectWrapper = new AtomicReference <>();
204
+
205
+ byte [] serializedValue = serializer .serialize (source );
206
+
207
+ assertThat (new String (serializedValue )) //
208
+ .contains ("\" primitiveArrayWrapper\" :[200,300]" ) //
209
+ .doesNotContain ("\" [Ljava.lang.Integer;\" " );
210
+
211
+ assertThat (serializer .deserialize (serializedValue )) //
212
+ .isInstanceOf (WithWrapperTypes .class ) //
213
+ .satisfies (it -> {
214
+ WithWrapperTypes deserialized = (WithWrapperTypes ) it ;
215
+ assertThat (deserialized .primitiveArrayWrapper ).hasValue (source .primitiveArrayWrapper .get ());
216
+ });
217
+ }
218
+
219
+ @ Test // GH-2361
220
+ void doesNotIncludeTypingForPrimitiveWrappers () {
221
+
222
+ GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer ();
223
+
224
+ WithWrapperTypes source = new WithWrapperTypes ();
225
+ source .primitiveWrapper = new AtomicReference <>(123L );
226
+
227
+ byte [] serializedValue = serializer .serialize (source );
228
+
229
+ assertThat (new String (serializedValue )) //
230
+ .contains ("\" primitiveWrapper\" :123" ) //
231
+ .doesNotContain ("\" Ljava.lang.Long;\" " );
232
+
233
+ assertThat (serializer .deserialize (serializedValue )) //
234
+ .isInstanceOf (WithWrapperTypes .class ) //
235
+ .satisfies (it -> {
236
+ WithWrapperTypes deserialized = (WithWrapperTypes ) it ;
237
+ assertThat (deserialized .primitiveWrapper ).hasValue (source .primitiveWrapper .get ());
238
+ });
239
+ }
240
+
241
+ @ Test // GH-2361
242
+ void includesTypingForWrappedObjectTypes () {
243
+
244
+ GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer ();
245
+
246
+ SimpleObject simpleObject = new SimpleObject (100L );
247
+ WithWrapperTypes source = new WithWrapperTypes ();
248
+ source .simpleObjectWrapper = new AtomicReference <>(simpleObject );
249
+
250
+ byte [] serializedValue = serializer .serialize (source );
251
+
252
+ assertThat (new String (serializedValue )) //
253
+ .contains (
254
+ "\" simpleObjectWrapper\" :{\" @class\" :\" org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$SimpleObject\" ,\" longValue\" :100}" );
255
+
256
+ assertThat (serializer .deserialize (serializedValue )) //
257
+ .isInstanceOf (WithWrapperTypes .class ) //
258
+ .satisfies (it -> {
259
+ WithWrapperTypes deserialized = (WithWrapperTypes ) it ;
260
+ assertThat (deserialized .simpleObjectWrapper ).hasValue (source .simpleObjectWrapper .get ());
261
+ });
262
+ }
263
+
182
264
private static void serializeAndDeserializeNullValue (GenericJackson2JsonRedisSerializer serializer ) {
183
265
184
266
NullValue nv = BeanUtils .instantiateClass (NullValue .class );
@@ -228,7 +310,6 @@ public boolean equals(Object obj) {
228
310
return nullSafeEquals (this .stringValue , other .stringValue )
229
311
&& nullSafeEquals (this .simpleObject , other .simpleObject );
230
312
}
231
-
232
313
}
233
314
234
315
@ Data
@@ -275,6 +356,14 @@ static class CountAndArray {
275
356
276
357
private int count ;
277
358
private int [] available ;
359
+ private Long [] arrayOfPrimitiveWrapper ;
278
360
}
279
361
362
+ @ Data
363
+ static class WithWrapperTypes {
364
+
365
+ AtomicReference <Long > primitiveWrapper ;
366
+ AtomicReference <Integer []> primitiveArrayWrapper ;
367
+ AtomicReference <SimpleObject > simpleObjectWrapper ;
368
+ }
280
369
}
0 commit comments