forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-openapi/spec/schema_loader.go

544 lines
9.0 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2015 go-swagger maintainers
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-05-14 13:07:09 +00:00
// you may not use this file except in compliance with the License.
2024-05-14 13:07:09 +00:00
// You may obtain a copy of the License at
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-05-14 13:07:09 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-05-14 13:07:09 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-05-14 13:07:09 +00:00
// See the License for the specific language governing permissions and
2024-05-14 13:07:09 +00:00
// limitations under the License.
package spec
import (
"encoding/json"
"fmt"
"log"
"net/url"
"reflect"
"strings"
"github.com/go-openapi/swag"
)
// PathLoader is a function to use when loading remote refs.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// This is a package level default. It may be overridden or bypassed by
2024-05-14 13:07:09 +00:00
// specifying the loader in ExpandOptions.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// NOTE: if you are using the go-openapi/loads package, it will override
2024-05-14 13:07:09 +00:00
// this value with its own default (a loader to retrieve YAML documents as
2024-05-14 13:07:09 +00:00
// well as JSON ones).
2024-05-14 13:07:09 +00:00
var PathLoader = func(pth string) (json.RawMessage, error) {
2024-05-14 13:07:09 +00:00
data, err := swag.LoadFromFileOrHTTP(pth)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return json.RawMessage(data), nil
2024-05-14 13:07:09 +00:00
}
// resolverContext allows to share a context during spec processing.
2024-05-14 13:07:09 +00:00
// At the moment, it just holds the index of circular references found.
2024-05-14 13:07:09 +00:00
type resolverContext struct {
2024-05-14 13:07:09 +00:00
// circulars holds all visited circular references, to shortcircuit $ref resolution.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// This structure is privately instantiated and needs not be locked against
2024-05-14 13:07:09 +00:00
// concurrent access, unless we chose to implement a parallel spec walking.
2024-05-14 13:07:09 +00:00
circulars map[string]bool
basePath string
loadDoc func(string) (json.RawMessage, error)
rootID string
2024-05-14 13:07:09 +00:00
}
func newResolverContext(options *ExpandOptions) *resolverContext {
2024-05-14 13:07:09 +00:00
expandOptions := optionsOrDefault(options)
// path loader may be overridden by options
2024-05-14 13:07:09 +00:00
var loader func(string) (json.RawMessage, error)
2024-05-14 13:07:09 +00:00
if expandOptions.PathLoader == nil {
2024-05-14 13:07:09 +00:00
loader = PathLoader
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
loader = expandOptions.PathLoader
2024-05-14 13:07:09 +00:00
}
return &resolverContext{
2024-05-14 13:07:09 +00:00
circulars: make(map[string]bool),
basePath: expandOptions.RelativeBase, // keep the root base path in context
loadDoc: loader,
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
type schemaLoader struct {
root interface{}
2024-05-14 13:07:09 +00:00
options *ExpandOptions
cache ResolutionCache
2024-05-14 13:07:09 +00:00
context *resolverContext
}
func (r *schemaLoader) transitiveResolver(basePath string, ref Ref) *schemaLoader {
2024-05-14 13:07:09 +00:00
if ref.IsRoot() || ref.HasFragmentOnly {
2024-05-14 13:07:09 +00:00
return r
2024-05-14 13:07:09 +00:00
}
baseRef := MustCreateRef(basePath)
2024-05-14 13:07:09 +00:00
currentRef := normalizeRef(&ref, basePath)
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(currentRef.String(), baseRef.String()) {
2024-05-14 13:07:09 +00:00
return r
2024-05-14 13:07:09 +00:00
}
// set a new root against which to resolve
2024-05-14 13:07:09 +00:00
rootURL := currentRef.GetURL()
2024-05-14 13:07:09 +00:00
rootURL.Fragment = ""
2024-05-14 13:07:09 +00:00
root, _ := r.cache.Get(rootURL.String())
// shallow copy of resolver options to set a new RelativeBase when
2024-05-14 13:07:09 +00:00
// traversing multiple documents
2024-05-14 13:07:09 +00:00
newOptions := r.options
2024-05-14 13:07:09 +00:00
newOptions.RelativeBase = rootURL.String()
return defaultSchemaLoader(root, newOptions, r.cache, r.context)
2024-05-14 13:07:09 +00:00
}
func (r *schemaLoader) updateBasePath(transitive *schemaLoader, basePath string) string {
2024-05-14 13:07:09 +00:00
if transitive != r {
2024-05-14 13:07:09 +00:00
if transitive.options != nil && transitive.options.RelativeBase != "" {
2024-05-14 13:07:09 +00:00
return normalizeBase(transitive.options.RelativeBase)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
return basePath
2024-05-14 13:07:09 +00:00
}
func (r *schemaLoader) resolveRef(ref *Ref, target interface{}, basePath string) error {
2024-05-14 13:07:09 +00:00
tgt := reflect.ValueOf(target)
2024-05-14 13:07:09 +00:00
if tgt.Kind() != reflect.Ptr {
2024-05-14 13:07:09 +00:00
return ErrResolveRefNeedsAPointer
2024-05-14 13:07:09 +00:00
}
if ref.GetURL() == nil {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
var (
res interface{}
2024-05-14 13:07:09 +00:00
data interface{}
err error
2024-05-14 13:07:09 +00:00
)
// Resolve against the root if it isn't nil, and if ref is pointing at the root, or has a fragment only which means
2024-05-14 13:07:09 +00:00
// it is pointing somewhere in the root.
2024-05-14 13:07:09 +00:00
root := r.root
2024-05-14 13:07:09 +00:00
if (ref.IsRoot() || ref.HasFragmentOnly) && root == nil && basePath != "" {
2024-05-14 13:07:09 +00:00
if baseRef, erb := NewRef(basePath); erb == nil {
2024-05-14 13:07:09 +00:00
root, _, _, _ = r.load(baseRef.GetURL())
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
if (ref.IsRoot() || ref.HasFragmentOnly) && root != nil {
2024-05-14 13:07:09 +00:00
data = root
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
baseRef := normalizeRef(ref, basePath)
2024-05-14 13:07:09 +00:00
data, _, _, err = r.load(baseRef.GetURL())
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
res = data
2024-05-14 13:07:09 +00:00
if ref.String() != "" {
2024-05-14 13:07:09 +00:00
res, _, err = ref.GetPointer().Get(data)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return swag.DynamicJSONToStruct(res, target)
2024-05-14 13:07:09 +00:00
}
func (r *schemaLoader) load(refURL *url.URL) (interface{}, url.URL, bool, error) {
2024-05-14 13:07:09 +00:00
debugLog("loading schema from url: %s", refURL)
2024-05-14 13:07:09 +00:00
toFetch := *refURL
2024-05-14 13:07:09 +00:00
toFetch.Fragment = ""
var err error
2024-05-14 13:07:09 +00:00
pth := toFetch.String()
2024-05-14 13:07:09 +00:00
normalized := normalizeBase(pth)
2024-05-14 13:07:09 +00:00
debugLog("loading doc from: %s", normalized)
data, fromCache := r.cache.Get(normalized)
2024-05-14 13:07:09 +00:00
if fromCache {
2024-05-14 13:07:09 +00:00
return data, toFetch, fromCache, nil
2024-05-14 13:07:09 +00:00
}
b, err := r.context.loadDoc(normalized)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, url.URL{}, false, err
2024-05-14 13:07:09 +00:00
}
var doc interface{}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(b, &doc); err != nil {
2024-05-14 13:07:09 +00:00
return nil, url.URL{}, false, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r.cache.Set(normalized, doc)
return doc, toFetch, fromCache, nil
2024-05-14 13:07:09 +00:00
}
// isCircular detects cycles in sequences of $ref.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// It relies on a private context (which needs not be locked).
2024-05-14 13:07:09 +00:00
func (r *schemaLoader) isCircular(ref *Ref, basePath string, parentRefs ...string) (foundCycle bool) {
2024-05-14 13:07:09 +00:00
normalizedRef := normalizeURI(ref.String(), basePath)
2024-05-14 13:07:09 +00:00
if _, ok := r.context.circulars[normalizedRef]; ok {
2024-05-14 13:07:09 +00:00
// circular $ref has been already detected in another explored cycle
2024-05-14 13:07:09 +00:00
foundCycle = true
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
foundCycle = swag.ContainsStrings(parentRefs, normalizedRef) // normalized windows url's are lower cased
2024-05-14 13:07:09 +00:00
if foundCycle {
2024-05-14 13:07:09 +00:00
r.context.circulars[normalizedRef] = true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
// Resolve resolves a reference against basePath and stores the result in target.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Resolve is not in charge of following references: it only resolves ref by following its URL.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// If the schema the ref is referring to holds nested refs, Resolve doesn't resolve them.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// If basePath is an empty string, ref is resolved against the root schema stored in the schemaLoader struct
2024-05-14 13:07:09 +00:00
func (r *schemaLoader) Resolve(ref *Ref, target interface{}, basePath string) error {
2024-05-14 13:07:09 +00:00
return r.resolveRef(ref, target, basePath)
2024-05-14 13:07:09 +00:00
}
func (r *schemaLoader) deref(input interface{}, parentRefs []string, basePath string) error {
2024-05-14 13:07:09 +00:00
var ref *Ref
2024-05-14 13:07:09 +00:00
switch refable := input.(type) {
2024-05-14 13:07:09 +00:00
case *Schema:
2024-05-14 13:07:09 +00:00
ref = &refable.Ref
2024-05-14 13:07:09 +00:00
case *Parameter:
2024-05-14 13:07:09 +00:00
ref = &refable.Ref
2024-05-14 13:07:09 +00:00
case *Response:
2024-05-14 13:07:09 +00:00
ref = &refable.Ref
2024-05-14 13:07:09 +00:00
case *PathItem:
2024-05-14 13:07:09 +00:00
ref = &refable.Ref
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
return fmt.Errorf("unsupported type: %T: %w", input, ErrDerefUnsupportedType)
2024-05-14 13:07:09 +00:00
}
curRef := ref.String()
2024-05-14 13:07:09 +00:00
if curRef == "" {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
normalizedRef := normalizeRef(ref, basePath)
2024-05-14 13:07:09 +00:00
normalizedBasePath := normalizedRef.RemoteURI()
if r.isCircular(normalizedRef, basePath, parentRefs...) {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
if err := r.resolveRef(ref, input, basePath); r.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
if ref.String() == "" || ref.String() == curRef {
2024-05-14 13:07:09 +00:00
// done with rereferencing
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
parentRefs = append(parentRefs, normalizedRef.String())
2024-05-14 13:07:09 +00:00
return r.deref(input, parentRefs, normalizedBasePath)
2024-05-14 13:07:09 +00:00
}
func (r *schemaLoader) shouldStopOnError(err error) bool {
2024-05-14 13:07:09 +00:00
if err != nil && !r.options.ContinueOnError {
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
if err != nil {
2024-05-14 13:07:09 +00:00
log.Println(err)
2024-05-14 13:07:09 +00:00
}
return false
2024-05-14 13:07:09 +00:00
}
func (r *schemaLoader) setSchemaID(target interface{}, id, basePath string) (string, string) {
2024-05-14 13:07:09 +00:00
debugLog("schema has ID: %s", id)
// handling the case when id is a folder
2024-05-14 13:07:09 +00:00
// remember that basePath has to point to a file
2024-05-14 13:07:09 +00:00
var refPath string
2024-05-14 13:07:09 +00:00
if strings.HasSuffix(id, "/") {
2024-05-14 13:07:09 +00:00
// ensure this is detected as a file, not a folder
2024-05-14 13:07:09 +00:00
refPath = fmt.Sprintf("%s%s", id, "placeholder.json")
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
refPath = id
2024-05-14 13:07:09 +00:00
}
// updates the current base path
2024-05-14 13:07:09 +00:00
// * important: ID can be a relative path
2024-05-14 13:07:09 +00:00
// * registers target to be fetchable from the new base proposed by this id
2024-05-14 13:07:09 +00:00
newBasePath := normalizeURI(refPath, basePath)
// store found IDs for possible future reuse in $ref
2024-05-14 13:07:09 +00:00
r.cache.Set(newBasePath, target)
// the root document has an ID: all $ref relative to that ID may
2024-05-14 13:07:09 +00:00
// be rebased relative to the root document
2024-05-14 13:07:09 +00:00
if basePath == r.context.basePath {
2024-05-14 13:07:09 +00:00
debugLog("root document is a schema with ID: %s (normalized as:%s)", id, newBasePath)
2024-05-14 13:07:09 +00:00
r.context.rootID = newBasePath
2024-05-14 13:07:09 +00:00
}
return newBasePath, refPath
2024-05-14 13:07:09 +00:00
}
func defaultSchemaLoader(
2024-05-14 13:07:09 +00:00
root interface{},
2024-05-14 13:07:09 +00:00
expandOptions *ExpandOptions,
2024-05-14 13:07:09 +00:00
cache ResolutionCache,
2024-05-14 13:07:09 +00:00
context *resolverContext) *schemaLoader {
if expandOptions == nil {
2024-05-14 13:07:09 +00:00
expandOptions = &ExpandOptions{}
2024-05-14 13:07:09 +00:00
}
cache = cacheOrDefault(cache)
if expandOptions.RelativeBase == "" {
2024-05-14 13:07:09 +00:00
// if no relative base is provided, assume the root document
2024-05-14 13:07:09 +00:00
// contains all $ref, or at least, that the relative documents
2024-05-14 13:07:09 +00:00
// may be resolved from the current working directory.
2024-05-14 13:07:09 +00:00
expandOptions.RelativeBase = baseForRoot(root, cache)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
debugLog("effective expander options: %#v", expandOptions)
if context == nil {
2024-05-14 13:07:09 +00:00
context = newResolverContext(expandOptions)
2024-05-14 13:07:09 +00:00
}
return &schemaLoader{
root: root,
2024-05-14 13:07:09 +00:00
options: expandOptions,
cache: cache,
2024-05-14 13:07:09 +00:00
context: context,
}
2024-05-14 13:07:09 +00:00
}