Merge pull request #42253 from liggitt/nil-invalid-field-error

Automatic merge from submit-queue (batch tested with PRs 42128, 42064, 42253, 42309, 42322)

Fix panic on nil invalid field error

bug fix for validation panic

if a field.Invalid is constructed with a nil badvalue, the Error() method panics, since reflect.TypeOf() returns nil
pull/6/head
Kubernetes Submit Queue 2017-03-02 05:00:52 -08:00 committed by GitHub
commit 3d868401e2
2 changed files with 12 additions and 1 deletions

View File

@ -50,7 +50,10 @@ func (v *Error) ErrorBody() string {
s = fmt.Sprintf("%s", v.Type)
default:
value := v.BadValue
if reflect.TypeOf(value).Kind() == reflect.Ptr {
valueType := reflect.TypeOf(value)
if value == nil || valueType == nil {
value = "null"
} else if valueType.Kind() == reflect.Ptr {
if reflectValue := reflect.ValueOf(value); reflectValue.IsNil() {
value = "null"
} else {

View File

@ -62,6 +62,14 @@ func TestMakeFuncs(t *testing.T) {
}
func TestErrorUsefulMessage(t *testing.T) {
{
s := Invalid(nil, nil, "").Error()
t.Logf("message: %v", s)
if !strings.Contains(s, "null") {
t.Errorf("error message did not contain 'null': %s", s)
}
}
s := Invalid(NewPath("foo"), "bar", "deet").Error()
t.Logf("message: %v", s)
for _, part := range []string{"foo", "bar", "deet", ErrorTypeInvalid.String()} {