Skip to content

Commit

Permalink
QueryParamVerifierV3 resilient to minimal OpenAPI V3 documents
Browse files Browse the repository at this point in the history
  • Loading branch information
seans3 committed May 10, 2023
1 parent 80b77bf commit 4cd33af
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Expand Up @@ -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
Expand Down
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit 4cd33af

Please sign in to comment.