Skip to content

Commit 23bc0b9

Browse files
Merge pull request #55936 from shiywang/json_test_enhance
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Some test enhance, comments enhance and duplicate code reduce while I was learning the codebase, found some place could be enhance, not confident about the comments of `Decode` I changed isn't right. /assign @sttts PTAL, thanks ```release-note NONE ``` Kubernetes-commit: fce605fad1ae9d7bd1464af012ddd0940bdb8616
2 parents a04e753 + ec20490 commit 23bc0b9

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

pkg/runtime/serializer/json/json.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,29 @@ func init() {
9898
jsoniter.RegisterTypeDecoderFunc("interface {}", decodeNumberAsInt64IfPossible)
9999
}
100100

101+
// gvkWithDefaults returns group kind and version defaulting from provided default
102+
func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
103+
if len(actual.Kind) == 0 {
104+
actual.Kind = defaultGVK.Kind
105+
}
106+
if len(actual.Version) == 0 && len(actual.Group) == 0 {
107+
actual.Group = defaultGVK.Group
108+
actual.Version = defaultGVK.Version
109+
}
110+
if len(actual.Version) == 0 && actual.Group == defaultGVK.Group {
111+
actual.Version = defaultGVK.Version
112+
}
113+
return actual
114+
}
115+
101116
// Decode attempts to convert the provided data into YAML or JSON, extract the stored schema kind, apply the provided default gvk, and then
102-
// 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
103-
// extracted and no decoding will be performed. If into is not registered with the typer, then the object will be straight decoded using
104-
// normal JSON/YAML unmarshalling. If into is provided and the original data is not fully qualified with kind/version/group, the type of
105-
// the into will be used to alter the returned gvk. On success or most errors, the method will return the calculated schema kind.
117+
// load that data into an object matching the desired schema kind or the provided into.
118+
// If into is *runtime.Unknown, the raw data will be extracted and no decoding will be performed.
119+
// If into is not registered with the typer, then the object will be straight decoded using normal JSON/YAML unmarshalling.
120+
// If into is provided and the original data is not fully qualified with kind/version/group, the type of the into will be used to alter the returned gvk.
121+
// If into is nil or data's gvk different from into's gvk, it will generate a new Object with ObjectCreater.New(gvk)
122+
// On success or most errors, the method will return the calculated schema kind.
123+
// The gvk calculate priority will be originalData > default gvk > into
106124
func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
107125
if versioned, ok := into.(*runtime.VersionedObjects); ok {
108126
into = versioned.Last()
@@ -129,17 +147,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
129147
}
130148

131149
if gvk != nil {
132-
// apply kind and version defaulting from provided default
133-
if len(actual.Kind) == 0 {
134-
actual.Kind = gvk.Kind
135-
}
136-
if len(actual.Version) == 0 && len(actual.Group) == 0 {
137-
actual.Group = gvk.Group
138-
actual.Version = gvk.Version
139-
}
140-
if len(actual.Version) == 0 && actual.Group == gvk.Group {
141-
actual.Version = gvk.Version
142-
}
150+
*actual = gvkWithDefaults(*actual, *gvk)
143151
}
144152

145153
if unk, ok := into.(*runtime.Unknown); ok && unk != nil {
@@ -161,17 +169,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
161169
case err != nil:
162170
return nil, actual, err
163171
default:
164-
typed := types[0]
165-
if len(actual.Kind) == 0 {
166-
actual.Kind = typed.Kind
167-
}
168-
if len(actual.Version) == 0 && len(actual.Group) == 0 {
169-
actual.Group = typed.Group
170-
actual.Version = typed.Version
171-
}
172-
if len(actual.Version) == 0 && actual.Group == typed.Group {
173-
actual.Version = typed.Version
174-
}
172+
*actual = gvkWithDefaults(*actual, types[0])
175173
}
176174
}
177175

pkg/runtime/serializer/json/json_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ func TestDecode(t *testing.T) {
9494
expectedObject: &testDecodable{},
9595
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
9696
},
97+
// group version, kind is defaulted
98+
{
99+
data: []byte(`{"apiVersion":"other1/blah1"}`),
100+
defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
101+
creater: &mockCreater{obj: &testDecodable{}},
102+
expectedObject: &testDecodable{},
103+
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other1", Version: "blah1"},
104+
},
105+
// gvk all provided then not defaulted at all
106+
{
107+
data: []byte(`{"kind":"Test","apiVersion":"other/blah"}`),
108+
defaultGVK: &schema.GroupVersionKind{Kind: "Test1", Group: "other1", Version: "blah1"},
109+
creater: &mockCreater{obj: &testDecodable{}},
110+
expectedObject: &testDecodable{},
111+
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
112+
},
113+
//gvk defaulting if kind not provided in data and defaultGVK use into's kind
114+
{
115+
data: []byte(`{"apiVersion":"b1/c1"}`),
116+
into: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
117+
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
118+
defaultGVK: nil,
119+
creater: &mockCreater{obj: &testDecodable{}},
120+
expectedObject: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}},
121+
expectedGVK: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"},
122+
},
97123

98124
// accept runtime.Unknown as into and bypass creator
99125
{

0 commit comments

Comments
 (0)