mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Fix issue with kyaml json unmarshalling
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,20 +50,57 @@ spec:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRNode_UnmarshalJSON(t *testing.T) {
|
func TestRNode_UnmarshalJSON(t *testing.T) {
|
||||||
instance := &RNode{}
|
testCases := []struct {
|
||||||
err := instance.UnmarshalJSON([]byte(`{"hello":"world"}`))
|
testName string
|
||||||
if !assert.NoError(t, err) {
|
input string
|
||||||
t.FailNow()
|
output string
|
||||||
}
|
}{
|
||||||
actual, err := instance.String()
|
{
|
||||||
if !assert.NoError(t, err) {
|
testName: "simple document",
|
||||||
t.FailNow()
|
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"}`
|
for i := range testCases {
|
||||||
if !assert.Equal(t,
|
tc := testCases[i]
|
||||||
strings.TrimSpace(expected), strings.TrimSpace(actual)) {
|
t.Run(tc.testName, func(t *testing.T) {
|
||||||
t.FailNow()
|
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()
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user