alibaba / alibaba/fastjson2

[BUG] JSONPath.extract on illegal path

Open
#2,580 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
JSONPath.extract对于illegal path抛出fastjson2.JSONException: TODO,fastjson 1.2.83 能够返回null

### 环境信息
- OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
- JDK信息: [Openjdk 17.0.6]
- 版本信息:[Fastjson2 2.0.50]

### 重现步骤

```java

import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class Issue116Mutated_466 {
String testJson = "{\"soap:Envelope\":{\"-xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\",\"-xmlns:soap\":\"http://schemas.xmlsoap.org/soap/envelope/\",\"-xmlns:xsd\":\"http://www.w3.org/2001/XMLSchema\",\"soap:Body\":{\"getCountryCityByIpResponse\":{\"-xmlns\":\"http://WebXml.com.cn/\",\"getCountryCityByIpResult\":{\"string\":[\"30.40.202.24\",\"美国 俄亥俄州哥伦布市国防部网络信息中心\",\"30.40.202.25\"]}}}}}";

@Test
public void testMutated() {
Object result = JSONPath.extract(testJson, "$.soap\\:Envelope.soap\\:Body.getCountryCityByIpResponse.getCountryCityByIpResult.string[*]");
assertNotNull(result);
assertEquals("[\"30.40.202.24\",\"美国 俄亥俄州哥伦布市国防部网络信息中心\",\"30.40.202.25\"]", result.toString());
}

@Test
public void testMutated1() {
Object result = JSONPath.extract(testJson, "$.soap\\:Envelope.soap\\:Body.getCountryCityByIpResponse.getCountryCityByIpResult.nothing[*]");
assertNull(result);
}

@Test
public void testMutated1fj() {
// fastjson 1.2.83
Object result = com.alibaba.fastjson.JSONPath.extract(testJson, "$.soap\\:Envelope.soap\\:Body.getCountryCityByIpResponse.getCountryCityByIpResult.nothing[*]");
assertNull(result);
}
}

```

### 期待的正确结果
返回null

### 相关日志输出
com.alibaba.fastjson2.JSONException: TODO
at com.alibaba.fastjson2.JSONPathSegment$AllSegment.accept(JSONPathSegment.java:1291)
at com.alibaba.fastjson2.JSONPathMulti.extract(JSONPathMulti.java:402)
at com.alibaba.fastjson2.JSONPath.extract(JSONPath.java:56)
at Issue116Mutated_466.testMutated1(Issue116Mutated_466.java:20)

Contributor guide

Open the contributing guide

Research direction

Start with JSONPathSegment.java at AllSegment.accept, then trace the call from JSONPathMulti.extract shown in the stack trace. Reproduce the supplied tests for a missing JSONPath property and the valid property path. Done means the illegal path returns null as expected without changing the valid extraction result.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.