mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
openapi: Handle properly empty/null fileds
This commit is contained in:
parent
aa1c79f32b
commit
4e87d2d572
@ -103,6 +103,9 @@ func (item *mapItem) VisitMap(schema *openapi.Map) {
|
||||
func (item *mapItem) VisitKind(schema *openapi.Kind) {
|
||||
// Verify each sub-field.
|
||||
for _, key := range item.sortedKeys() {
|
||||
if item.Map[key] == nil {
|
||||
continue
|
||||
}
|
||||
subItem, err := itemFactory(item.Path().FieldPath(key), item.Map[key])
|
||||
if err != nil {
|
||||
item.AddError(err)
|
||||
@ -118,7 +121,7 @@ func (item *mapItem) VisitKind(schema *openapi.Kind) {
|
||||
|
||||
// Verify that all required fields are present.
|
||||
for _, required := range schema.RequiredFields {
|
||||
if _, ok := item.Map[required]; !ok {
|
||||
if v, ok := item.Map[required]; !ok || v == nil {
|
||||
item.AddValidationError(MissingRequiredFieldError{Path: schema.GetPath().String(), Field: required})
|
||||
}
|
||||
}
|
||||
@ -139,7 +142,12 @@ func (item *arrayItem) VisitPrimitive(schema *openapi.Primitive) {
|
||||
|
||||
func (item *arrayItem) VisitArray(schema *openapi.Array) {
|
||||
for i, v := range item.Array {
|
||||
subItem, err := itemFactory(item.Path().ArrayPath(i), v)
|
||||
path := item.Path().ArrayPath(i)
|
||||
if v == nil {
|
||||
item.AddValidationError(InvalidObjectTypeError{Type: "nil", Path: path.String()})
|
||||
continue
|
||||
}
|
||||
subItem, err := itemFactory(path, v)
|
||||
if err != nil {
|
||||
item.AddError(err)
|
||||
continue
|
||||
|
@ -231,14 +231,101 @@ spec:
|
||||
`))
|
||||
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.InvalidObjectTypeError{
|
||||
Path: "Pod.spec.containers[0].args[0]",
|
||||
Type: "nil",
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0].args",
|
||||
Err: validation.InvalidObjectTypeError{
|
||||
Path: "Pod.spec.containers[0].args[0]",
|
||||
Type: "nil",
|
||||
},
|
||||
},
|
||||
validation.InvalidObjectTypeError{
|
||||
Path: "Pod.spec.containers[0].command[0]",
|
||||
Type: "nil",
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0].command",
|
||||
Err: validation.InvalidObjectTypeError{
|
||||
Path: "Pod.spec.containers[0].command[0]",
|
||||
Type: "nil",
|
||||
},
|
||||
},
|
||||
})))
|
||||
})
|
||||
|
||||
It("fails if required fields are missing", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- command: ["my", "command"]
|
||||
`))
|
||||
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0]",
|
||||
Err: validation.MissingRequiredFieldError{
|
||||
Path: "io.k8s.api.core.v1.Container",
|
||||
Field: "name",
|
||||
},
|
||||
},
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0]",
|
||||
Err: validation.MissingRequiredFieldError{
|
||||
Path: "io.k8s.api.core.v1.Container",
|
||||
Field: "image",
|
||||
},
|
||||
},
|
||||
})))
|
||||
})
|
||||
|
||||
It("fails if required fields are empty", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- image:
|
||||
name:
|
||||
`))
|
||||
|
||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0]",
|
||||
Err: validation.MissingRequiredFieldError{
|
||||
Path: "io.k8s.api.core.v1.Container",
|
||||
Field: "name",
|
||||
},
|
||||
},
|
||||
validation.ValidationError{
|
||||
Path: "Pod.spec.containers[0]",
|
||||
Err: validation.MissingRequiredFieldError{
|
||||
Path: "io.k8s.api.core.v1.Container",
|
||||
Field: "image",
|
||||
},
|
||||
},
|
||||
})))
|
||||
})
|
||||
|
||||
It("is fine with empty non-mandatory fields", func() {
|
||||
err := validator.ValidateBytes([]byte(`
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
labels:
|
||||
name: redis-master
|
||||
name: name
|
||||
spec:
|
||||
containers:
|
||||
- image: image
|
||||
name: name
|
||||
command:
|
||||
`))
|
||||
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user