Skip to content

Commit 55b06a4

Browse files
authored
Merge pull request #1803 from openshift-cherrypick-robot/cherry-pick-1802-to-release-0.7
[release-0.7] Fix schemacompat npe
2 parents c81a760 + bbf5ac7 commit 55b06a4

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

pkg/schemacompat/schemacompat.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
"go.uber.org/multierr"
2626

27-
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
27+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
2828
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2929
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
3030
"k8s.io/apimachinery/pkg/util/sets"
@@ -361,14 +361,14 @@ func lcdForObject(fldPath *field.Path, existing, new *schema.Structural, lcd *sc
361361
delete(lcd.Properties, removedProperty)
362362
}
363363

364-
} else if new.AdditionalProperties.Structural != nil {
364+
} else if new.AdditionalProperties != nil && new.AdditionalProperties.Structural != nil {
365365
for _, key := range sets.StringKeySet(existing.Properties).List() {
366366
existingPropertySchema := existing.Properties[key]
367367
lcdPropertySchema := lcd.Properties[key]
368368
multierr.AppendInto(&err, lcdForStructural(fldPath.Child("properties").Key(key), &existingPropertySchema, new.AdditionalProperties.Structural, &lcdPropertySchema, narrowExisting))
369369
lcd.Properties[key] = lcdPropertySchema
370370
}
371-
} else if new.AdditionalProperties.Bool {
371+
} else if new.AdditionalProperties != nil && new.AdditionalProperties.Bool {
372372
// that allows named properties only.
373373
// => Keep the existing schemas as the lcd.
374374
} else {

pkg/schemacompat/schemacompat_test.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)