Merge pull request #2334 from mortent/FixKyamlJson

Fix issue with kyaml json unmarshalling
This commit is contained in:
Kubernetes Prow Robot
2020-04-05 15:51:46 -07:00
committed by GitHub
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 {
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 {
return err
}

View File

@@ -50,20 +50,57 @@ spec:
}
func TestRNode_UnmarshalJSON(t *testing.T) {
instance := &RNode{}
err := instance.UnmarshalJSON([]byte(`{"hello":"world"}`))
if !assert.NoError(t, err) {
t.FailNow()
}
actual, err := instance.String()
if !assert.NoError(t, err) {
t.FailNow()
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
`,
},
}
expected := `{"hello": "world"}`
if !assert.Equal(t,
strings.TrimSpace(expected), strings.TrimSpace(actual)) {
t.FailNow()
for i := range testCases {
tc := testCases[i]
t.Run(tc.testName, func(t *testing.T) {
instance := &RNode{}
err := instance.UnmarshalJSON([]byte(tc.input))
if !assert.NoError(t, err) {
t.FailNow()
}
actual, err := instance.String()
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.Equal(t,
strings.TrimSpace(tc.output), strings.TrimSpace(actual)) {
t.FailNow()
}
})
}
}