From eee70cd00862ee42c95ba5cc80331b513f714466 Mon Sep 17 00:00:00 2001 From: xzxiaoshan <365384722@qq.com> Date: Thu, 20 Feb 2025 08:35:48 +0800 Subject: [PATCH 1/5] =?UTF-8?q?parseArray=20=E6=96=B0=E5=A2=9E=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Filter=20=E5=8F=82=E6=95=B0=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parseArray 新增支持 Filter 参数重载,在转换 jsonArrayString 时,支持通过 Filter 设置增强对原来版本 autoType 的需求,如:JSONReader.autoTypeFilter(...) --- .../main/java/com/alibaba/fastjson2/JSON.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSON.java b/core/src/main/java/com/alibaba/fastjson2/JSON.java index 9d57d3f09f..0c8c67b333 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSON.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSON.java @@ -2863,6 +2863,65 @@ static List parseArray(String text, Class type, JSONReader.Feature... } } + /** + * Parses the json string as a list of {@link T}. Returns + * {@code null} if received {@link String} is {@code null} or empty. + * + * @param text the specified string to be parsed + * @param type the specified actual class of {@link T} + * @param filter the specified filter is applied to parsing + * @param features the specified features is applied to parsing + * @return {@link List} or {@code null} + * @throws JSONException If a parsing error occurs + */ + @SuppressWarnings("unchecked") + static List parseArray(String text, Class type, Filter filter, JSONReader.Feature... features) { + if (text == null || text.isEmpty()) { + return null; + } + + final JSONReader.Context context = JSONFactory.createReadContext(filter, features); + try (JSONReader reader = JSONReader.of(text, context)) { + List list = reader.readArray(type); + if (reader.resolveTasks != null) { + reader.handleResolveTasks(list); + } + if (reader.ch != EOI && (context.features & IgnoreCheckClose.mask) == 0) { + throw new JSONException(reader.info("input not end")); + } + return list; + } + } + + /** + * Parses the json string as a list of {@link T}. Returns + * {@code null} if received {@link String} is {@code null} or empty. + * + * @param text the specified string to be parsed + * @param type the specified actual class of {@link T} + * @param filter the specified filter is applied to parsing + * @param features the specified features is applied to parsing + * @return {@link List} or {@code null} + * @throws JSONException If a parsing error occurs + */ + @SuppressWarnings("unchecked") + static List parseArray(String text, Class type, Filter filter, JSONReader.Feature... features) { + if (text == null || text.isEmpty()) { + return null; + } + + final JSONReader.Context context = JSONFactory.createReadContext(filter, features); + try (JSONReader reader = JSONReader.of(text, context)) { + List list = reader.readArray(type); + if (reader.resolveTasks != null) { + reader.handleResolveTasks(list); + } + if (reader.ch != EOI && (context.features & IgnoreCheckClose.mask) == 0) { + throw new JSONException(reader.info("input not end")); + } + return list; + } + } /** * Parses the json char array as a list of {@link T}. Returns * {@code null} if received char array is {@code null} or empty. From 0d352e166a2069b810ead62ce3c475a644e8c695 Mon Sep 17 00:00:00 2001 From: xzxiaoshan <365384722@qq.com> Date: Mon, 5 Jan 2026 09:58:41 +0800 Subject: [PATCH 2/5] =?UTF-8?q?parseArray=20=E6=96=B0=E5=A2=9E=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Filter=20=E5=8F=82=E6=95=B0=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/alibaba/fastjson2/JSON.java | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSON.java b/core/src/main/java/com/alibaba/fastjson2/JSON.java index 0c8c67b333..c869a7a033 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSON.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSON.java @@ -2863,36 +2863,6 @@ static List parseArray(String text, Class type, JSONReader.Feature... } } - /** - * Parses the json string as a list of {@link T}. Returns - * {@code null} if received {@link String} is {@code null} or empty. - * - * @param text the specified string to be parsed - * @param type the specified actual class of {@link T} - * @param filter the specified filter is applied to parsing - * @param features the specified features is applied to parsing - * @return {@link List} or {@code null} - * @throws JSONException If a parsing error occurs - */ - @SuppressWarnings("unchecked") - static List parseArray(String text, Class type, Filter filter, JSONReader.Feature... features) { - if (text == null || text.isEmpty()) { - return null; - } - - final JSONReader.Context context = JSONFactory.createReadContext(filter, features); - try (JSONReader reader = JSONReader.of(text, context)) { - List list = reader.readArray(type); - if (reader.resolveTasks != null) { - reader.handleResolveTasks(list); - } - if (reader.ch != EOI && (context.features & IgnoreCheckClose.mask) == 0) { - throw new JSONException(reader.info("input not end")); - } - return list; - } - } - /** * Parses the json string as a list of {@link T}. Returns * {@code null} if received {@link String} is {@code null} or empty. From 2606838c0201b85b9c61952f73850118efedecfa Mon Sep 17 00:00:00 2001 From: xzxiaoshan <365384722@qq.com> Date: Sat, 1 Aug 2026 14:46:51 +0800 Subject: [PATCH 3/5] =?UTF-8?q?test:=20=E4=B8=BA=20parseArray(String,=20Cl?= =?UTF-8?q?ass,=20Filter,=20Feature...)=20=E9=87=8D=E8=BD=BD=E8=A1=A5?= =?UTF-8?q?=E5=85=85=20Filter=20=E4=BC=A0=E5=8F=82=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/fastjson2/JSONTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/core/src/test/java/com/alibaba/fastjson2/JSONTest.java b/core/src/test/java/com/alibaba/fastjson2/JSONTest.java index c761815c0e..ee92fc7765 100644 --- a/core/src/test/java/com/alibaba/fastjson2/JSONTest.java +++ b/core/src/test/java/com/alibaba/fastjson2/JSONTest.java @@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.*; @@ -883,6 +884,60 @@ public static class User { public String name; } + public static class AutoTypeBase { + public int id; + } + + public static class AutoTypeDerived + extends AutoTypeBase { + public String name; + } + + /** + * Verifies that {@link JSON#parseArray(String, Class, Filter, JSONReader.Feature...)} + * actually passes the {@link Filter} to {@code JSONFactory.createReadContext(filter, features)}. + * + *

If a future refactor accidentally drops the filter (e.g. calls + * {@code createReadContext(features)} instead), the AutoTypeBeforeHandler + * would be silently ignored — a security regression with no test to catch it.

+ */ + @Test + public void test_parseArray_withFilter() { + AutoTypeDerived derived = new AutoTypeDerived(); + derived.id = 101; + derived.name = "fastjson2"; + String jsonArray = "[" + JSON.toJSONString(derived, JSONWriter.Feature.WriteClassName) + "]"; + + // 1. Filter allows AutoTypeDerived → @type honoured, derived type returned. + // If the filter were silently dropped, @type would be ignored and + // AutoTypeBase would be returned instead. + Filter allowFilter = JSONReader.autoTypeFilter(AutoTypeDerived.class); + List allowed = JSON.parseArray(jsonArray, AutoTypeBase.class, allowFilter); + assertEquals(1, allowed.size()); + assertSame(AutoTypeDerived.class, allowed.get(0).getClass()); + assertEquals(101, allowed.get(0).id); + assertEquals("fastjson2", ((AutoTypeDerived) allowed.get(0)).name); + + // 2. Direct proof: a tracking handler records whether it was invoked. + // If the filter never reaches createReadContext, the flag stays false. + AtomicBoolean handlerInvoked = new AtomicBoolean(false); + JSONReader.AutoTypeBeforeHandler trackingHandler = (typeName, expectClass, f) -> { + handlerInvoked.set(true); + return null; + }; + JSON.parseArray(jsonArray, AutoTypeBase.class, trackingHandler); + assertTrue(handlerInvoked.get(), "AutoTypeBeforeHandler must be invoked by parseArray"); + + // 3. Without filter → @type ignored, declared type returned. + List noFilter = JSON.parseArray(jsonArray, AutoTypeBase.class); + assertEquals(1, noFilter.size()); + assertSame(AutoTypeBase.class, noFilter.get(0).getClass()); + + // 4. Edge cases: null / empty text → null. + assertNull(JSON.parseArray(null, AutoTypeBase.class, allowFilter)); + assertNull(JSON.parseArray("", AutoTypeBase.class, allowFilter)); + } + public static class MyList extends ArrayList { } From 82eb33bfe9ea71204c9fadc5ec711fad19bf5717 Mon Sep 17 00:00:00 2001 From: xzxiaoshan <365384722@qq.com> Date: Tue, 4 Aug 2026 08:44:47 +0800 Subject: [PATCH 4/5] Supplement JSON.parseArray test cases --- .../com/alibaba/fastjson2/codec/JSONTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/core/src/test/java/com/alibaba/fastjson2/codec/JSONTest.java b/core/src/test/java/com/alibaba/fastjson2/codec/JSONTest.java index 92c68e88f2..f833a489dd 100644 --- a/core/src/test/java/com/alibaba/fastjson2/codec/JSONTest.java +++ b/core/src/test/java/com/alibaba/fastjson2/codec/JSONTest.java @@ -946,6 +946,39 @@ public void test_parseArray_withFilter() { // 4. Edge cases: null / empty text → null. assertNull(JSON.parseArray(null, AutoTypeBase.class, allowFilter)); assertNull(JSON.parseArray("", AutoTypeBase.class, allowFilter)); + + // 5. Trailing content: pins the features varargs and the EOI guard together. + // The default (empty varargs) must reject trailing input with JSONException; + // passing IgnoreCheckClose through the varargs must suppress the guard. + // A refactor that dropped either `features` from createReadContext(filter, features) + // or the `reader.ch != EOI` branch would flip one of these assertions. + assertThrows(JSONException.class, + () -> JSON.parseArray(jsonArray + " trailing", AutoTypeBase.class, allowFilter)); + List lenient = JSON.parseArray(jsonArray + " trailing", + AutoTypeBase.class, allowFilter, JSONReader.Feature.IgnoreCheckClose); + assertEquals(1, lenient.size()); + assertSame(AutoTypeDerived.class, lenient.get(0).getClass()); + assertEquals(101, lenient.get(0).id); + } + + /** + * Verifies that {@link JSON#parseArray(String, Class, Filter, JSONReader.Feature...)} + * resolves {@code $ref} references through {@code handleResolveTasks}. + * + *

The {@code reader.resolveTasks != null} branch in + * {@code JSON.parseArray(String, Class, Filter, Feature...)} is otherwise never + * reached: {@link #test_parseArray_withFilter()} only feeds {@code @type}-based + * input that produces no reference tasks.

+ */ + @Test + public void test_parseArray_withFilter_ref() { + Filter allowFilter = JSONReader.autoTypeFilter(AutoTypeDerived.class); + + List refs = JSON.parseArray( + "[{\"id\":1},{\"$ref\":\"$[0]\"}]", AutoTypeBase.class, allowFilter); + assertEquals(2, refs.size()); + assertEquals(1, refs.get(0).id); + assertSame(refs.get(0), refs.get(1)); } public static class MyList From af6701d6cdb869652fa446ca204b7383244e32f4 Mon Sep 17 00:00:00 2001 From: xzxiaoshan Date: Sat, 31 Oct 2026 15:32:38 +0800 Subject: [PATCH 5/5] docs: note literal-null ambiguity in parseArray Filter overload --- core/src/main/java/com/alibaba/fastjson2/JSON.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSON.java b/core/src/main/java/com/alibaba/fastjson2/JSON.java index f6f199dfb3..b27f785d0b 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSON.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSON.java @@ -3014,6 +3014,11 @@ static List parseArray(String text, Class type, JSONReader.Feature... * Parses the json string as a list of {@link T}. Returns * {@code null} if received {@link String} is {@code null} or empty. * + *

Note: a literal {@code null} filter followed by at least one feature + * (e.g. {@code parseArray(text, type, null, feature)}) is a compile-time + * ambiguity with {@code parseArray(String, Class, JSONReader.Feature...)}; + * cast the argument to {@code (Filter) null} if that call shape is needed. + * * @param text the specified string to be parsed * @param type the specified actual class of {@link T} * @param filter the specified filter is applied to parsing