Skip to content

Commit

Permalink
Merge pull request #95933 from brianpursley/kubernetes-95882
Browse files Browse the repository at this point in the history
Fix bug in JSON path parser where an error occurs when a range is empty
  • Loading branch information
k8s-ci-robot committed Nov 3, 2020
2 parents a69a4a4 + 10634c6 commit cd99c63
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
16 changes: 13 additions & 3 deletions staging/src/k8s.io/client-go/util/jsonpath/jsonpath.go
Expand Up @@ -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--

Expand Down
15 changes: 15 additions & 0 deletions staging/src/k8s.io/client-go/util/jsonpath/jsonpath_test.go
Expand Up @@ -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": [
Expand Down

0 comments on commit cd99c63

Please sign in to comment.