generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-1.2] override unmarshalJSON for supportedfeature to ensure c…
…omptability (#3465) * override json unmarshal for supportedfeature to ensure backward compatability * add issue link --------- Co-authored-by: Lior Lieberman <[email protected]>
- Loading branch information
1 parent
f735045
commit 20b8e4e
Showing
3 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
) | ||
|
||
// Below code handles the experimental field breaking change introduced in | ||
// https://github.com/kubernetes-sigs/gateway-api/pull/3200/. | ||
// We are overriding the UnmarshalJSON function to be able to handle cases where | ||
// users had the old version of the GatewayClass CRD applied with SupportedFeatures | ||
// as a list of strings and not list of objects. | ||
// See https://github.com/kubernetes-sigs/gateway-api/issues/3464 | ||
// for more information. | ||
|
||
func (s *SupportedFeature) UnmarshalJSON(data []byte) error { | ||
var oldSupportedFeature oldSupportedFeature | ||
var unmarshalTypeErr *json.UnmarshalTypeError | ||
if err := json.Unmarshal(data, &oldSupportedFeature); err == nil { | ||
s.Name = FeatureName(oldSupportedFeature) | ||
return nil | ||
} else if !errors.As(err, &unmarshalTypeErr) { | ||
// If the error is not a type error, return it | ||
return err | ||
} | ||
|
||
var si supportedFeatureInternal | ||
if err := json.Unmarshal(data, &si); err != nil { | ||
return err | ||
} | ||
s.Name = si.Name | ||
return nil | ||
} | ||
|
||
// This is solely for the purpose of ensuring backward compatibility and | ||
// SHOULD NOT be used elsewhere. | ||
type supportedFeatureInternal struct { | ||
Name FeatureName `json:"name"` | ||
} | ||
|
||
// This is solely for the purpose of ensuring backward compatibility and | ||
// SHOULD NOT be used elsewhere. | ||
type oldSupportedFeature string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestSupportedFeature_UnmarshalJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected SupportedFeature | ||
expectError bool | ||
}{ | ||
{ | ||
name: "old struct input", | ||
input: `"featureA"`, | ||
expected: SupportedFeature{Name: "featureA"}, | ||
}, | ||
{ | ||
name: "new struct input", | ||
input: `{"name": "featureB"}`, | ||
expected: SupportedFeature{Name: "featureB"}, | ||
}, | ||
{ | ||
name: "expected error", | ||
input: `["featureA", "featureB"]`, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var result SupportedFeature | ||
err := json.Unmarshal([]byte(tt.input), &result) | ||
|
||
if tt.expectError { | ||
if err == nil { | ||
t.Errorf("Expected an error, but got nil") | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if result != tt.expected { | ||
t.Errorf("Expected %+v, got %+v", tt.expected, result) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.