@@ -9,8 +9,11 @@ import (
9
9
"strings"
10
10
"testing"
11
11
12
+ "k8s.io/api/core/v1"
13
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
14
"k8s.io/apimachinery/pkg/runtime"
13
15
"k8s.io/apimachinery/pkg/runtime/schema"
16
+ jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
14
17
"k8s.io/apimachinery/pkg/util/validation/field"
15
18
"k8s.io/kubernetes/pkg/api/legacyscheme"
16
19
api "k8s.io/kubernetes/pkg/apis/core"
@@ -111,6 +114,60 @@ func TestAllowedGrouplessVersion(t *testing.T) {
111
114
}
112
115
}
113
116
117
+ func TestAllowedTypeCoercion (t * testing.T ) {
118
+ ten := int64 (10 )
119
+
120
+ testcases := []struct {
121
+ name string
122
+ input []byte
123
+ into runtime.Object
124
+ expected runtime.Object
125
+ }{
126
+ {
127
+ name : "string to number" ,
128
+ input : []byte (`{
129
+ "kind":"Pod",
130
+ "apiVersion":"v1",
131
+ "spec":{"activeDeadlineSeconds":"10"}
132
+ }` ),
133
+ expected : & v1.Pod {
134
+ TypeMeta : metav1.TypeMeta {Kind : "Pod" , APIVersion : "v1" },
135
+ Spec : v1.PodSpec {ActiveDeadlineSeconds : & ten },
136
+ },
137
+ },
138
+ {
139
+ name : "empty object to array" ,
140
+ input : []byte (`{
141
+ "kind":"Pod",
142
+ "apiVersion":"v1",
143
+ "spec":{"containers":{}}
144
+ }` ),
145
+ expected : & v1.Pod {
146
+ TypeMeta : metav1.TypeMeta {Kind : "Pod" , APIVersion : "v1" },
147
+ Spec : v1.PodSpec {Containers : []v1.Container {}},
148
+ },
149
+ },
150
+ }
151
+
152
+ for i := range testcases {
153
+ func (i int ) {
154
+ tc := testcases [i ]
155
+ t .Run (tc .name , func (t * testing.T ) {
156
+ s := jsonserializer .NewSerializer (jsonserializer .DefaultMetaFactory , legacyscheme .Scheme , legacyscheme .Scheme , false )
157
+ obj , _ , err := s .Decode (tc .input , nil , tc .into )
158
+ if err != nil {
159
+ t .Error (err )
160
+ return
161
+ }
162
+ if ! reflect .DeepEqual (obj , tc .expected ) {
163
+ t .Errorf ("Expected\n %#v\n got\n %#v" , tc .expected , obj )
164
+ return
165
+ }
166
+ })
167
+ }(i )
168
+ }
169
+ }
170
+
114
171
func getJSONValue (data map [string ]interface {}, keys ... string ) (interface {}, bool , error ) {
115
172
// No keys, current value is it
116
173
if len (keys ) == 0 {
0 commit comments