[BUG]复杂筛选问题升级后的问题
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
首先非常感谢温少增加了复杂的筛选规则。详见:https://github.com/alibaba/fastjson2/issues/1410
我升级到2.0.35后,又发现了个问题,不知道是我的方法不对,还是程序处理的问题,烦请温少帮忙看一下原因。
### 环境信息
*请填写以下信息:*
- OS信息: [e.g.:Window11 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.0.35]
### 重现步骤
执行以下代码,childArr的结果为空。(jsonArr对象详见“附录:jsonArr”)
String path="$.[?( @.dictTypeId =='city' && @.dictId =='101' )]";
Object childArr=JsonUtil.read(jsonArr, path); //此时,childArr为null
改为以下代码,结果正常:
String path="$..[?( @.dictTypeId =='city' && @.dictId =='101' )]"; //开头的$后面两个点,即:“$..”,相当于递归查询所有子节点
Object childArr=JsonUtil.read(jsonArr, path);
childArr的结果详见“附录:childArr结果1”
但筛选条件改为只有一个条件时,返回的结果重复。
String path="$..[?( @.dictId =='101' )]";
Object childArr=JsonUtil.read(jsonArr, path); //此时childArr的值为两个JSONObject对象,而在jsonArr只有一个JSONObject节点
childArr的结果详见“附录:childArr结果2”
请问,path的定义是否存在问题,该如何定义?
### 期待的正确结果
期待的结果与“附录:childArr结果1”相同
#### 附加信息
附录:jsonArr
{
"dictId": "CN",
"dictName": "China",
"dictTypeId": "country",
"level": 1,
"rank": 1,
"seqno": ".CN.",
"sortno": 1,
"status": 1,
"children": [
{
"dictId": "1",
"dictName": "Jiangsu",
"dictTypeId": "province",
"level": 1,
"parentId": "CN",
"rank": 2,
"seqno": ".CN.1.",
"sortno": 1,
"status": 1,
"children": [
{
"dictId": "101",
"dictName": "Nanjing",
"dictTypeId": "city",
"level": 1,
"parentId": "1",
"rank": 3,
"seqno": ".CN.1.101.",
"sortno": 1,
"status": 1
},
{
"dictId": "102",
"dictName": "Changzhou",
"dictTypeId": "city",
"level": 1,
"parentId": "1",
"rank": 3,
"seqno": ".CN.1.102.",
"sortno": 2,
"status": 1
},
{
"dictId": "103",
"dictName": "Wuxi",
"dictTypeId": "city",
"level": 1,
"parentId": "1",
"rank": 3,
"seqno": ".CN.1.103.",
"sortno": 3,
"status": 1
}
]
},
{
"dictId": "2",
"dictName": "Guangdong",
"dictTypeId": "province",
"level": 1,
"parentId": "CN",
"rank": 2,
"seqno": ".CN.2.",
"sortno": 2,
"status": 1,
"children": [
{
"dictId": "201",
"dictName": "Guangzhou",
"dictTypeId": "city",
"level": 1,
"parentId": "2",
"rank": 3,
"seqno": ".CN.2.201.",
"sortno": 1,
"status": 1
},
{
"dictId": "202",
"dictName": "Shantou",
"dictTypeId": "city",
"level": 1,
"parentId": "2",
"rank": 3,
"seqno": ".CN.2.202.",
"sortno": 2,
"status": 1
},
{
"dictId": "203",
"dictName": "Shenzhen",
"dictTypeId": "city",
"level": 1,
"parentId": "2",
"rank": 3,
"seqno": ".CN.2.203.",
"sortno": 3,
"status": 1
}
]
},
{
"dictId": "3",
"dictName": "Fujian",
"dictTypeId": "province",
"level": 1,
"parentId": "CN",
"rank": 2,
"seqno": ".CN.3.",
"sortno": 3,
"status": 1,
"children": [
{
"dictId": "301",
"dictName": "Xiamen",
"dictTypeId": "city",
"level": 1,
"parentId": "3",
"rank": 3,
"seqno": ".CN.3.301.",
"sortno": 1,
"status": 1
},
{
"dictId": "302",
"dictName": "Fuzhou",
"dictTypeId": "city",
"level": 1,
"parentId": "3",
"rank": 3,
"seqno": ".CN.3.302.",
"sortno": 2,
"status": 1
}
]
}
]
}
附录:childArr结果1
[
{
"dictId": "101",
"dictName": "Nanjing",
"dictTypeId": "city",
"level": 1,
"parentId": "1",
"rank": 3,
"seqno": ".CN.1.101.",
"sortno": 1,
"status": 1
}
]
附录:childArr结果2
[
{
"dictId": "101",
"dictName": "Nanjing",
"dictTypeId": "city",
"level": 1,
"parentId": "1",
"rank": 3,
"seqno": ".CN.1.101.",
"sortno": 1,
"status": 1
},
{
"dictId": "101",
"dictName": "Nanjing",
"dictTypeId": "city",
"level": 1,
"parentId": "1",
"rank": 3,
"seqno": ".CN.1.101.",
"sortno": 1,
"status": 1
}
]
Contributor guide
Research direction
Start with the JsonUtil.read calls and the three JSONPath expressions shown in the reproduction, comparing direct-child selection with recursive selection. Reproduce the empty result and duplicate JSONObject result using the supplied jsonArr, then confirm that the two-condition query returns one matching city without duplicates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100