2020-06-30 14:11:05 +00:00
|
|
|
// Copyright 2020 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-12-30 08:20:58 +00:00
|
|
|
// Package openapi3 provides functions for generating OpenAPI v3 (Swagger) documentation.
|
2020-06-30 14:11:05 +00:00
|
|
|
package openapi3
|
|
|
|
|
|
|
|
import (
|
2022-05-02 14:07:52 +00:00
|
|
|
"fmt"
|
2023-02-18 22:43:26 +00:00
|
|
|
"io"
|
2020-06-30 14:11:05 +00:00
|
|
|
|
|
|
|
gyaml "github.com/ghodss/yaml"
|
|
|
|
|
2022-05-02 14:07:52 +00:00
|
|
|
"errors"
|
2020-06-30 14:11:05 +00:00
|
|
|
|
|
|
|
kopenapi3 "github.com/getkin/kin-openapi/openapi3"
|
|
|
|
"github.com/gohugoio/hugo/cache/namedmemcache"
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"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 {
|
2020-12-23 08:26:23 +00:00
|
|
|
// TODO(bep) consolidate when merging that "other branch" -- but be aware of the keys.
|
2020-06-30 14:11:05 +00:00
|
|
|
cache := namedmemcache.New()
|
|
|
|
deps.BuildStartListeners.Add(
|
|
|
|
func() {
|
|
|
|
cache.Clear()
|
|
|
|
})
|
|
|
|
|
|
|
|
return &Namespace{
|
|
|
|
cache: cache,
|
|
|
|
deps: deps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace provides template functions for the "openapi3".
|
|
|
|
type Namespace struct {
|
|
|
|
cache *namedmemcache.Cache
|
|
|
|
deps *deps.Deps
|
|
|
|
}
|
|
|
|
|
2022-12-13 10:11:14 +00:00
|
|
|
// OpenAPIDocument represents an OpenAPI 3 document.
|
|
|
|
type OpenAPIDocument struct {
|
|
|
|
*kopenapi3.T
|
|
|
|
}
|
|
|
|
|
2022-12-30 08:20:58 +00:00
|
|
|
// Unmarshal unmarshals the given resource into an OpenAPI 3 document.
|
2022-12-13 10:11:14 +00:00
|
|
|
func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*OpenAPIDocument, error) {
|
2020-06-30 14:11:05 +00:00
|
|
|
key := r.Key()
|
|
|
|
if key == "" {
|
|
|
|
return nil, errors.New("no Key set in Resource")
|
|
|
|
}
|
|
|
|
|
2022-03-17 21:03:27 +00:00
|
|
|
v, err := ns.cache.GetOrCreate(key, func() (any, error) {
|
2023-01-04 17:24:36 +00:00
|
|
|
f := metadecoders.FormatFromStrings(r.MediaType().Suffixes()...)
|
2020-06-30 14:11:05 +00:00
|
|
|
if f == "" {
|
2022-05-02 14:07:52 +00:00
|
|
|
return nil, fmt.Errorf("MIME %q not supported", r.MediaType())
|
2020-06-30 14:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := r.ReadSeekCloser()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
2023-02-18 22:43:26 +00:00
|
|
|
b, err := io.ReadAll(reader)
|
2020-06-30 14:11:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:16:23 +00:00
|
|
|
s := &kopenapi3.T{}
|
2020-06-30 14:11:05 +00:00
|
|
|
switch f {
|
|
|
|
case metadecoders.YAML:
|
|
|
|
err = gyaml.Unmarshal(b, s)
|
|
|
|
default:
|
|
|
|
err = metadecoders.Default.UnmarshalTo(b, f, s)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:16:23 +00:00
|
|
|
err = kopenapi3.NewLoader().ResolveRefsIn(s, nil)
|
2020-06-30 14:11:05 +00:00
|
|
|
|
2022-12-13 10:11:14 +00:00
|
|
|
return &OpenAPIDocument{T: s}, err
|
2020-06-30 14:11:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-13 10:11:14 +00:00
|
|
|
return v.(*OpenAPIDocument), nil
|
2020-06-30 14:11:05 +00:00
|
|
|
}
|