summaryrefslogtreecommitdiffstats
path: root/tpl/openapi
diff options
context:
space:
mode:
Diffstat (limited to 'tpl/openapi')
-rw-r--r--tpl/openapi/openapi3/integration_test.go2
-rw-r--r--tpl/openapi/openapi3/openapi3.go28
2 files changed, 14 insertions, 16 deletions
diff --git a/tpl/openapi/openapi3/integration_test.go b/tpl/openapi/openapi3/integration_test.go
index d3be0eda9..6914a60b3 100644
--- a/tpl/openapi/openapi3/integration_test.go
+++ b/tpl/openapi/openapi3/integration_test.go
@@ -67,7 +67,7 @@ API: {{ $api.Info.Title | safeHTML }}
b.AssertFileContent("public/index.html", `API: Sample API`)
b.
- EditFileReplace("assets/api/myapi.yaml", func(s string) string { return strings.ReplaceAll(s, "Sample API", "Hugo API") }).
+ EditFileReplaceFunc("assets/api/myapi.yaml", func(s string) string { return strings.ReplaceAll(s, "Sample API", "Hugo API") }).
Build()
b.AssertFileContent("public/index.html", `API: Hugo API`)
diff --git a/tpl/openapi/openapi3/openapi3.go b/tpl/openapi/openapi3/openapi3.go
index 38857dd98..f929c7f62 100644
--- a/tpl/openapi/openapi3/openapi3.go
+++ b/tpl/openapi/openapi3/openapi3.go
@@ -15,44 +15,42 @@
package openapi3
import (
+ "errors"
"fmt"
"io"
gyaml "github.com/ghodss/yaml"
- "errors"
-
kopenapi3 "github.com/getkin/kin-openapi/openapi3"
- "github.com/gohugoio/hugo/cache/namedmemcache"
+ "github.com/gohugoio/hugo/cache/dynacache"
"github.com/gohugoio/hugo/deps"
+ "github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/parser/metadecoders"
"github.com/gohugoio/hugo/resources/resource"
)
// New returns a new instance of the openapi3-namespaced template functions.
func New(deps *deps.Deps) *Namespace {
- // TODO(bep) consolidate when merging that "other branch" -- but be aware of the keys.
- cache := namedmemcache.New()
- deps.BuildStartListeners.Add(
- func() {
- cache.Clear()
- })
-
return &Namespace{
- cache: cache,
+ cache: dynacache.GetOrCreatePartition[string, *OpenAPIDocument](deps.MemCache, "/tmpl/openapi3", dynacache.OptionsPartition{Weight: 30, ClearWhen: dynacache.ClearOnChange}),
deps: deps,
}
}
// Namespace provides template functions for the "openapi3".
type Namespace struct {
- cache *namedmemcache.Cache
+ cache *dynacache.Partition[string, *OpenAPIDocument]
deps *deps.Deps
}
// OpenAPIDocument represents an OpenAPI 3 document.
type OpenAPIDocument struct {
*kopenapi3.T
+ identityGroup identity.Identity
+}
+
+func (o *OpenAPIDocument) GetIdentityGroup() identity.Identity {
+ return o.identityGroup
}
// Unmarshal unmarshals the given resource into an OpenAPI 3 document.
@@ -62,7 +60,7 @@ func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*OpenAPIDocument
return nil, errors.New("no Key set in Resource")
}
- v, err := ns.cache.GetOrCreate(key, func() (any, error) {
+ v, err := ns.cache.GetOrCreate(key, func(string) (*OpenAPIDocument, error) {
f := metadecoders.FormatFromStrings(r.MediaType().Suffixes()...)
if f == "" {
return nil, fmt.Errorf("MIME %q not supported", r.MediaType())
@@ -92,11 +90,11 @@ func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*OpenAPIDocument
err = kopenapi3.NewLoader().ResolveRefsIn(s, nil)
- return &OpenAPIDocument{T: s}, err
+ return &OpenAPIDocument{T: s, identityGroup: identity.FirstIdentity(r)}, err
})
if err != nil {
return nil, err
}
- return v.(*OpenAPIDocument), nil
+ return v, nil
}