Fix issue with kyaml json unmarshalling

This commit is contained in:
Morten Torkildsen
2020-04-04 18:42:15 -07:00
parent 0f3d5c80e4
commit 022805b56b
2 changed files with 60 additions and 13 deletions

View File

@@ -697,7 +697,17 @@ func (rn *RNode) MarshalJSON() ([]byte, error) {
} }
func (rn *RNode) UnmarshalJSON(b []byte) error { func (rn *RNode) UnmarshalJSON(b []byte) error {
r, err := Parse(string(b)) m := map[string]interface{}{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
c, err := Marshal(m)
if err != nil {
return err
}
r, err := Parse(string(c))
if err != nil { if err != nil {
return err return err
} }

View File

@@ -50,21 +50,58 @@ spec:
} }
func TestRNode_UnmarshalJSON(t *testing.T) { func TestRNode_UnmarshalJSON(t *testing.T) {
testCases := []struct {
testName string
input string
output string
}{
{
testName: "simple document",
input: `{"hello":"world"}`,
output: `hello: world`,
},
{
testName: "nested structure",
input: `
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "my-deployment",
"namespace": "default"
}
}
`,
output: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
namespace: default
`,
},
}
for i := range testCases {
tc := testCases[i]
t.Run(tc.testName, func(t *testing.T) {
instance := &RNode{} instance := &RNode{}
err := instance.UnmarshalJSON([]byte(`{"hello":"world"}`)) err := instance.UnmarshalJSON([]byte(tc.input))
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.FailNow() t.FailNow()
} }
actual, err := instance.String() actual, err := instance.String()
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.FailNow() t.FailNow()
} }
expected := `{"hello": "world"}`
if !assert.Equal(t, if !assert.Equal(t,
strings.TrimSpace(expected), strings.TrimSpace(actual)) { strings.TrimSpace(tc.output), strings.TrimSpace(actual)) {
t.FailNow() t.FailNow()
} }
})
}
} }
func TestRNode_MarshalJSON(t *testing.T) { func TestRNode_MarshalJSON(t *testing.T) {