|
| 1 | +package com.fasterxml.jackson.databind.deser; |
| 2 | + |
| 3 | +import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import com.fasterxml.jackson.databind.BeanDescription; |
| 6 | +import com.fasterxml.jackson.databind.DeserializationConfig; |
| 7 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 10 | +import com.fasterxml.jackson.databind.type.ArrayType; |
| 11 | +import java.util.concurrent.atomic.AtomicInteger; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +/** |
| 15 | + * Unit test for [databind#4216] : Primitive array deserializer cannot being captured by DeserializerModifier |
| 16 | + */ |
| 17 | +public class BeanDeserializerModifier4216Test |
| 18 | +{ |
| 19 | + |
| 20 | + static class WrapperBean4216 { |
| 21 | + public Byte[] objArr; |
| 22 | + public byte[] primArr; |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testModifierCalledTwice() throws Exception |
| 27 | + { |
| 28 | + // Given : Configure and construct |
| 29 | + AtomicInteger counter = new AtomicInteger(0); |
| 30 | + ObjectMapper objectMapper = jsonMapperBuilder() |
| 31 | + .addModules(getSimpleModuleWithCounter(counter)) |
| 32 | + .build(); |
| 33 | + |
| 34 | + // Given : Set-up data |
| 35 | + WrapperBean4216 test = new WrapperBean4216(); |
| 36 | + test.primArr = new byte[]{(byte) 0x11}; |
| 37 | + test.objArr = new Byte[]{(byte) 0x11}; |
| 38 | + String sample = objectMapper.writeValueAsString(test); |
| 39 | + |
| 40 | + // When |
| 41 | + objectMapper.readValue(sample, WrapperBean4216.class); |
| 42 | + |
| 43 | + // Then : modifyArrayDeserializer should be called twice |
| 44 | + assertEquals(2, counter.get()); |
| 45 | + } |
| 46 | + |
| 47 | + private static SimpleModule getSimpleModuleWithCounter(AtomicInteger counter) { |
| 48 | + SimpleModule module = new SimpleModule(); |
| 49 | + module.setDeserializerModifier( |
| 50 | + new BeanDeserializerModifier() { |
| 51 | + @Override |
| 52 | + public JsonDeserializer<?> modifyArrayDeserializer(DeserializationConfig config, |
| 53 | + ArrayType valueType, BeanDescription beanDesc, JsonDeserializer<?> deserializer) |
| 54 | + { |
| 55 | + // Count invocations |
| 56 | + counter.incrementAndGet(); |
| 57 | + return deserializer; |
| 58 | + } |
| 59 | + }); |
| 60 | + return module; |
| 61 | + } |
| 62 | +} |
0 commit comments