Skip to content

Commit 962b35e

Browse files
override unmarshalJSON for supportedfeature to ensure comptability (#3454)
* override json unmarshal for supportedfeature to ensure backward compatability * add issue link
1 parent ee81237 commit 962b35e

File tree

3 files changed

+149
-2
lines changed

3 files changed

+149
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
"encoding/json"
21+
"errors"
22+
)
23+
24+
// Below code handles the experimental field breaking change introduced in
25+
// https://github.com/kubernetes-sigs/gateway-api/pull/3200/.
26+
// We are overriding the UnmarshalJSON function to be able to handle cases where
27+
// users had the old version of the GatewayClass CRD applied with SupportedFeatures
28+
// as a list of strings and not list of objects.
29+
// See https://github.com/kubernetes-sigs/gateway-api/issues/3464
30+
// for more information.
31+
32+
func (s *SupportedFeature) UnmarshalJSON(data []byte) error {
33+
var oldSupportedFeature oldSupportedFeature
34+
var unmarshalTypeErr *json.UnmarshalTypeError
35+
if err := json.Unmarshal(data, &oldSupportedFeature); err == nil {
36+
s.Name = FeatureName(oldSupportedFeature)
37+
return nil
38+
} else if !errors.As(err, &unmarshalTypeErr) {
39+
// If the error is not a type error, return it
40+
return err
41+
}
42+
43+
var si supportedFeatureInternal
44+
if err := json.Unmarshal(data, &si); err != nil {
45+
return err
46+
}
47+
s.Name = si.Name
48+
return nil
49+
}
50+
51+
// This is solely for the purpose of ensuring backward compatibility and
52+
// SHOULD NOT be used elsewhere.
53+
type supportedFeatureInternal struct {
54+
Name FeatureName `json:"name"`
55+
}
56+
57+
// This is solely for the purpose of ensuring backward compatibility and
58+
// SHOULD NOT be used elsewhere.
59+
type oldSupportedFeature string

apis/v1/gatewayclass_types_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import (
20+
"encoding/json"
21+
"testing"
22+
)
23+
24+
func TestSupportedFeature_UnmarshalJSON(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
input string
28+
expected SupportedFeature
29+
expectError bool
30+
}{
31+
{
32+
name: "old struct input",
33+
input: `"featureA"`,
34+
expected: SupportedFeature{Name: "featureA"},
35+
},
36+
{
37+
name: "new struct input",
38+
input: `{"name": "featureB"}`,
39+
expected: SupportedFeature{Name: "featureB"},
40+
},
41+
{
42+
name: "expected error",
43+
input: `["featureA", "featureB"]`,
44+
expectError: true,
45+
},
46+
}
47+
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
var result SupportedFeature
51+
err := json.Unmarshal([]byte(tt.input), &result)
52+
53+
if tt.expectError {
54+
if err == nil {
55+
t.Errorf("Expected an error, but got nil")
56+
}
57+
} else {
58+
if err != nil {
59+
t.Errorf("Unexpected error: %v", err)
60+
}
61+
if result != tt.expected {
62+
t.Errorf("Expected %+v, got %+v", tt.expected, result)
63+
}
64+
}
65+
})
66+
}
67+
}

pkg/generated/openapi/zz_generated.openapi.go

Lines changed: 23 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)