Skip to content

Commit da295db

Browse files
liggittdeads2k
authored andcommitted
UPSTREAM: <carry>: switch back to use ugorji/go to avoid deserialization errors
:100644 100644 37a5106cb8... de3485af9a... M pkg/api/testing/serialization_test.go :100644 100644 cad9142313... 51973be626... M staging/src/k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go :100644 100644 8ec26f54c4... b261dd2ed5... M staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/group_version_test.go :100644 100644 c7442ed7ff... 59a82bbfe5... M staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types_test.go :100644 100644 aa913e2a45... 7dbe2499aa... M staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go
1 parent 21c7a18 commit da295db

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

pkg/api/testing/serialization_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"testing"
2727

2828
"github.com/golang/protobuf/proto"
29+
"github.com/ugorji/go/codec"
2930

3031
"k8s.io/api/core/v1"
3132
"k8s.io/api/extensions/v1beta1"
@@ -558,11 +559,13 @@ func BenchmarkDecodeIntoJSONCodecGen(b *testing.B) {
558559
encoded[i] = data
559560
}
560561

562+
handler := &codec.JsonHandle{}
563+
561564
b.ResetTimer()
562565
for i := 0; i < b.N; i++ {
563566
obj := v1.Pod{}
564-
//if err := jsoniter.ConfigFastest.Unmarshal(encoded[i%width], &obj); err != nil {
565-
if err := json.Unmarshal(encoded[i%width], &obj); err != nil {
567+
// if err := jsoniter.ConfigFastest.Unmarshal(encoded[i%width], &obj); err != nil {
568+
if err := codec.NewDecoderBytes(encoded[i%width], handler).Decode(&obj); err != nil {
566569
b.Fatal(err)
567570
}
568571
}

staging/src/k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -268,21 +268,23 @@ func v1alpha1FuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
268268
case 0:
269269
r.Cells[i] = c.RandString()
270270
case 1:
271-
// largest representable int (unstructured data parses as floats)
272-
r.Cells[i] = float64(c.Int63n(9007199254740991))
271+
r.Cells[i] = c.Uint64()
273272
case 2:
274273
r.Cells[i] = c.RandBool()
275274
case 3:
276-
x := map[string]interface{}{}
277-
for j := c.Intn(10) + 1; j >= 0; j-- {
278-
x[c.RandString()] = c.RandString()
279-
}
280-
r.Cells[i] = x
275+
// maps roundtrip as map[interface{}]interface{}, but the json codec cannot encode that
276+
// TODO: get maps to roundtrip properly
277+
/*
278+
x := map[string]interface{}{}
279+
for j := c.Intn(10) + 1; j >= 0; j-- {
280+
x[c.RandString()] = c.RandString()
281+
}
282+
r.Cells[i] = x
283+
*/
281284
case 4:
282285
x := make([]interface{}, c.Intn(10))
283286
for i := range x {
284-
// largest representable int (unstructured data parses as floats)
285-
x[i] = float64(c.Int63n(9007199254740991))
287+
x[i] = c.Uint64()
286288
}
287289
r.Cells[i] = x
288290
default:

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/group_version_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"encoding/json"
2021
"reflect"
2122
"testing"
2223

23-
"k8s.io/apimachinery/pkg/util/json"
24+
"github.com/ugorji/go/codec"
2425
)
2526

2627
type GroupVersionHolder struct {
@@ -47,8 +48,10 @@ func TestGroupVersionUnmarshalJSON(t *testing.T) {
4748
}
4849
// test the json-iterator codec
4950
// if err := jsoniter.ConfigFastest.Unmarshal(c.input, &result); err != nil {
50-
if err := json.Unmarshal(c.input, &result); err != nil {
51-
t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
51+
// t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
52+
// test the Ugorji codec
53+
if err := codec.NewDecoderBytes(c.input, new(codec.JsonHandle)).Decode(&result); err != nil {
54+
t.Errorf("Ugorji codec failed to unmarshal input '%v': %v", c.input, err)
5255
}
5356
if !reflect.DeepEqual(result.GV, c.expect) {
5457
t.Errorf("json-iterator codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"encoding/json"
2021
"reflect"
2122
"testing"
2223

23-
"k8s.io/apimachinery/pkg/util/json"
24+
"github.com/ugorji/go/codec"
2425
)
2526

2627
func TestVerbsUgorjiMarshalJSON(t *testing.T) {
@@ -58,7 +59,7 @@ func TestVerbsUgorjiUnmarshalJSON(t *testing.T) {
5859
for i, c := range cases {
5960
var result APIResource
6061
// if err := jsoniter.ConfigFastest.Unmarshal([]byte(c.input), &result); err != nil {
61-
if err := json.Unmarshal([]byte(c.input), &result); err != nil {
62+
if err := codec.NewDecoderBytes([]byte(c.input), new(codec.JsonHandle)).Decode(&result); err != nil {
6263
t.Errorf("[%d] Failed to unmarshal input '%v': %v", i, c.input, err)
6364
}
6465
if !reflect.DeepEqual(result, c.result) {

staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go

+36-5
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ limitations under the License.
1717
package json
1818

1919
import (
20-
gojson "encoding/json"
20+
"encoding/json"
2121
"io"
2222

2323
"github.com/ghodss/yaml"
24+
"github.com/ugorji/go/codec"
2425

2526
"k8s.io/apimachinery/pkg/runtime"
2627
"k8s.io/apimachinery/pkg/runtime/schema"
2728
"k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
2829
"k8s.io/apimachinery/pkg/util/framer"
29-
"k8s.io/apimachinery/pkg/util/json"
3030
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
3131
)
3232

@@ -66,6 +66,36 @@ type Serializer struct {
6666
var _ runtime.Serializer = &Serializer{}
6767
var _ recognizer.RecognizingDecoder = &Serializer{}
6868

69+
func init() {
70+
// Force jsoniter to decode number to interface{} via ints, if possible.
71+
// decodeNumberAsInt64IfPossible := func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
72+
// switch iter.WhatIsNext() {
73+
// case jsoniter.NumberValue:
74+
// var number json.Number
75+
// iter.ReadVal(&number)
76+
// u64, err := strconv.ParseUint(string(number), 10, 64)
77+
// if err == nil {
78+
// *(*interface{})(ptr) = u64
79+
// return
80+
// }
81+
// i64, err := strconv.ParseInt(string(number), 10, 64)
82+
// if err == nil {
83+
// *(*interface{})(ptr) = i64
84+
// return
85+
// }
86+
// f64, err := strconv.ParseFloat(string(number), 64)
87+
// if err == nil {
88+
// *(*interface{})(ptr) = f64
89+
// return
90+
// }
91+
// // Not much we can do here.
92+
// default:
93+
// *(*interface{})(ptr) = iter.Read()
94+
// }
95+
// }
96+
// jsoniter.RegisterTypeDecoderFunc("interface {}", decodeNumberAsInt64IfPossible)
97+
}
98+
6999
// Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
70100
// load that data into an object matching the desired schema kind or the provided into. If into is *runtime.Unknown, the raw data will be
71101
// extracted and no decoding will be performed. If into is not registered with the typer, then the object will be straight decoded using
@@ -123,7 +153,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
123153
switch {
124154
case runtime.IsNotRegisteredError(err), isUnstructured:
125155
//if err := jsoniter.ConfigFastest.Unmarshal(data, into); err != nil {
126-
if err := json.Unmarshal(data, into); err != nil {
156+
if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(into); err != nil {
127157
return nil, actual, err
128158
}
129159
return into, actual, nil
@@ -157,7 +187,8 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
157187
return nil, actual, err
158188
}
159189

160-
if err := json.Unmarshal(data, obj); err != nil {
190+
// if err := jsoniter.ConfigFastest.Unmarshal(data, obj); err != nil {
191+
if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(obj); err != nil {
161192
return nil, actual, err
162193
}
163194
return obj, actual, nil
@@ -179,7 +210,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
179210
}
180211

181212
if s.pretty {
182-
data, err := gojson.MarshalIndent(obj, "", " ")
213+
data, err := json.MarshalIndent(obj, "", " ")
183214
if err != nil {
184215
return err
185216
}

0 commit comments

Comments
 (0)