Skip to content

Commit

Permalink
Merge pull request #5765 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…5670-to-release-1.0

🐛 Fix adding appended list directly causing changing previous value
  • Loading branch information
k8s-ci-robot authored Dec 1, 2021
2 parents 6467781 + 347b9eb commit 89db44e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,11 @@ func paths(path []string, diff map[string]interface{}) [][]string {
for key, m := range diff {
nested, ok := m.(map[string]interface{})
if !ok {
allPaths = append(allPaths, append(path, key))
// We have to use a copy of path, because otherwise the slice we append to
// allPaths would be overwritten in another iteration.
tmp := make([]string, len(path))
copy(tmp, path)
allPaths = append(allPaths, append(tmp, key))
continue
}
allPaths = append(allPaths, paths(append(path, key), nested)...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,23 @@ func TestPaths(t *testing.T) {
diff: map[string]interface{}{},
expected: [][]string{},
},
{
name: "long recursive check with two keys",
diff: map[string]interface{}{
"spec": map[string]interface{}{
"kubeadmConfigSpec": map[string]interface{}{
"clusterConfiguration": map[string]interface{}{
"version": "v2.0.1",
"abc": "d",
},
},
},
},
expected: [][]string{
{"spec", "kubeadmConfigSpec", "clusterConfiguration", "version"},
{"spec", "kubeadmConfigSpec", "clusterConfiguration", "abc"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 89db44e

Please sign in to comment.