Repository navigation
parseArray 新增支持 Filter 参数的方法重载 - #3343
xzxiaoshan wants to merge 8 commits into
Conversation
parseArray 新增支持 Filter 参数重载,在转换 jsonArrayString 时,支持通过 Filter 设置增强对原来版本 autoType 的需求,如:JSONReader.autoTypeFilter(...)
|
作者得空方便的时候,处理一下这个 PR。谢谢! @wenshao |
|
@wenshao 麻烦您有时间审核下这个 |
wenshao
left a comment
There was a problem hiding this comment.
Reviewed — no blockers. Suggestions are inline.
中文说明
已审查——无阻断问题。 建议见行内评论。
— qwen3.8-max-preview via Qwen Code /review
| static <T> List<T> parseArray(String text, Class<T> type, Filter filter, JSONReader.Feature... features) { | ||
| if (text == null || text.isEmpty()) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
[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
|
@jujn 嚯,你也在管理 fastjson2,我在 agentscope-java 中的 PR 我看也是你在回复。太巧了,在这里跨项目打个招呼。^_^ |
哈哈,最近太忙了,也没时间来 fastjson2 这边帮忙了。对了你的那个agui PR需要讨论后再合入,因为涉及比较核心的agent上下文状态。 |
嗯,你们辛苦。 @jujn |
wenshao
left a comment
There was a problem hiding this comment.
Reviewed — no blockers. Suggestions are inline.
中文说明
已审查——无阻断问题。 建议见行内评论。
— qwen3.8-max via Qwen Code /review (v0.21.4)
| Filter allowFilter = JSONReader.autoTypeFilter(AutoTypeDerived.class); | ||
| List<AutoTypeBase> allowed = JSON.parseArray(jsonArray, AutoTypeBase.class, allowFilter); |
There was a problem hiding this comment.
[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)
| if (reader.resolveTasks != null) { | ||
| reader.handleResolveTasks(list); | ||
| } |
There was a problem hiding this comment.
[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")); | ||
| } |
There was a problem hiding this comment.
[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)
| * @throws JSONException If a parsing error occurs | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| static <T> List<T> parseArray(String text, Class<T> type, Filter filter, JSONReader.Feature... features) { |
There was a problem hiding this comment.
[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)
|
@wenshao 该 PR 的 review 建议均已处理完毕,烦查阅合并,谢谢! |
parseArray 新增支持 Filter 参数重载,在转换 jsonArrayString 时,支持通过 Filter 设置增强对原来版本 autoType 的转换需求,如:JSONReader.autoTypeFilter(...)