Repository navigation
parseArray 新增支持 Filter 参数的方法重载 #3343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
eee70cd
0d352e1
2606838
f23320b
82eb33b
b985a78
f47cd51
af6701d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
| if (text == null || text.isEmpty()) { | ||
| return null; | ||
| } | ||
|
Comment on lines
+3030
to
+3033
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] 新增的 建议添加测试:传入 中文说明[建议] 新增的 失败场景: 如果未来重构意外将 建议修复: 添加测试用例,传入 — 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] This Failure scenario: mutation-verified — removing only the Suggested fix: add a List<AutoTypeBase> refs = JSON.parseArray(
"[{\"id\":1},{\"$ref\":\"$[0]\"}]", AutoTypeBase.class, allowFilter);
assertSame(refs.get(0), refs.get(1));中文说明[建议] 该 失败场景: 已实际运行突变体验证——仅删除 建议修复: 补充一个经由新重载的 — 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] This trailing-input guard ( Failure scenario: if the guard were dropped, 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());中文说明[建议] 该尾随输入检查( 失败场景: 若该检查被删除, 建议修复: 补充一个尾随内容用例(同时杀死本突变体与 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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.*; | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] The Failure scenario: mutation-verified — replacing 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));中文说明[建议] 新重载 失败场景: 已实际运行突变体验证——将 建议修复: 补充下方两个用例(同时杀死本突变体与 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> { | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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...)(literalnullthird 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 toparseArray(String, Class, Feature...)) but fails against this PR withreference to parseArray is ambiguous. Two mitigating facts: (1) that call shape always NPE'd at runtime on 2.0.64 (createReadContextdereferences the null feature element), so no working downstream code breaks — this converts a runtime-NPE shape into a compile error; (2)parseObjectalready 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 formparseArray(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) nullcast.中文说明
[建议] 新增的 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)