Skip to content
29 changes: 29 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -3010,6 +3010,35 @@ static <T> List<T> parseArray(String text, Class<T> 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 <T> List<T> parseArray(String text, Class<T> type, Filter filter, JSONReader.Feature... features) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The new varargs overload introduces a source incompatibility: parseArray(String, Class, null, Feature...) (literal null third argument followed by at least one feature) becomes a compile-time ambiguity.

Failure scenario: A/B-verified — JSON.parseArray("[]", Bean.class, null, JSONReader.Feature.SupportAutoType) compiles against released 2.0.64 (binding to parseArray(String, Class, Feature...)) but fails against this PR with reference to parseArray is ambiguous. Two mitigating facts: (1) that call shape always NPE'd at runtime on 2.0.64 (createReadContext dereferences the null feature element), so no working downstream code breaks — this converts a runtime-NPE shape into a compile error; (2) parseObject already carries an identical literal-null ambiguity in released 2.0.64 ((String,Type,Filter,Feature...) vs (String,Type,String,Feature...)), so this matches existing API precedent. The 3-arg form parseArray(text, Class, null) is unaffected (verified: still binds the old overload).

Suggested fix: the ambiguity is inherent to adjacent varargs overloads of this shape — no code change required; add a release-note entry for the next version so affected callers disambiguate with a (Filter) null cast.

中文说明

[建议] 新增的 varargs 重载引入了一处源码不兼容:parseArray(String, Class, null, Feature...)(第三参为字面量 null 且至少跟一个 feature)成为编译期歧义。

失败场景: 经 A/B 实测——JSON.parseArray("[]", Bean.class, null, JSONReader.Feature.SupportAutoType) 在已发布的 2.0.64 上可编译(绑定到 parseArray(String, Class, Feature...)),但在本 PR 上编译失败,报 reference to parseArray is ambiguous。两点减轻情节:(1) 该调用形状在 2.0.64 上运行时本就会 NPE(createReadContext 对 null feature 元素解引用),因此不会有正常工作的下游代码被破坏——这实际上把一个运行时 NPE 形状转变为编译期错误;(2) parseObject 在已发布的 2.0.64 中已有完全相同的字面量 null 歧义先例((String,Type,Filter,Feature...) 与 (String,Type,String,Feature...)),故与现有 API 先例一致。三参形式 parseArray(text, Class, null) 不受影响(已验证:仍绑定旧重载)。

建议修复: 该歧义由相邻同形 varargs 重载固有导致,无需代码改动;建议在下一版本的 release notes 中记录,受影响的调用方可用 (Filter) null 强制转型消除歧义。

— qwen3.8-max via Qwen Code /review (v0.21.4)

if (text == null || text.isEmpty()) {
return null;
}
Comment on lines +3030 to +3033

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] 新增的 parseArray(String, Class<T>, Filter, JSONReader.Feature...) 重载缺少测试覆盖。Filter 参数传递到 JSONFactory.createReadContext(filter, features) 是该方法与已有重载唯一的行为差异,但目前没有任何测试验证此路径。— 失败场景:如果未来重构意外将 createReadContext(filter, features) 改为 createReadContext(features)(丢弃 filter 参数),Filter 将被静默忽略,用户依赖 JSON.parseArray(json, Bean.class, JSONReader.autoTypeFilter(Bean.class), JSONReader.Feature.SupportAutoType) 进行 autoType 限制时安全过滤器将不生效,且无测试能捕获此回归。

建议添加测试:传入 AutoTypeBeforeHandler filter 并断言其被正确应用(例如验证被阻止的类型确实被拒绝)。

中文说明

[建议] 新增的 parseArray(String, Class<T>, Filter, JSONReader.Feature...) 重载缺少测试覆盖。Filter 参数传递到 JSONFactory.createReadContext(filter, features) 是该方法与已有重载唯一的行为差异,但目前没有任何测试验证此路径。

失败场景: 如果未来重构意外将 createReadContext(filter, features) 改为 createReadContext(features)(丢弃 filter 参数),Filter 将被静默忽略,用户依赖 autoType 过滤器的安全限制将不生效,且无测试能捕获此回归。

建议修复: 添加测试用例,传入 AutoTypeBeforeHandler filter 并断言其被正确应用。

— qwen3.8-max-preview via Qwen Code /review


final JSONReader.Context context = JSONFactory.createReadContext(filter, features);
try (JSONReader reader = JSONReader.of(text, context)) {
List<T> list = reader.readArray(type);
if (reader.resolveTasks != null) {
reader.handleResolveTasks(list);
}
Comment on lines +3038 to +3040

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This $ref resolution branch (handleResolveTasks(list)) is never exercised through the new overload — the only test of this overload (test_parseArray_withFilter) uses @type content only, and no other caller exists. One of 3 untested paths of the new overload.

Failure scenario: mutation-verified — removing only the handleResolveTasks(list) call leaves input [{"id":1},{"$ref":"$[0]"}] returning elem1 == null instead of the referenced instance, and the whole suite stays green.

Suggested fix: add a $ref case through the new overload:

List<AutoTypeBase> refs = JSON.parseArray(
        "[{\"id\":1},{\"$ref\":\"$[0]\"}]", AutoTypeBase.class, allowFilter);
assertSame(refs.get(0), refs.get(1));
中文说明

[建议] 该 $ref 解析分支(handleResolveTasks(list))从未通过新重载被测试覆盖——新重载唯一的测试 test_parseArray_withFilter 仅使用 @type 内容,且无其他调用方。这是新重载 3 条未覆盖路径之一。

失败场景: 已实际运行突变体验证——仅删除 handleResolveTasks(list) 调用后,输入 [{"id":1},{"$ref":"$[0]"}] 的第二元素返回 null 而非被引用实例,且整个套件全绿。

建议修复: 补充一个经由新重载的 $ref 用例,代码见上方英文代码块。

— qwen3.8-max via Qwen Code /review (v0.21.4)

if (reader.ch != EOI && (context.features & IgnoreCheckClose.mask) == 0) {
throw new JSONException(reader.info("input not end"));
}
Comment on lines +3041 to +3043

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This trailing-input guard (reader.ch != EOI && IgnoreCheckClose) is never exercised through the new overload — all four call sites in test_parseArray_withFilter pass null/empty or fully-consumed input, so deleting or inverting this check changes zero test outcomes. One of 3 untested paths of the new overload.

Failure scenario: if the guard were dropped, JSON.parseArray("[{\"id\":101}] trailing garbage", AutoTypeBase.class, filter) would silently return the list instead of throwing the JSONException promised by the javadoc and enforced by every sibling overload.

Suggested fix: add a trailing-content case (kills this mutant and the features-varargs mutant together):

assertThrows(JSONException.class,
        () -> JSON.parseArray(jsonArray + " trailing", AutoTypeBase.class, allowFilter));
List<AutoTypeBase> lenient = JSON.parseArray(jsonArray + " trailing",
        AutoTypeBase.class, allowFilter, JSONReader.Feature.IgnoreCheckClose);
assertEquals(1, lenient.size());
中文说明

[建议] 该尾随输入检查(reader.ch != EOI && IgnoreCheckClose)从未通过新重载被测试覆盖——test_parseArray_withFilter 的四个调用点均传 null/空串或恰好在末尾结束的输入,因此删除或反转该检查不会改变任何测试结果。这是新重载 3 条未覆盖路径之一。

失败场景: 若该检查被删除,JSON.parseArray("[{\"id\":101}] trailing garbage", AutoTypeBase.class, filter) 将静默返回列表,而非抛出 javadoc 承诺且所有兄弟重载均强制执行的 JSONException。

建议修复: 补充一个尾随内容用例(同时杀死本突变体与 features 变参突变体),代码见上方英文代码块。

— qwen3.8-max via Qwen Code /review (v0.21.4)

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.
Expand Down
55 changes: 55 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/codec/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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.*;
Expand Down Expand Up @@ -893,6 +894,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)}.
*
* <p>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.</p>
*/
@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<AutoTypeBase> allowed = JSON.parseArray(jsonArray, AutoTypeBase.class, allowFilter);
Comment on lines +924 to +925

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The features varargs of the new parseArray(String, Class<T>, Filter, JSONReader.Feature...) overload is never exercised — all four call sites in this test pass empty varargs, so a mutation dropping features ships green. This is one of 3 untested paths of the new overload (the others: the $ref branch at JSON.java:3033 and the trailing-input guard at JSON.java:3036).

Failure scenario: mutation-verified — replacing createReadContext(filter, features) with createReadContext(filter) keeps the whole suite green; a probe passing IgnoreCheckClose flips it.

Suggested fix: add the two cases below (kills this mutant and the EOI-guard mutant together):

// trailing content rejected; IgnoreCheckClose suppresses it —
// pins the features varargs and the EOI branch together
List<AutoTypeBase> lenient = JSON.parseArray(jsonArray + " trailing",
        AutoTypeBase.class, allowFilter, JSONReader.Feature.IgnoreCheckClose);
assertEquals(1, lenient.size());
assertThrows(JSONException.class,
        () -> JSON.parseArray(jsonArray + " trailing", AutoTypeBase.class, allowFilter));
中文说明

[建议] 新重载 parseArray(String, Class<T>, Filter, JSONReader.Feature...) 的 features 变参从未被测试覆盖——本测试的四个调用点均传空 varargs,因此丢弃 features 的突变可以静默通过。这是新重载 3 条未覆盖路径之一(另两条:JSON.java:3033 的 $ref 分支、JSON.java:3036 的尾随输入检查)。

失败场景: 已实际运行突变体验证——将 createReadContext(filter, features) 改为 createReadContext(filter) 后整个套件仍全绿;传入 IgnoreCheckClose 的探针会使其翻转。

建议修复: 补充下方两个用例(同时杀死本突变体与 EOI 检查突变体),代码见上方英文代码块。

— qwen3.8-max via Qwen Code /review (v0.21.4)

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<AutoTypeBase> 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<T>
extends ArrayList<T> {
}
Expand Down
Loading