Skip to content

Commit

Permalink
UPSTREAM: <carry>: switch back to use ugorji/go to avoid deserializat…
Browse files Browse the repository at this point in the history
…ion 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
  • Loading branch information
liggitt authored and deads2k committed Jan 2, 2018
1 parent b536596 commit 3ed39b9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
7 changes: 5 additions & 2 deletions pkg/api/testing/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/ugorji/go/codec"

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

handler := &codec.JsonHandle{}

b.ResetTimer()
for i := 0; i < b.N; i++ {
obj := v1.Pod{}
//if err := jsoniter.ConfigFastest.Unmarshal(encoded[i%width], &obj); err != nil {
if err := json.Unmarshal(encoded[i%width], &obj); err != nil {
// if err := jsoniter.ConfigFastest.Unmarshal(encoded[i%width], &obj); err != nil {
if err := codec.NewDecoderBytes(encoded[i%width], handler).Decode(&obj); err != nil {
b.Fatal(err)
}
}
Expand Down
20 changes: 11 additions & 9 deletions staging/src/k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,23 @@ func v1alpha1FuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
case 0:
r.Cells[i] = c.RandString()
case 1:
// largest representable int (unstructured data parses as floats)
r.Cells[i] = float64(c.Int63n(9007199254740991))
r.Cells[i] = c.Uint64()
case 2:
r.Cells[i] = c.RandBool()
case 3:
x := map[string]interface{}{}
for j := c.Intn(10) + 1; j >= 0; j-- {
x[c.RandString()] = c.RandString()
}
r.Cells[i] = x
// maps roundtrip as map[interface{}]interface{}, but the json codec cannot encode that
// TODO: get maps to roundtrip properly
/*
x := map[string]interface{}{}
for j := c.Intn(10) + 1; j >= 0; j-- {
x[c.RandString()] = c.RandString()
}
r.Cells[i] = x
*/
case 4:
x := make([]interface{}, c.Intn(10))
for i := range x {
// largest representable int (unstructured data parses as floats)
x[i] = float64(c.Int63n(9007199254740991))
x[i] = c.Uint64()
}
r.Cells[i] = x
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package v1

import (
"encoding/json"
"reflect"
"testing"

"k8s.io/apimachinery/pkg/util/json"
"github.com/ugorji/go/codec"
)

type GroupVersionHolder struct {
Expand All @@ -47,8 +48,10 @@ func TestGroupVersionUnmarshalJSON(t *testing.T) {
}
// test the json-iterator codec
// if err := jsoniter.ConfigFastest.Unmarshal(c.input, &result); err != nil {
if err := json.Unmarshal(c.input, &result); err != nil {
t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
// t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
// test the Ugorji codec
if err := codec.NewDecoderBytes(c.input, new(codec.JsonHandle)).Decode(&result); err != nil {
t.Errorf("Ugorji codec failed to unmarshal input '%v': %v", c.input, err)
}
if !reflect.DeepEqual(result.GV, c.expect) {
t.Errorf("json-iterator codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package v1

import (
"encoding/json"
"reflect"
"testing"

"k8s.io/apimachinery/pkg/util/json"
"github.com/ugorji/go/codec"
)

func TestVerbsUgorjiMarshalJSON(t *testing.T) {
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestVerbsUgorjiUnmarshalJSON(t *testing.T) {
for i, c := range cases {
var result APIResource
// if err := jsoniter.ConfigFastest.Unmarshal([]byte(c.input), &result); err != nil {
if err := json.Unmarshal([]byte(c.input), &result); err != nil {
if err := codec.NewDecoderBytes([]byte(c.input), new(codec.JsonHandle)).Decode(&result); err != nil {
t.Errorf("[%d] Failed to unmarshal input '%v': %v", i, c.input, err)
}
if !reflect.DeepEqual(result, c.result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ limitations under the License.
package json

import (
gojson "encoding/json"
"encoding/json"
"io"

"github.com/ghodss/yaml"
"github.com/ugorji/go/codec"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
"k8s.io/apimachinery/pkg/util/framer"
"k8s.io/apimachinery/pkg/util/json"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
)

Expand Down Expand Up @@ -66,6 +66,36 @@ type Serializer struct {
var _ runtime.Serializer = &Serializer{}
var _ recognizer.RecognizingDecoder = &Serializer{}

func init() {
// Force jsoniter to decode number to interface{} via ints, if possible.
// decodeNumberAsInt64IfPossible := func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
// switch iter.WhatIsNext() {
// case jsoniter.NumberValue:
// var number json.Number
// iter.ReadVal(&number)
// u64, err := strconv.ParseUint(string(number), 10, 64)
// if err == nil {
// *(*interface{})(ptr) = u64
// return
// }
// i64, err := strconv.ParseInt(string(number), 10, 64)
// if err == nil {
// *(*interface{})(ptr) = i64
// return
// }
// f64, err := strconv.ParseFloat(string(number), 64)
// if err == nil {
// *(*interface{})(ptr) = f64
// return
// }
// // Not much we can do here.
// default:
// *(*interface{})(ptr) = iter.Read()
// }
// }
// jsoniter.RegisterTypeDecoderFunc("interface {}", decodeNumberAsInt64IfPossible)
}

// Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
// 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
// extracted and no decoding will be performed. If into is not registered with the typer, then the object will be straight decoded using
Expand Down Expand Up @@ -123,7 +153,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
switch {
case runtime.IsNotRegisteredError(err), isUnstructured:
//if err := jsoniter.ConfigFastest.Unmarshal(data, into); err != nil {
if err := json.Unmarshal(data, into); err != nil {
if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(into); err != nil {
return nil, actual, err
}
return into, actual, nil
Expand Down Expand Up @@ -157,7 +187,8 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
return nil, actual, err
}

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

if s.pretty {
data, err := gojson.MarshalIndent(obj, "", " ")
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
Expand Down

0 comments on commit 3ed39b9

Please sign in to comment.