Skip to content

Commit

Permalink
fix: use k8syml.YAMLToJSON when marshalling resources
Browse files Browse the repository at this point in the history
Properly handles cases where the YAML is not valid JSON, such as
maps with integer keys (#3446).
  • Loading branch information
dwalters committed Apr 23, 2022
1 parent 9d5491c commit 83b46b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
25 changes: 25 additions & 0 deletions api/krusty/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,31 @@ metadata:
`)
}

func TestIssue3446(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK(".", `
resources:
- cm.yaml
`)
th.WriteF("cm.yaml", `
apiVersion: v1
kind: ConfigMap
metadata:
name: cm
data:
123: abc
`)
m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: v1
data:
"123": abc
kind: ConfigMap
metadata:
name: cm
`)
}

func TestGeneratorSimpleOverlay(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK("base", `
Expand Down
10 changes: 6 additions & 4 deletions kyaml/yaml/rnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strconv"
"strings"

k8syaml "sigs.k8s.io/yaml"

"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml"
"sigs.k8s.io/kustomize/kyaml/sliceutil"
Expand Down Expand Up @@ -887,11 +889,11 @@ func (rn *RNode) MarshalJSON() ([]byte, error) {
return json.Marshal(a)
}

m := map[string]interface{}{}
if err := Unmarshal([]byte(s), &m); err != nil {
return nil, err
b, err := k8syaml.YAMLToJSON([]byte(s))
if err != nil {
return nil, errors.Wrap(err)
}
return json.Marshal(m)
return b, nil
}

// UnmarshalJSON overwrites this RNode with data from []byte.
Expand Down

0 comments on commit 83b46b6

Please sign in to comment.