diff --git a/openapi/cached/groupversion.go b/openapi/cached/groupversion.go index 65a4189f..73730c51 100644 --- a/openapi/cached/groupversion.go +++ b/openapi/cached/groupversion.go @@ -56,3 +56,7 @@ func (g *groupversion) Schema(contentType string) ([]byte, error) { return cachedInfo.data, cachedInfo.err } + +func (c *groupversion) ServerRelativeURL() string { + return c.delegate.ServerRelativeURL() +} diff --git a/openapi/groupversion.go b/openapi/groupversion.go index 601dcbe3..40d91b9a 100644 --- a/openapi/groupversion.go +++ b/openapi/groupversion.go @@ -27,6 +27,12 @@ const ContentTypeOpenAPIV3PB = "application/com.github.proto-openapi.spec.v3@v1. type GroupVersion interface { Schema(contentType string) ([]byte, error) + + // ServerRelativeURL. Returns the path and parameters used to fetch the schema. + // You should use the Schema method to fetch it, but this value can be used + // to key the current version of the schema in a cache since it contains a + // hash string which changes upon schema update. + ServerRelativeURL() string } type groupversion struct { @@ -68,3 +74,9 @@ func (g *groupversion) Schema(contentType string) ([]byte, error) { return path.Do(context.TODO()).Raw() } + +// URL used for fetching the schema. The URL includes a hash and can be used +// to key the current version of the schema in a cache. +func (g *groupversion) ServerRelativeURL() string { + return g.item.ServerRelativeURL +} diff --git a/openapi/openapitest/fakeclient.go b/openapi/openapitest/fakeclient.go index ec3d8432..43b5f679 100644 --- a/openapi/openapitest/fakeclient.go +++ b/openapi/openapitest/fakeclient.go @@ -77,3 +77,8 @@ func (f FakeGroupVersion) Schema(contentType string) ([]byte, error) { } return f.GVSpec, nil } + +// ServerRelativeURL returns an empty string. +func (f FakeGroupVersion) ServerRelativeURL() string { + panic("unimplemented") +} diff --git a/openapi/openapitest/fileclient.go b/openapi/openapitest/fileclient.go index d53f63a1..96b882de 100644 --- a/openapi/openapitest/fileclient.go +++ b/openapi/openapitest/fileclient.go @@ -94,3 +94,8 @@ func (f *fileGroupVersion) Schema(contentType string) ([]byte, error) { } return fs.ReadFile(f.f, f.filename) } + +// ServerRelativeURL returns an empty string. +func (f *fileGroupVersion) ServerRelativeURL() string { + return f.filename +}