spyzhov / ajson

Abstract JSON for Golang with JSONPath support
MIT License
247 stars 23 forks source link

got panic in GetArray() for `$..` in `Eval` #81

Closed OTCMKTS closed 4 days ago

OTCMKTS commented 1 week ago

Code to repro:

func test2() {
    jb := []byte(`[{"a": 1, "b": 2, "c": 3}]`)
    root, err := ajson.Unmarshal(jb)
    if err != nil {
        fmt.Println("Got error1:", err)
        return
    }
    result, err := ajson.Eval(root, "$...")
    if err != nil {
        fmt.Println("Got error2:", err)
        return
    }

    arrs, err := result.GetArray()
    if err != nil {
        fmt.Println("Got error3:", err)
        return
    } else {
        fmt.Println("Try:", arrs)
        for _, arr := range arrs {
            if arr.Type() == ajson.Array {
                arr, err := arr.GetArray()
                if err != nil {
                    fmt.Println("Got error4:", err)
                    return
                } else {
                    fmt.Println("Try:", arr)
                }
            }
        }
    }
}

Console output:

Try: [[{"a": 1, "b": 2, "c": 3}] {"a": 1, "b": 2, "c": 3} {"a": 1, "b": 2, "c": 3}]
panic: runtime error: index out of range [2] with length 1

goroutine 1 [running]:
github.com/spyzhov/ajson.(*Node).getValue(0x14000128720)
    github.com/spyzhov/ajson@v0.9.5/node.go:391 +0x66c
github.com/spyzhov/ajson.(*Node).GetArray(0x10224bad8?)
    github.com/spyzhov/ajson@v0.9.5/node.go:483 +0x30
main.test2()
    bug-ajson/main.go:39 +0x25c
main.main()
    bug-ajson/main.go:15 +0x20
make: *** [run] Error 2
spyzhov commented 5 days ago

Hello, Thanks for the issue, I will fix it asap.

spyzhov commented 4 days ago

Thanks once again for the issue, it looks much deeper than just a small fix, it was caused because all Nodes referred to the same downstream objects, so the index was also replaced.

I will try my best to fix it as soon as possible.

spyzhov commented 4 days ago

Fixed in v0.9.6