Skip to content

[BUG] Exception in JSONPath.eval #2585

@Cooperzzy

Description

@Cooperzzy

问题描述

JSONPath.eval 两个不同字符串时抛exception,以下代码在fastjson 1.2.83中均通过,感觉是个问题

环境信息

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson2 2.0.50]

重现步骤

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

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

public class Issue1965_757_2 {

    @Test
    public void testMutant1() {
        String str1 = "{\"data\": [1]}";
        String str2 = "{\n" +
                "  \"code\": \"1003\", \n" +
                "  \"data\": [1], \n" +
                "  \"message\": \"code: 1003,以【你好】开头的句子的长度不符合要求, 长度限制:3~500,实际长度:2\"\n" +
                "}\n";
        System.out.println(str1);
        System.out.println(str2);
        Object obj1 = JSONPath.eval(
                str1, "$.data[0][0]");
        Object obj2 = JSONPath.eval(
                str2, "$.data[0][0]");
        assertEquals(obj1, obj2);
    }

    @Test
    public void testMutant2() {
        String str1 = "{\"data\": [1,2]}";
        String str2 = "{\n" +
                "  \"code\": \"1003\", \n" +
                "  \"data\": [1,2], \n" +
                "  \"message\": \"code: 1003,以【你好】开头的句子的长度不符合要求, 长度限制:3~500,实际长度:2\"\n" +
                "}\n";
        System.out.println(str1);
        System.out.println(str2);
        assertEquals(
                JSONPath.eval(str1, "$.data[0][0]"),
                JSONPath.eval(str2, "$.data[0][0]"));
    }

    @Test
    public void testMutant3() {
        String str1 = "{\"data\": [{\"id\":\"1\"}]}";
        String str2 = "{\n" +
                "  \"code\": \"1003\", \n" +
                "  \"data\": [{\"id\":\"1\"}], \n" +
                "  \"message\": \"code: 1003,以【你好】开头的句子的长度不符合要求, 长度限制:3~500,实际长度:2\"\n" +
                "}\n";
        System.out.println(str1);
        System.out.println(str2);
        assertEquals(
                JSONPath.eval(str1, "$.data[0][0]"),
                JSONPath.eval(str2, "$.data[0][0]"));
    }

}

相关日志输出

com.alibaba.fastjson2.JSONValidException: error, offset 51, char :
at com.alibaba.fastjson2.JSONReader.error(JSONReader.java:4544)
at com.alibaba.fastjson2.JSONReaderUTF16.skipValue(JSONReaderUTF16.java:3726)
at com.alibaba.fastjson2.JSONPathSegmentIndex.accept(JSONPathSegmentIndex.java:489)
at com.alibaba.fastjson2.JSONPathMulti.extract(JSONPathMulti.java:402)
at com.alibaba.fastjson2.JSONPath.extract(JSONPath.java:56)
at com.alibaba.fastjson2.JSONPath.eval(JSONPath.java:66)
at Issue1965_757_2.testMutant2(Issue1965_757_2.java:39)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions