mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +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) {
|
func (item *mapItem) VisitKind(schema *openapi.Kind) {
|
||||||
// Verify each sub-field.
|
// Verify each sub-field.
|
||||||
for _, key := range item.sortedKeys() {
|
for _, key := range item.sortedKeys() {
|
||||||
|
if item.Map[key] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
subItem, err := itemFactory(item.Path().FieldPath(key), item.Map[key])
|
subItem, err := itemFactory(item.Path().FieldPath(key), item.Map[key])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
item.AddError(err)
|
item.AddError(err)
|
||||||
@ -118,7 +121,7 @@ func (item *mapItem) VisitKind(schema *openapi.Kind) {
|
|||||||
|
|
||||||
// Verify that all required fields are present.
|
// Verify that all required fields are present.
|
||||||
for _, required := range schema.RequiredFields {
|
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})
|
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) {
|
func (item *arrayItem) VisitArray(schema *openapi.Array) {
|
||||||
for i, v := range item.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 {
|
if err != nil {
|
||||||
item.AddError(err)
|
item.AddError(err)
|
||||||
continue
|
continue
|
||||||
|
@ -231,14 +231,101 @@ spec:
|
|||||||
`))
|
`))
|
||||||
|
|
||||||
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
Expect(err).To(Equal(utilerrors.NewAggregate([]error{
|
||||||
validation.InvalidObjectTypeError{
|
validation.ValidationError{
|
||||||
Path: "Pod.spec.containers[0].args[0]",
|
Path: "Pod.spec.containers[0].args",
|
||||||
Type: "nil",
|
Err: validation.InvalidObjectTypeError{
|
||||||
|
Path: "Pod.spec.containers[0].args[0]",
|
||||||
|
Type: "nil",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
validation.InvalidObjectTypeError{
|
validation.ValidationError{
|
||||||
Path: "Pod.spec.containers[0].command[0]",
|
Path: "Pod.spec.containers[0].command",
|
||||||
Type: "nil",
|
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