|
24 | 24 | import lombok.ToString;
|
25 | 25 |
|
26 | 26 | import java.io.IOException;
|
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
27 | 28 | import java.util.Map;
|
28 | 29 |
|
29 | 30 | import org.junit.jupiter.api.Test;
|
@@ -171,7 +172,7 @@ void deserializeShouldBeAbleToRestoreFinalObjectAfterSerialization() {
|
171 | 172 | }
|
172 | 173 |
|
173 | 174 | @Test // GH-2361
|
174 |
| - void shouldDeserializeArrayWithoutTypeHint() { |
| 175 | + void shouldDeserializePrimitiveArrayWithoutTypeHint() { |
175 | 176 |
|
176 | 177 | GenericJackson2JsonRedisSerializer gs = new GenericJackson2JsonRedisSerializer();
|
177 | 178 | CountAndArray result = (CountAndArray) gs.deserialize(
|
@@ -262,6 +263,87 @@ void shouldConsiderReader() {
|
262 | 263 | });
|
263 | 264 | }
|
264 | 265 |
|
| 266 | + @Test // GH-2361 |
| 267 | + void shouldDeserializePrimitiveWrapperArrayWithoutTypeHint() { |
| 268 | + |
| 269 | + GenericJackson2JsonRedisSerializer gs = new GenericJackson2JsonRedisSerializer(); |
| 270 | + CountAndArray result = (CountAndArray) gs.deserialize( |
| 271 | + ("{\"@class\":\"org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$CountAndArray\", \"count\":1, \"arrayOfPrimitiveWrapper\":[0,1]}") |
| 272 | + .getBytes()); |
| 273 | + |
| 274 | + assertThat(result.getCount()).isEqualTo(1); |
| 275 | + assertThat(result.getArrayOfPrimitiveWrapper()).containsExactly(0L, 1L); |
| 276 | + } |
| 277 | + |
| 278 | + @Test // GH-2361 |
| 279 | + void doesNotIncludeTypingForPrimitiveArrayWrappers() { |
| 280 | + |
| 281 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 282 | + |
| 283 | + WithWrapperTypes source = new WithWrapperTypes(); |
| 284 | + source.primitiveWrapper = new AtomicReference<>(); |
| 285 | + source.primitiveArrayWrapper = new AtomicReference<>(new Integer[] { 200, 300 }); |
| 286 | + source.simpleObjectWrapper = new AtomicReference<>(); |
| 287 | + |
| 288 | + byte[] serializedValue = serializer.serialize(source); |
| 289 | + |
| 290 | + assertThat(new String(serializedValue)) // |
| 291 | + .contains("\"primitiveArrayWrapper\":[200,300]") // |
| 292 | + .doesNotContain("\"[Ljava.lang.Integer;\""); |
| 293 | + |
| 294 | + assertThat(serializer.deserialize(serializedValue)) // |
| 295 | + .isInstanceOf(WithWrapperTypes.class) // |
| 296 | + .satisfies(it -> { |
| 297 | + WithWrapperTypes deserialized = (WithWrapperTypes) it; |
| 298 | + assertThat(deserialized.primitiveArrayWrapper).hasValue(source.primitiveArrayWrapper.get()); |
| 299 | + }); |
| 300 | + } |
| 301 | + |
| 302 | + @Test // GH-2361 |
| 303 | + void doesNotIncludeTypingForPrimitiveWrappers() { |
| 304 | + |
| 305 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 306 | + |
| 307 | + WithWrapperTypes source = new WithWrapperTypes(); |
| 308 | + source.primitiveWrapper = new AtomicReference<>(123L); |
| 309 | + |
| 310 | + byte[] serializedValue = serializer.serialize(source); |
| 311 | + |
| 312 | + assertThat(new String(serializedValue)) // |
| 313 | + .contains("\"primitiveWrapper\":123") // |
| 314 | + .doesNotContain("\"Ljava.lang.Long;\""); |
| 315 | + |
| 316 | + assertThat(serializer.deserialize(serializedValue)) // |
| 317 | + .isInstanceOf(WithWrapperTypes.class) // |
| 318 | + .satisfies(it -> { |
| 319 | + WithWrapperTypes deserialized = (WithWrapperTypes) it; |
| 320 | + assertThat(deserialized.primitiveWrapper).hasValue(source.primitiveWrapper.get()); |
| 321 | + }); |
| 322 | + } |
| 323 | + |
| 324 | + @Test // GH-2361 |
| 325 | + void includesTypingForWrappedObjectTypes() { |
| 326 | + |
| 327 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 328 | + |
| 329 | + SimpleObject simpleObject = new SimpleObject(100L); |
| 330 | + WithWrapperTypes source = new WithWrapperTypes(); |
| 331 | + source.simpleObjectWrapper = new AtomicReference<>(simpleObject); |
| 332 | + |
| 333 | + byte[] serializedValue = serializer.serialize(source); |
| 334 | + |
| 335 | + assertThat(new String(serializedValue)) // |
| 336 | + .contains( |
| 337 | + "\"simpleObjectWrapper\":{\"@class\":\"org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$SimpleObject\",\"longValue\":100}"); |
| 338 | + |
| 339 | + assertThat(serializer.deserialize(serializedValue)) // |
| 340 | + .isInstanceOf(WithWrapperTypes.class) // |
| 341 | + .satisfies(it -> { |
| 342 | + WithWrapperTypes deserialized = (WithWrapperTypes) it; |
| 343 | + assertThat(deserialized.simpleObjectWrapper).hasValue(source.simpleObjectWrapper.get()); |
| 344 | + }); |
| 345 | + } |
| 346 | + |
265 | 347 | private static void serializeAndDeserializeNullValue(GenericJackson2JsonRedisSerializer serializer) {
|
266 | 348 |
|
267 | 349 | NullValue nv = BeanUtils.instantiateClass(NullValue.class);
|
@@ -311,7 +393,6 @@ public boolean equals(Object obj) {
|
311 | 393 | return nullSafeEquals(this.stringValue, other.stringValue)
|
312 | 394 | && nullSafeEquals(this.simpleObject, other.simpleObject);
|
313 | 395 | }
|
314 |
| - |
315 | 396 | }
|
316 | 397 |
|
317 | 398 | @Data
|
@@ -373,5 +454,14 @@ static class CountAndArray {
|
373 | 454 |
|
374 | 455 | private int count;
|
375 | 456 | private int[] available;
|
| 457 | + private Long[] arrayOfPrimitiveWrapper; |
| 458 | + } |
| 459 | + |
| 460 | + @Data |
| 461 | + static class WithWrapperTypes { |
| 462 | + |
| 463 | + AtomicReference<Long> primitiveWrapper; |
| 464 | + AtomicReference<Integer[]> primitiveArrayWrapper; |
| 465 | + AtomicReference<SimpleObject> simpleObjectWrapper; |
376 | 466 | }
|
377 | 467 | }
|
0 commit comments