diff --git a/openapi3/root.go b/openapi3/root.go index 7fe8386d..4333e862 100644 --- a/openapi3/root.go +++ b/openapi3/root.go @@ -125,7 +125,7 @@ func (r *root) retrieveGVBytes(gv schema.GroupVersion) ([]byte, error) { apiPath := gvToAPIPath(gv) gvOpenAPI, found := paths[apiPath] if !found { - return nil, fmt.Errorf("GroupVersion (%s) not found in OpenAPI V3 root document", gv) + return nil, &GroupVersionNotFoundError{gv: gv} } return gvOpenAPI.Schema(runtime.ContentTypeJSON) } @@ -170,3 +170,13 @@ func pathToGroupVersion(path string) (schema.GroupVersion, error) { } return gv, nil } + +// Encapsulates GroupVersion not found as one of the paths +// at OpenAPI V3 endpoint. +type GroupVersionNotFoundError struct { + gv schema.GroupVersion +} + +func (r *GroupVersionNotFoundError) Error() string { + return fmt.Sprintf("GroupVersion (%v) not found as OpenAPI V3 path", r.gv) +} diff --git a/openapi3/root_test.go b/openapi3/root_test.go index b36c7c3c..004674e9 100644 --- a/openapi3/root_test.go +++ b/openapi3/root_test.go @@ -113,7 +113,7 @@ func TestOpenAPIV3Root_GVSpec(t *testing.T) { name string gv schema.GroupVersion expectedPaths []string - err bool + err error }{ { name: "OpenAPI V3 for apps/v1 works", @@ -144,7 +144,7 @@ func TestOpenAPIV3Root_GVSpec(t *testing.T) { { name: "OpenAPI V3 spec not found", gv: schema.GroupVersion{Group: "not", Version: "found"}, - err: true, + err: &GroupVersionNotFoundError{gv: schema.GroupVersion{Group: "not", Version: "found"}}, }, } @@ -153,8 +153,8 @@ func TestOpenAPIV3Root_GVSpec(t *testing.T) { client := openapitest.NewFileClient(t) root := NewRoot(client) gvSpec, err := root.GVSpec(test.gv) - if test.err { - require.Error(t, err) + if test.err != nil { + assert.True(t, reflect.DeepEqual(test.err, err)) return } require.NoError(t, err) @@ -172,7 +172,7 @@ func TestOpenAPIV3Root_GVSpecAsMap(t *testing.T) { name string gv schema.GroupVersion expectedPaths []string - err bool + err error }{ { name: "OpenAPI V3 for apps/v1 works", @@ -203,7 +203,7 @@ func TestOpenAPIV3Root_GVSpecAsMap(t *testing.T) { { name: "OpenAPI V3 spec not found", gv: schema.GroupVersion{Group: "not", Version: "found"}, - err: true, + err: &GroupVersionNotFoundError{gv: schema.GroupVersion{Group: "not", Version: "found"}}, }, } @@ -212,8 +212,8 @@ func TestOpenAPIV3Root_GVSpecAsMap(t *testing.T) { client := openapitest.NewFileClient(t) root := NewRoot(client) gvSpecAsMap, err := root.GVSpecAsMap(test.gv) - if test.err { - require.Error(t, err) + if test.err != nil { + assert.True(t, reflect.DeepEqual(test.err, err)) return } require.NoError(t, err)