diff --git a/pkg/resource/query_param_verifier_v3.go b/pkg/resource/query_param_verifier_v3.go index 82cd024c..050174fd 100644 --- a/pkg/resource/query_param_verifier_v3.go +++ b/pkg/resource/query_param_verifier_v3.go @@ -106,6 +106,9 @@ func hasGVKExtensionV3(extensions spec.Extensions, gvk schema.GroupVersionKind) // the PATCH end-point. Returns true if the query param is supported by the // spec for the passed GVK; false otherwise. func supportsQueryParamV3(doc *spec3.OpenAPI, gvk schema.GroupVersionKind, queryParam VerifiableQueryParam) bool { + if doc == nil || doc.Paths == nil { + return false + } for _, path := range doc.Paths.Paths { // If operation is not PATCH, then continue. op := path.PathProps.Patch diff --git a/pkg/resource/query_param_verifier_v3_test.go b/pkg/resource/query_param_verifier_v3_test.go index 6cbf207c..ad7c606a 100644 --- a/pkg/resource/query_param_verifier_v3_test.go +++ b/pkg/resource/query_param_verifier_v3_test.go @@ -23,6 +23,7 @@ import ( "k8s.io/client-go/openapi/cached" "k8s.io/client-go/openapi/openapitest" "k8s.io/client-go/openapi3" + "k8s.io/kube-openapi/pkg/spec3" ) func TestV3SupportsQueryParamBatchV1(t *testing.T) { @@ -135,3 +136,70 @@ func TestV3SupportsQueryParamBatchV1(t *testing.T) { }) } } + +func TestInvalidOpenAPIV3Document(t *testing.T) { + tests := map[string]struct { + spec *spec3.OpenAPI + }{ + "nil document correctly returns Unsupported error": { + spec: nil, + }, + "empty document correctly returns Unsupported error": { + spec: &spec3.OpenAPI{}, + }, + "minimal document correctly returns Unsupported error": { + spec: &spec3.OpenAPI{ + Version: "openapi 3.0.0", + Paths: nil, + }, + }, + "document with empty Paths correctly returns Unsupported error": { + spec: &spec3.OpenAPI{ + Version: "openapi 3.0.0", + Paths: &spec3.Paths{}, + }, + }, + } + + gvk := schema.GroupVersionKind{ + Group: "batch", + Version: "v1", + Kind: "Job", + } + for tn, tc := range tests { + t.Run(tn, func(t *testing.T) { + + verifier := &queryParamVerifierV3{ + finder: NewCRDFinder(func() ([]schema.GroupKind, error) { + return []schema.GroupKind{}, nil + }), + root: &fakeRoot{tc.spec}, + queryParam: QueryParamFieldValidation, + } + err := verifier.HasSupport(gvk) + if err == nil { + t.Errorf("Expected not supports error, but none received.") + } + }) + } +} + +// fakeRoot implements Root interface; manually specifies the returned OpenAPI V3 document. +type fakeRoot struct { + spec *spec3.OpenAPI +} + +func (f *fakeRoot) GroupVersions() ([]schema.GroupVersion, error) { + // Unused + return nil, nil +} + +// GVSpec returns hard-coded OpenAPI V3 document. +func (f *fakeRoot) GVSpec(gv schema.GroupVersion) (*spec3.OpenAPI, error) { + return f.spec, nil +} + +func (f *fakeRoot) GVSpecAsMap(gv schema.GroupVersion) (map[string]interface{}, error) { + // Unused + return nil, nil +}