OpenAPI V3 invalid document checks

This commit is contained in:
Sean Sullivan 2023-05-11 14:57:05 -07:00
parent 02659772cb
commit 3da79e2850
2 changed files with 31 additions and 3 deletions

View File

@ -110,6 +110,9 @@ func supportsQueryParamV3(doc *spec3.OpenAPI, gvk schema.GroupVersionKind, query
} }
for _, path := range doc.Paths.Paths { for _, path := range doc.Paths.Paths {
// If operation is not PATCH, then continue. // If operation is not PATCH, then continue.
if path == nil {
continue
}
op := path.PathProps.Patch op := path.PathProps.Patch
if op == nil { if op == nil {
continue continue
@ -127,5 +130,5 @@ func supportsQueryParamV3(doc *spec3.OpenAPI, gvk schema.GroupVersionKind, query
} }
return NewParamUnsupportedError(gvk, queryParam) return NewParamUnsupportedError(gvk, queryParam)
} }
return NewParamUnsupportedError(gvk, queryParam) return fmt.Errorf("Path not found for GVK (%s) in OpenAPI V3 doc", gvk)
} }

View File

@ -141,18 +141,43 @@ func TestV3SupportsQueryParamBatchV1(t *testing.T) {
func TestInvalidOpenAPIV3Document(t *testing.T) { func TestInvalidOpenAPIV3Document(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
spec *spec3.OpenAPI spec *spec3.OpenAPI
err string
}{ }{
"nil document returns error": { "nil document returns error": {
spec: nil, spec: nil,
err: "Invalid OpenAPI V3 document",
}, },
"empty document returns error": { "empty document returns error": {
spec: &spec3.OpenAPI{}, spec: &spec3.OpenAPI{},
err: "Invalid OpenAPI V3 document",
}, },
"minimal document returns error": { "minimal document returns error": {
spec: &spec3.OpenAPI{ spec: &spec3.OpenAPI{
Version: "openapi 3.0.0", Version: "openapi 3.0.0",
Paths: nil, Paths: nil,
}, },
err: "Invalid OpenAPI V3 document",
},
"empty Paths returns error": {
spec: &spec3.OpenAPI{
Version: "openapi 3.0.0",
Paths: &spec3.Paths{},
},
err: "Path not found for GVK",
},
"nil Path returns error": {
spec: &spec3.OpenAPI{
Version: "openapi 3.0.0",
Paths: &spec3.Paths{Paths: map[string]*spec3.Path{"/version": nil}},
},
err: "Path not found for GVK",
},
"empty Path returns error": {
spec: &spec3.OpenAPI{
Version: "openapi 3.0.0",
Paths: &spec3.Paths{Paths: map[string]*spec3.Path{"/version": {}}},
},
err: "Path not found for GVK",
}, },
} }
@ -172,8 +197,8 @@ func TestInvalidOpenAPIV3Document(t *testing.T) {
queryParam: QueryParamFieldValidation, queryParam: QueryParamFieldValidation,
} }
err := verifier.HasSupport(gvk) err := verifier.HasSupport(gvk)
if !strings.Contains(err.Error(), "Invalid OpenAPI V3 document") { if !strings.Contains(err.Error(), tc.err) {
t.Errorf("Expected invalid document error, but none received.") t.Errorf("Expected error (%s), but received (%s)", tc.err, err.Error())
} }
}) })
} }