diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm.go index b2ea74aec0b..164105c9408 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm.go @@ -95,8 +95,8 @@ func skipPrune(x interface{}, s *structuralschema.Structural) { } if prop, ok := s.Properties[k]; ok { prune(v, &prop) - } else { - skipPrune(v, nil) + } else if s.AdditionalProperties != nil { + prune(v, s.AdditionalProperties.Structural) } } case []interface{}: diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go index bb1c494fb27..755028cce49 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning/algorithm_test.go @@ -84,6 +84,12 @@ func TestPrune(t *testing.T) { "unspecifiedObject": {"unspecified": "bar"}, "pruning": {"unspecified": "bar"}, "preserving": {"unspecified": "bar"} + }, + "preservingAdditionalProperties": { + "foo": { + "specified": {"unspecified":"bar"}, + "unspecified": "bar" + } } } `, schema: &structuralschema.Structural{ @@ -117,6 +123,20 @@ func TestPrune(t *testing.T) { }, }, }, + "preservingAdditionalProperties": { + Extensions: structuralschema.Extensions{XPreserveUnknownFields: true}, + Generic: structuralschema.Generic{ + Type: "object", + AdditionalProperties: &structuralschema.StructuralOrBool{ + Structural: &structuralschema.Structural{ + Generic: structuralschema.Generic{Type: "object"}, + Properties: map[string]structuralschema.Structural{ + "specified": {Generic: structuralschema.Generic{Type: "object"}}, + }, + }, + }, + }, + }, }, }, expected: ` { @@ -133,6 +153,11 @@ func TestPrune(t *testing.T) { "unspecifiedObject": {"unspecified": "bar"}, "pruning": {}, "preserving": {"unspecified": "bar"} + }, + "preservingAdditionalProperties": { + "foo": { + "specified": {} + } } } `},