mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Merge pull request #125615 from benluddy/apiextensions-json-unmarshal-copy
Copy input to UnmarshalJSON on the apiextensions JSON types.
This commit is contained in:
commit
d2d55831e5
@ -130,7 +130,7 @@ func (s JSON) MarshalJSON() ([]byte, error) {
|
|||||||
|
|
||||||
func (s *JSON) UnmarshalJSON(data []byte) error {
|
func (s *JSON) UnmarshalJSON(data []byte) error {
|
||||||
if len(data) > 0 && !bytes.Equal(data, nullLiteral) {
|
if len(data) > 0 && !bytes.Equal(data, nullLiteral) {
|
||||||
s.Raw = data
|
s.Raw = append(s.Raw[0:0], data...)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -148,3 +148,24 @@ func TestJSONSchemaPropsOrArrayMarshalJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJSONUnderlyingArrayReuse(t *testing.T) {
|
||||||
|
const want = `{"foo":"bar"}`
|
||||||
|
|
||||||
|
b := []byte(want)
|
||||||
|
|
||||||
|
var s JSON
|
||||||
|
if err := s.UnmarshalJSON(b); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Underlying array is modified.
|
||||||
|
copy(b[2:5], "bar")
|
||||||
|
copy(b[8:11], "foo")
|
||||||
|
|
||||||
|
// If UnmarshalJSON copied the bytes of its argument, then it should not have been affected
|
||||||
|
// by the mutation.
|
||||||
|
if got := string(s.Raw); got != want {
|
||||||
|
t.Errorf("unexpected mutation, got %s want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -130,7 +130,7 @@ func (s JSON) MarshalJSON() ([]byte, error) {
|
|||||||
|
|
||||||
func (s *JSON) UnmarshalJSON(data []byte) error {
|
func (s *JSON) UnmarshalJSON(data []byte) error {
|
||||||
if len(data) > 0 && !bytes.Equal(data, nullLiteral) {
|
if len(data) > 0 && !bytes.Equal(data, nullLiteral) {
|
||||||
s.Raw = data
|
s.Raw = append(s.Raw[0:0], data...)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -148,3 +148,24 @@ func TestJSONSchemaPropsOrArrayMarshalJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJSONUnderlyingArrayReuse(t *testing.T) {
|
||||||
|
const want = `{"foo":"bar"}`
|
||||||
|
|
||||||
|
b := []byte(want)
|
||||||
|
|
||||||
|
var s JSON
|
||||||
|
if err := s.UnmarshalJSON(b); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Underlying array is modified.
|
||||||
|
copy(b[2:5], "bar")
|
||||||
|
copy(b[8:11], "foo")
|
||||||
|
|
||||||
|
// If UnmarshalJSON copied the bytes of its argument, then it should not have been affected
|
||||||
|
// by the mutation.
|
||||||
|
if got := string(s.Raw); got != want {
|
||||||
|
t.Errorf("unexpected mutation, got %s want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user