Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the converts an empty string to nil. #119229

Merged
merged 1 commit into from Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions staging/src/k8s.io/apimachinery/pkg/runtime/converter.go
Expand Up @@ -231,7 +231,7 @@ func (c *fromUnstructuredContext) pushKey(key string) {

}

// FromUnstructuredWIthValidation converts an object from map[string]interface{} representation into a concrete type.
// FromUnstructuredWithValidation converts an object from map[string]interface{} representation into a concrete type.
// It uses encoding/json/Unmarshaler if object implements it or reflection if not.
// It takes a validationDirective that indicates how to behave when it encounters unknown fields.
func (c *unstructuredConverter) FromUnstructuredWithValidation(u map[string]interface{}, obj interface{}, returnUnknownFields bool) error {
Expand Down Expand Up @@ -465,7 +465,7 @@ func sliceFromUnstructured(sv, dv reflect.Value, ctx *fromUnstructuredContext) e
}
dv.SetBytes(data)
} else {
dv.Set(reflect.Zero(dt))
dv.Set(reflect.MakeSlice(dt, 0, 0))
}
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/runtime/converter_test.go
Expand Up @@ -91,6 +91,7 @@ type F struct {
G []int `json:"fg"`
H []bool `json:"fh"`
I []float32 `json:"fi"`
J []byte `json:"fj"`
}

type G struct {
Expand Down Expand Up @@ -751,6 +752,10 @@ func TestUnrecognized(t *testing.T) {
data: "{\"ff\":[\"abc\"],\"fg\":[123],\"fh\":[true,false]}",
obj: &F{},
},
{
data: "{\"fj\":\"\"}",
obj: &F{},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is good enough? Did that test fail to pass without the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is good enough? Did that test fail to pass without the change?

This test will fails without the change, The error message is as follows:

    converter_test.go:691: Object changed, diff:   &runtime_test.F{
          	... // 7 identical fields
          	H: nil,
          	I: nil,
        - 	J: []uint8{},
        + 	J: nil,
          }

{
// Invalid string data
data: "{\"fa\":123}",
Expand Down