Skip to content

Commit

Permalink
Merge pull request #149 from sttts/sttts-handler-yaml-int-types
Browse files Browse the repository at this point in the history
handler: convert int types correctly like yaml2 does and gnostic expects
  • Loading branch information
k8s-ci-robot committed Feb 28, 2019
2 parents d50a959 + 1d6fc57 commit b3a7cee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
23 changes: 20 additions & 3 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
"time"

"github.com/NYTimes/gziphandler"
restful "github.com/emicklei/go-restful"
"github.com/emicklei/go-restful"
"github.com/go-openapi/spec"
"github.com/golang/protobuf/proto"
openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
"github.com/googleapis/gnostic/OpenAPIv2"
"github.com/googleapis/gnostic/compiler"
"github.com/json-iterator/go"
"github.com/munnerz/goautoneg"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"

"k8s.io/kube-openapi/pkg/builder"
"k8s.io/kube-openapi/pkg/common"
Expand Down Expand Up @@ -159,6 +159,23 @@ func jsonToYAMLValue(j interface{}) interface{} {
ret[i] = jsonToYAMLValue(j[i])
}
return ret
case float64:
// replicate the logic in https://github.com/go-yaml/yaml/blob/51d6538a90f86fe93ac480b35f37b2be17fef232/resolve.go#L151
if i64 := int64(j); j == float64(i64) {
if i := int(i64); i64 == int64(i) {
return i
}
return i64
}
if ui64 := uint64(j); j == float64(ui64) {
return ui64
}
return j
case int64:
if i := int(j); j == int64(i) {
return i
}
return j
}
return j
}
Expand Down
39 changes: 28 additions & 11 deletions pkg/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package handler

import (
"io/ioutil"
"math"
"net/http"
"net/http/httptest"
"reflect"
"sort"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/go-openapi/spec"
json "github.com/json-iterator/go"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -98,6 +100,13 @@ func TestRegisterOpenAPIVersionedService(t *testing.T) {
}

func TestJsonToYAML(t *testing.T) {
intOrInt64 := func(i64 int64) interface{} {
if i := int(i64); i64 == int64(i) {
return i
}
return i64
}

tests := []struct {
name string
input map[string]interface{}
Expand All @@ -108,20 +117,28 @@ func TestJsonToYAML(t *testing.T) {
{
"values",
map[string]interface{}{
"int64": int64(42),
"float64": float64(42.0),
"string": string("foo"),
"bool": true,
"slice": []interface{}{"foo", "bar"},
"map": map[string]interface{}{"foo": "bar"},
"bool": true,
"float64": float64(42.1),
"fractionless": float64(42),
"int": int(42),
"int64": int64(42),
"int64 big": float64(math.Pow(2, 62)),
"map": map[string]interface{}{"foo": "bar"},
"slice": []interface{}{"foo", "bar"},
"string": string("foo"),
"uint64 big": float64(math.Pow(2, 63)),
},
yaml.MapSlice{
{"int64", int64(42)},
{"float64", float64(42.0)},
{"string", string("foo")},
{"bool", true},
{"slice", []interface{}{"foo", "bar"}},
{"float64", float64(42.1)},
{"fractionless", int(42)},
{"int", int(42)},
{"int64", int(42)},
{"int64 big", intOrInt64(int64(1) << 62)},
{"map", yaml.MapSlice{{"foo", "bar"}}},
{"slice", []interface{}{"foo", "bar"}},
{"string", string("foo")},
{"uint64 big", uint64(1) << 63},
},
},
}
Expand All @@ -131,7 +148,7 @@ func TestJsonToYAML(t *testing.T) {
sortMapSlicesInPlace(tt.expected)
sortMapSlicesInPlace(got)
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("jsonToYAML() = %v, want %v", got, tt.expected)
t.Errorf("jsonToYAML() = %v, want %v", spew.Sdump(got), spew.Sdump(tt.expected))
}
})
}
Expand Down

0 comments on commit b3a7cee

Please sign in to comment.