Merge pull request #95933 from brianpursley/kubernetes-95882

Fix bug in JSON path parser where an error occurs when a range is empty

Kubernetes-commit: cd99c63570eb1489dd631c12ea86db708dbdcd59
This commit is contained in:
Kubernetes Publisher 2020-11-03 01:26:15 -08:00
commit 534b10dd04
5 changed files with 36 additions and 11 deletions

4
Godeps/Godeps.json generated
View File

@ -448,11 +448,11 @@
},
{
"ImportPath": "k8s.io/api",
"Rev": "7a95b569f5ff"
"Rev": "18d4659ab40b"
},
{
"ImportPath": "k8s.io/apimachinery",
"Rev": "8046191f3410"
"Rev": "1aed267868af"
},
{
"ImportPath": "k8s.io/gengo",

8
go.mod
View File

@ -26,14 +26,14 @@ require (
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
k8s.io/api v0.0.0-20201102202211-7a95b569f5ff
k8s.io/apimachinery v0.0.0-20201102202032-8046191f3410
k8s.io/api v0.0.0-20201103002210-18d4659ab40b
k8s.io/apimachinery v0.0.0-20201103082028-1aed267868af
k8s.io/klog/v2 v2.2.0
k8s.io/utils v0.0.0-20200729134348-d5654de09c73
sigs.k8s.io/yaml v1.2.0
)
replace (
k8s.io/api => k8s.io/api v0.0.0-20201102202211-7a95b569f5ff
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20201102202032-8046191f3410
k8s.io/api => k8s.io/api v0.0.0-20201103002210-18d4659ab40b
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20201103082028-1aed267868af
)

4
go.sum
View File

@ -335,8 +335,8 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
k8s.io/api v0.0.0-20201102202211-7a95b569f5ff/go.mod h1:DvHvdeQUPLTohsRVYB2Fd49FYz7l0WWEnJBD4BrP9Pc=
k8s.io/apimachinery v0.0.0-20201102202032-8046191f3410/go.mod h1:6s3VNb000AUbBIxR7q3WHlbBwfpEGqIJsCG5gIX+0LI=
k8s.io/api v0.0.0-20201103002210-18d4659ab40b/go.mod h1:b+K6RbdbECmbVaw11+iMzmt6th7YpfdynGdh/HKUa54=
k8s.io/apimachinery v0.0.0-20201103082028-1aed267868af/go.mod h1:6s3VNb000AUbBIxR7q3WHlbBwfpEGqIJsCG5gIX+0LI=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A=

View File

@ -103,13 +103,23 @@ func (j *JSONPath) FindResults(data interface{}) ([][]reflect.Value, error) {
if j.beginRange > 0 {
j.beginRange--
j.inRange++
for _, value := range results {
if len(results) > 0 {
for _, value := range results {
j.parser.Root.Nodes = nodes[i+1:]
nextResults, err := j.FindResults(value.Interface())
if err != nil {
return nil, err
}
fullResult = append(fullResult, nextResults...)
}
} else {
// If the range has no results, we still need to process the nodes within the range
// so the position will advance to the end node
j.parser.Root.Nodes = nodes[i+1:]
nextResults, err := j.FindResults(value.Interface())
_, err := j.FindResults(nil)
if err != nil {
return nil, err
}
fullResult = append(fullResult, nextResults...)
}
j.inRange--

View File

@ -393,6 +393,21 @@ func TestKubernetes(t *testing.T) {
testJSONPathSortOutput(randomPrintOrderTests, t)
}
func TestEmptyRange(t *testing.T) {
var input = []byte(`{"items":[]}`)
var emptyList interface{}
err := json.Unmarshal(input, &emptyList)
if err != nil {
t.Error(err)
}
tests := []jsonpathTest{
{"empty range", `{range .items[*]}{.metadata.name}{end}`, &emptyList, "", false},
{"empty nested range", `{range .items[*]}{.metadata.name}{":"}{range @.spec.containers[*]}{.name}{","}{end}{"+"}{end}`, &emptyList, "", false},
}
testJSONPath(tests, true, t)
}
func TestNestedRanges(t *testing.T) {
var input = []byte(`{
"items": [