Merge pull request #95933 from brianpursley/kubernetes-95882

Fix bug in JSON path parser where an error occurs when a range is empty
This commit is contained in:
Kubernetes Prow Robot 2020-11-03 01:26:15 -08:00 committed by GitHub
commit cd99c63570
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

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": [