|
| 1 | +package com.alibaba.fastjson2.issues; |
| 2 | + |
| 3 | +import com.alibaba.fastjson2.JSON; |
| 4 | +import com.alibaba.fastjson2.JSONReader; |
| 5 | +import com.alibaba.fastjson2.TypeReference; |
| 6 | +import com.alibaba.fastjson2.annotation.JSONType; |
| 7 | +import com.alibaba.fastjson2.reader.ObjectReader; |
| 8 | +import org.junit.jupiter.api.Tag; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.lang.reflect.Type; |
| 12 | +import java.util.AbstractList; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 18 | + |
| 19 | +/** |
| 20 | + * TypeReference with a custom List implementation (not derived from ArrayList) |
| 21 | + * lost its generic element type — elements were decoded as JSONObject. |
| 22 | + */ |
| 23 | +@Tag("regression") |
| 24 | +public class Issue7684 { |
| 25 | + public static class CustomList<E> extends AbstractList<E> { |
| 26 | + private final List<E> list = new ArrayList<>(); |
| 27 | + |
| 28 | + @Override |
| 29 | + public E get(int index) { |
| 30 | + return list.get(index); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public int size() { |
| 35 | + return list.size(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void add(int index, E element) { |
| 40 | + list.add(index, element); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public E set(int index, E element) { |
| 45 | + return list.set(index, element); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public E remove(int index) { |
| 50 | + return list.remove(index); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static class Bean { |
| 55 | + public int id; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * A non-{@code ArrayList} {@code List} subclass that declares a custom deserializer. |
| 60 | + * Used to verify the deserializer is honored (not bypassed by the list fast-path) |
| 61 | + * when the type is used as a parameterized {@link TypeReference} argument. |
| 62 | + */ |
| 63 | + @JSONType(deserializer = DeserializerListReader.class) |
| 64 | + public static class DeserializerList<E> extends AbstractList<E> { |
| 65 | + private final List<E> list = new ArrayList<>(); |
| 66 | + |
| 67 | + @Override |
| 68 | + public E get(int index) { |
| 69 | + return list.get(index); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public int size() { |
| 74 | + return list.size(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void add(int index, E element) { |
| 79 | + list.add(index, element); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static class DeserializerListReader implements ObjectReader { |
| 84 | + @Override |
| 85 | + public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { |
| 86 | + jsonReader.readAny(); // consume the JSON payload |
| 87 | + // Return a recognizable sentinel so the test can prove this reader ran |
| 88 | + // (rather than the generic list reader that would bypass it). |
| 89 | + DeserializerList list = new DeserializerList(); |
| 90 | + list.add("INVOKED"); |
| 91 | + return list; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void customListGenericPreserved() { |
| 97 | + String json = "[{\"id\":1},{\"id\":2}]"; |
| 98 | + CustomList<Bean> result = JSON.parseObject(json, new TypeReference<CustomList<Bean>>() {}); |
| 99 | + |
| 100 | + assertEquals(2, result.size()); |
| 101 | + assertTrue(result.get(0) instanceof Bean, "element type must be Bean, was: " + result.get(0).getClass()); |
| 102 | + assertEquals(1, result.get(0).id); |
| 103 | + assertEquals(2, result.get(1).id); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void customDeserializerOnListSubclassHonored() { |
| 108 | + // Regression: @JSONType(deserializer=...) on a non-ArrayList List subclass used as a |
| 109 | + // TypeReference argument must be honored, not silently bypassed by the list fast-path. |
| 110 | + DeserializerList<String> result = JSON.parseObject( |
| 111 | + "[\"a\",\"b\"]", new TypeReference<DeserializerList<String>>() {}); |
| 112 | + |
| 113 | + assertEquals(1, result.size()); |
| 114 | + assertEquals("INVOKED", result.get(0)); |
| 115 | + } |
| 116 | +} |
0 commit comments