@@ -20,6 +20,7 @@ import (
2020 "testing"
2121
2222 "github.com/google/go-cmp/cmp"
23+ "go.uber.org/multierr"
2324
2425 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2526 "k8s.io/apimachinery/pkg/util/validation/field"
@@ -440,12 +441,52 @@ func TestCompatibility(t *testing.T) {
440441 },
441442 },
442443 },
444+ }, {
445+ desc : "existing has properties, new has neither properties or additionalProperties" ,
446+ existing : & apiextensionsv1.JSONSchemaProps {
447+ Type : "object" ,
448+ Properties : map [string ]apiextensionsv1.JSONSchemaProps {
449+ "existing" : {Type : "boolean" },
450+ },
451+ },
452+ new : & apiextensionsv1.JSONSchemaProps {
453+ Type : "array" ,
454+ Items : & apiextensionsv1.JSONSchemaPropsOrArray {
455+ Schema : & apiextensionsv1.JSONSchemaProps {
456+ Type : "object" ,
457+ Properties : map [string ]apiextensionsv1.JSONSchemaProps {
458+ "existing" : {Type : "integer" },
459+ },
460+ },
461+ },
462+ },
463+ wantErr : multierr .Append (
464+ field .Invalid (
465+ field .NewPath ("schema" , "openAPISchema" ).Child ("type" ),
466+ "array" ,
467+ `The type changed (was "object", now "array")` ,
468+ ),
469+ field .Invalid (
470+ field .NewPath ("schema" , "openAPISchema" ).Child ("properties" ),
471+ []string {"existing" },
472+ "properties value has been completely cleared in an incompatible way" ,
473+ ),
474+ ),
443475 }} {
444476 t .Run (c .desc , func (t * testing.T ) {
445477 gotLCD , err := EnsureStructuralSchemaCompatibility (field .NewPath ("schema" , "openAPISchema" ), c .existing , c .new , c .narrowExisting )
446- if d := cmp .Diff (c .wantErr , err ); d != "" {
447- t .Errorf ("Error Diff(-want,+got): %s" , d )
478+ if c .wantErr != nil {
479+ if err == nil {
480+ t .Fatalf ("expected err %v but got nil" , c .wantErr )
481+ }
482+
483+ if d := cmp .Diff (c .wantErr .Error (), err .Error ()); d != "" {
484+ t .Errorf ("Error Diff(-want,+got): %s" , d )
485+ }
486+ } else if err != nil {
487+ t .Fatalf ("unexpected err %v" , err )
448488 }
489+
449490 if d := cmp .Diff (c .wantLCD , gotLCD ); d != "" {
450491 t .Errorf ("LCD Diff(-want,+got): %s" , d )
451492 }
0 commit comments