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

1018 lines
17 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"
)
// ExpandOptions provides options for the spec expander.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// RelativeBase is the path to the root document. This can be a remote URL or a path to a local file.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// If left empty, the root document is assumed to be located in the current working directory:
2024-05-14 13:07:09 +00:00
// all relative $ref's will be resolved from there.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// PathLoader injects a document loading method. By default, this resolves to the function provided by the SpecLoader package variable.
2024-05-14 13:07:09 +00:00
type ExpandOptions struct {
RelativeBase string // the path to the root document to expand. This is a file, not a directory
SkipSchemas bool // do not expand schemas, just paths, parameters and responses
ContinueOnError bool // continue expanding even after and error is found
PathLoader func(string) (json.RawMessage, error) `json:"-"` // the document loading method that takes a path as input and yields a json document
AbsoluteCircularRef bool // circular $ref remaining after expansion remain absolute URLs
2024-05-14 13:07:09 +00:00
}
func optionsOrDefault(opts *ExpandOptions) *ExpandOptions {
2024-05-14 13:07:09 +00:00
if opts != nil {
2024-05-14 13:07:09 +00:00
clone := *opts // shallow clone to avoid internal changes to be propagated to the caller
2024-05-14 13:07:09 +00:00
if clone.RelativeBase != "" {
2024-05-14 13:07:09 +00:00
clone.RelativeBase = normalizeBase(clone.RelativeBase)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
// if the relative base is empty, let the schema loader choose a pseudo root document
2024-05-14 13:07:09 +00:00
return &clone
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return &ExpandOptions{}
2024-05-14 13:07:09 +00:00
}
// ExpandSpec expands the references in a swagger spec
2024-05-14 13:07:09 +00:00
func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
2024-05-14 13:07:09 +00:00
options = optionsOrDefault(options)
2024-05-14 13:07:09 +00:00
resolver := defaultSchemaLoader(spec, options, nil, nil)
specBasePath := options.RelativeBase
if !options.SkipSchemas {
2024-05-14 13:07:09 +00:00
for key, definition := range spec.Definitions {
2024-05-14 13:07:09 +00:00
parentRefs := make([]string, 0, 10)
2024-05-14 13:07:09 +00:00
parentRefs = append(parentRefs, "#/definitions/"+key)
def, err := expandSchema(definition, parentRefs, resolver, specBasePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
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
if def != nil {
2024-05-14 13:07:09 +00:00
spec.Definitions[key] = *def
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
for key := range spec.Parameters {
2024-05-14 13:07:09 +00:00
parameter := spec.Parameters[key]
2024-05-14 13:07:09 +00:00
if err := expandParameterOrResponse(&parameter, resolver, specBasePath); resolver.shouldStopOnError(err) {
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
spec.Parameters[key] = parameter
2024-05-14 13:07:09 +00:00
}
for key := range spec.Responses {
2024-05-14 13:07:09 +00:00
response := spec.Responses[key]
2024-05-14 13:07:09 +00:00
if err := expandParameterOrResponse(&response, resolver, specBasePath); resolver.shouldStopOnError(err) {
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
spec.Responses[key] = response
2024-05-14 13:07:09 +00:00
}
if spec.Paths != nil {
2024-05-14 13:07:09 +00:00
for key := range spec.Paths.Paths {
2024-05-14 13:07:09 +00:00
pth := spec.Paths.Paths[key]
2024-05-14 13:07:09 +00:00
if err := expandPathItem(&pth, resolver, specBasePath); resolver.shouldStopOnError(err) {
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
spec.Paths.Paths[key] = pth
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}
const rootBase = ".root"
// baseForRoot loads in the cache the root document and produces a fake ".root" base path entry
2024-05-14 13:07:09 +00:00
// for further $ref resolution
2024-05-14 13:07:09 +00:00
func baseForRoot(root interface{}, cache ResolutionCache) string {
2024-05-14 13:07:09 +00:00
// cache the root document to resolve $ref's
2024-05-14 13:07:09 +00:00
normalizedBase := normalizeBase(rootBase)
if root == nil {
2024-05-14 13:07:09 +00:00
// ensure that we never leave a nil root: always cache the root base pseudo-document
2024-05-14 13:07:09 +00:00
cachedRoot, found := cache.Get(normalizedBase)
2024-05-14 13:07:09 +00:00
if found && cachedRoot != nil {
2024-05-14 13:07:09 +00:00
// the cache is already preloaded with a root
2024-05-14 13:07:09 +00:00
return normalizedBase
2024-05-14 13:07:09 +00:00
}
root = map[string]interface{}{}
2024-05-14 13:07:09 +00:00
}
cache.Set(normalizedBase, root)
return normalizedBase
2024-05-14 13:07:09 +00:00
}
// ExpandSchema expands the refs in the schema object with reference to the root object.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// go-openapi/validate uses this function.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Notice that it is impossible to reference a json schema in a different document other than root
2024-05-14 13:07:09 +00:00
// (use ExpandSchemaWithBasePath to resolve external references).
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Setting the cache is optional and this parameter may safely be left to nil.
2024-05-14 13:07:09 +00:00
func ExpandSchema(schema *Schema, root interface{}, cache ResolutionCache) error {
2024-05-14 13:07:09 +00:00
cache = cacheOrDefault(cache)
2024-05-14 13:07:09 +00:00
if root == nil {
2024-05-14 13:07:09 +00:00
root = schema
2024-05-14 13:07:09 +00:00
}
opts := &ExpandOptions{
2024-05-14 13:07:09 +00:00
// when a root is specified, cache the root as an in-memory document for $ref retrieval
RelativeBase: baseForRoot(root, cache),
SkipSchemas: false,
2024-05-14 13:07:09 +00:00
ContinueOnError: false,
}
return ExpandSchemaWithBasePath(schema, cache, opts)
2024-05-14 13:07:09 +00:00
}
// ExpandSchemaWithBasePath expands the refs in the schema object, base path configured through expand options.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Setting the cache is optional and this parameter may safely be left to nil.
2024-05-14 13:07:09 +00:00
func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *ExpandOptions) error {
2024-05-14 13:07:09 +00:00
if schema == nil {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
cache = cacheOrDefault(cache)
opts = optionsOrDefault(opts)
resolver := defaultSchemaLoader(nil, opts, cache, nil)
parentRefs := make([]string, 0, 10)
2024-05-14 13:07:09 +00:00
s, err := expandSchema(*schema, parentRefs, resolver, opts.RelativeBase)
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
if s != nil {
2024-05-14 13:07:09 +00:00
// guard for when continuing on error
2024-05-14 13:07:09 +00:00
*schema = *s
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}
func expandItems(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
2024-05-14 13:07:09 +00:00
if target.Items == nil {
2024-05-14 13:07:09 +00:00
return &target, nil
2024-05-14 13:07:09 +00:00
}
// array
2024-05-14 13:07:09 +00:00
if target.Items.Schema != nil {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(*target.Items.Schema, parentRefs, resolver, basePath)
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
*target.Items.Schema = *t
2024-05-14 13:07:09 +00:00
}
// tuple
2024-05-14 13:07:09 +00:00
for i := range target.Items.Schemas {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(target.Items.Schemas[i], parentRefs, resolver, basePath)
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
target.Items.Schemas[i] = *t
2024-05-14 13:07:09 +00:00
}
return &target, nil
2024-05-14 13:07:09 +00:00
}
func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
2024-05-14 13:07:09 +00:00
if target.Ref.String() == "" && target.Ref.IsRoot() {
2024-05-14 13:07:09 +00:00
newRef := normalizeRef(&target.Ref, basePath)
2024-05-14 13:07:09 +00:00
target.Ref = *newRef
2024-05-14 13:07:09 +00:00
return &target, nil
2024-05-14 13:07:09 +00:00
}
// change the base path of resolution when an ID is encountered
2024-05-14 13:07:09 +00:00
// otherwise the basePath should inherit the parent's
2024-05-14 13:07:09 +00:00
if target.ID != "" {
2024-05-14 13:07:09 +00:00
basePath, _ = resolver.setSchemaID(target, target.ID, basePath)
2024-05-14 13:07:09 +00:00
}
if target.Ref.String() != "" {
2024-05-14 13:07:09 +00:00
if !resolver.options.SkipSchemas {
2024-05-14 13:07:09 +00:00
return expandSchemaRef(target, parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
}
// when "expand" with SkipSchema, we just rebase the existing $ref without replacing
2024-05-14 13:07:09 +00:00
// the full schema.
2024-05-14 13:07:09 +00:00
rebasedRef, err := NewRef(normalizeURI(target.Ref.String(), basePath))
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
target.Ref = denormalizeRef(&rebasedRef, resolver.context.basePath, resolver.context.rootID)
return &target, nil
2024-05-14 13:07:09 +00:00
}
for k := range target.Definitions {
2024-05-14 13:07:09 +00:00
tt, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if tt != nil {
2024-05-14 13:07:09 +00:00
target.Definitions[k] = *tt
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
t, err := expandItems(target, parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
target = *t
2024-05-14 13:07:09 +00:00
}
for i := range target.AllOf {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(target.AllOf[i], parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
target.AllOf[i] = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
for i := range target.AnyOf {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(target.AnyOf[i], parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
target.AnyOf[i] = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
for i := range target.OneOf {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(target.OneOf[i], parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
target.OneOf[i] = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
if target.Not != nil {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(*target.Not, parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
*target.Not = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
for k := range target.Properties {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(target.Properties[k], parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
target.Properties[k] = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
if target.AdditionalProperties != nil && target.AdditionalProperties.Schema != nil {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(*target.AdditionalProperties.Schema, parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
*target.AdditionalProperties.Schema = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
for k := range target.PatternProperties {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(target.PatternProperties[k], parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
target.PatternProperties[k] = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
for k := range target.Dependencies {
2024-05-14 13:07:09 +00:00
if target.Dependencies[k].Schema != nil {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(*target.Dependencies[k].Schema, parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
*target.Dependencies[k].Schema = *t
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
if target.AdditionalItems != nil && target.AdditionalItems.Schema != nil {
2024-05-14 13:07:09 +00:00
t, err := expandSchema(*target.AdditionalItems.Schema, parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return &target, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if t != nil {
2024-05-14 13:07:09 +00:00
*target.AdditionalItems.Schema = *t
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 &target, nil
2024-05-14 13:07:09 +00:00
}
func expandSchemaRef(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
2024-05-14 13:07:09 +00:00
// if a Ref is found, all sibling fields are skipped
2024-05-14 13:07:09 +00:00
// Ref also changes the resolution scope of children expandSchema
// here the resolution scope is changed because a $ref was encountered
2024-05-14 13:07:09 +00:00
normalizedRef := normalizeRef(&target.Ref, basePath)
2024-05-14 13:07:09 +00:00
normalizedBasePath := normalizedRef.RemoteURI()
if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
2024-05-14 13:07:09 +00:00
// this means there is a cycle in the recursion tree: return the Ref
2024-05-14 13:07:09 +00:00
// - circular refs cannot be expanded. We leave them as ref.
2024-05-14 13:07:09 +00:00
// - denormalization means that a new local file ref is set relative to the original basePath
2024-05-14 13:07:09 +00:00
debugLog("short circuit circular ref: basePath: %s, normalizedPath: %s, normalized ref: %s",
2024-05-14 13:07:09 +00:00
basePath, normalizedBasePath, normalizedRef.String())
2024-05-14 13:07:09 +00:00
if !resolver.options.AbsoluteCircularRef {
2024-05-14 13:07:09 +00:00
target.Ref = denormalizeRef(normalizedRef, resolver.context.basePath, resolver.context.rootID)
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
target.Ref = *normalizedRef
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return &target, nil
2024-05-14 13:07:09 +00:00
}
var t *Schema
2024-05-14 13:07:09 +00:00
err := resolver.Resolve(&target.Ref, &t, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
if t == nil {
2024-05-14 13:07:09 +00:00
// guard for when continuing on error
2024-05-14 13:07:09 +00:00
return &target, nil
2024-05-14 13:07:09 +00:00
}
parentRefs = append(parentRefs, normalizedRef.String())
2024-05-14 13:07:09 +00:00
transitiveResolver := resolver.transitiveResolver(basePath, target.Ref)
basePath = resolver.updateBasePath(transitiveResolver, normalizedBasePath)
return expandSchema(*t, parentRefs, transitiveResolver, basePath)
2024-05-14 13:07:09 +00:00
}
func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string) error {
2024-05-14 13:07:09 +00:00
if pathItem == nil {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
parentRefs := make([]string, 0, 10)
2024-05-14 13:07:09 +00:00
if err := resolver.deref(pathItem, parentRefs, basePath); resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
if pathItem.Ref.String() != "" {
2024-05-14 13:07:09 +00:00
transitiveResolver := resolver.transitiveResolver(basePath, pathItem.Ref)
2024-05-14 13:07:09 +00:00
basePath = transitiveResolver.updateBasePath(resolver, basePath)
2024-05-14 13:07:09 +00:00
resolver = transitiveResolver
2024-05-14 13:07:09 +00:00
}
pathItem.Ref = Ref{}
2024-05-14 13:07:09 +00:00
for i := range pathItem.Parameters {
2024-05-14 13:07:09 +00:00
if err := expandParameterOrResponse(&(pathItem.Parameters[i]), resolver, basePath); resolver.shouldStopOnError(err) {
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
}
ops := []*Operation{
2024-05-14 13:07:09 +00:00
pathItem.Get,
2024-05-14 13:07:09 +00:00
pathItem.Head,
2024-05-14 13:07:09 +00:00
pathItem.Options,
2024-05-14 13:07:09 +00:00
pathItem.Put,
2024-05-14 13:07:09 +00:00
pathItem.Post,
2024-05-14 13:07:09 +00:00
pathItem.Patch,
2024-05-14 13:07:09 +00:00
pathItem.Delete,
}
2024-05-14 13:07:09 +00:00
for _, op := range ops {
2024-05-14 13:07:09 +00:00
if err := expandOperation(op, resolver, basePath); resolver.shouldStopOnError(err) {
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
}
return nil
2024-05-14 13:07:09 +00:00
}
func expandOperation(op *Operation, resolver *schemaLoader, basePath string) error {
2024-05-14 13:07:09 +00:00
if op == nil {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
for i := range op.Parameters {
2024-05-14 13:07:09 +00:00
param := op.Parameters[i]
2024-05-14 13:07:09 +00:00
if err := expandParameterOrResponse(&param, resolver, basePath); resolver.shouldStopOnError(err) {
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
op.Parameters[i] = param
2024-05-14 13:07:09 +00:00
}
if op.Responses == nil {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
responses := op.Responses
2024-05-14 13:07:09 +00:00
if err := expandParameterOrResponse(responses.Default, resolver, basePath); resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
for code := range responses.StatusCodeResponses {
2024-05-14 13:07:09 +00:00
response := responses.StatusCodeResponses[code]
2024-05-14 13:07:09 +00:00
if err := expandParameterOrResponse(&response, resolver, basePath); resolver.shouldStopOnError(err) {
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
responses.StatusCodeResponses[code] = response
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}
// ExpandResponseWithRoot expands a response based on a root document, not a fetchable document
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Notice that it is impossible to reference a json schema in a different document other than root
2024-05-14 13:07:09 +00:00
// (use ExpandResponse to resolve external references).
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Setting the cache is optional and this parameter may safely be left to nil.
2024-05-14 13:07:09 +00:00
func ExpandResponseWithRoot(response *Response, root interface{}, cache ResolutionCache) error {
2024-05-14 13:07:09 +00:00
cache = cacheOrDefault(cache)
2024-05-14 13:07:09 +00:00
opts := &ExpandOptions{
2024-05-14 13:07:09 +00:00
RelativeBase: baseForRoot(root, cache),
}
2024-05-14 13:07:09 +00:00
resolver := defaultSchemaLoader(root, opts, cache, nil)
return expandParameterOrResponse(response, resolver, opts.RelativeBase)
2024-05-14 13:07:09 +00:00
}
// ExpandResponse expands a response based on a basepath
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// All refs inside response will be resolved relative to basePath
2024-05-14 13:07:09 +00:00
func ExpandResponse(response *Response, basePath string) error {
2024-05-14 13:07:09 +00:00
opts := optionsOrDefault(&ExpandOptions{
2024-05-14 13:07:09 +00:00
RelativeBase: basePath,
})
2024-05-14 13:07:09 +00:00
resolver := defaultSchemaLoader(nil, opts, nil, nil)
return expandParameterOrResponse(response, resolver, opts.RelativeBase)
2024-05-14 13:07:09 +00:00
}
// ExpandParameterWithRoot expands a parameter based on a root document, not a fetchable document.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Notice that it is impossible to reference a json schema in a different document other than root
2024-05-14 13:07:09 +00:00
// (use ExpandParameter to resolve external references).
2024-05-14 13:07:09 +00:00
func ExpandParameterWithRoot(parameter *Parameter, root interface{}, cache ResolutionCache) error {
2024-05-14 13:07:09 +00:00
cache = cacheOrDefault(cache)
opts := &ExpandOptions{
2024-05-14 13:07:09 +00:00
RelativeBase: baseForRoot(root, cache),
}
2024-05-14 13:07:09 +00:00
resolver := defaultSchemaLoader(root, opts, cache, nil)
return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
2024-05-14 13:07:09 +00:00
}
// ExpandParameter expands a parameter based on a basepath.
2024-05-14 13:07:09 +00:00
// This is the exported version of expandParameter
2024-05-14 13:07:09 +00:00
// all refs inside parameter will be resolved relative to basePath
2024-05-14 13:07:09 +00:00
func ExpandParameter(parameter *Parameter, basePath string) error {
2024-05-14 13:07:09 +00:00
opts := optionsOrDefault(&ExpandOptions{
2024-05-14 13:07:09 +00:00
RelativeBase: basePath,
})
2024-05-14 13:07:09 +00:00
resolver := defaultSchemaLoader(nil, opts, nil, nil)
return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
2024-05-14 13:07:09 +00:00
}
func getRefAndSchema(input interface{}) (*Ref, *Schema, error) {
2024-05-14 13:07:09 +00:00
var (
ref *Ref
2024-05-14 13:07:09 +00:00
sch *Schema
)
switch refable := input.(type) {
2024-05-14 13:07:09 +00:00
case *Parameter:
2024-05-14 13:07:09 +00:00
if refable == nil {
2024-05-14 13:07:09 +00:00
return nil, nil, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
ref = &refable.Ref
2024-05-14 13:07:09 +00:00
sch = refable.Schema
2024-05-14 13:07:09 +00:00
case *Response:
2024-05-14 13:07:09 +00:00
if refable == nil {
2024-05-14 13:07:09 +00:00
return nil, nil, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
ref = &refable.Ref
2024-05-14 13:07:09 +00:00
sch = refable.Schema
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
return nil, nil, fmt.Errorf("unsupported type: %T: %w", input, ErrExpandUnsupportedType)
2024-05-14 13:07:09 +00:00
}
return ref, sch, nil
2024-05-14 13:07:09 +00:00
}
func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePath string) error {
2024-05-14 13:07:09 +00:00
ref, sch, err := getRefAndSchema(input)
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
}
if ref == nil && sch == nil { // nothing to do
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
parentRefs := make([]string, 0, 10)
2024-05-14 13:07:09 +00:00
if ref != nil {
2024-05-14 13:07:09 +00:00
// dereference this $ref
2024-05-14 13:07:09 +00:00
if err = resolver.deref(input, parentRefs, basePath); resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
ref, sch, _ = getRefAndSchema(input)
2024-05-14 13:07:09 +00:00
}
if ref.String() != "" {
2024-05-14 13:07:09 +00:00
transitiveResolver := resolver.transitiveResolver(basePath, *ref)
2024-05-14 13:07:09 +00:00
basePath = resolver.updateBasePath(transitiveResolver, basePath)
2024-05-14 13:07:09 +00:00
resolver = transitiveResolver
2024-05-14 13:07:09 +00:00
}
if sch == nil {
2024-05-14 13:07:09 +00:00
// nothing to be expanded
2024-05-14 13:07:09 +00:00
if ref != nil {
2024-05-14 13:07:09 +00:00
*ref = Ref{}
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}
if sch.Ref.String() != "" {
2024-05-14 13:07:09 +00:00
rebasedRef, ern := NewRef(normalizeURI(sch.Ref.String(), basePath))
2024-05-14 13:07:09 +00:00
if ern != nil {
2024-05-14 13:07:09 +00:00
return ern
2024-05-14 13:07:09 +00:00
}
if resolver.isCircular(&rebasedRef, basePath, parentRefs...) {
2024-05-14 13:07:09 +00:00
// this is a circular $ref: stop expansion
2024-05-14 13:07:09 +00:00
if !resolver.options.AbsoluteCircularRef {
2024-05-14 13:07:09 +00:00
sch.Ref = denormalizeRef(&rebasedRef, resolver.context.basePath, resolver.context.rootID)
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
sch.Ref = rebasedRef
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
// $ref expansion or rebasing is performed by expandSchema below
2024-05-14 13:07:09 +00:00
if ref != nil {
2024-05-14 13:07:09 +00:00
*ref = Ref{}
2024-05-14 13:07:09 +00:00
}
// expand schema
2024-05-14 13:07:09 +00:00
// yes, we do it even if options.SkipSchema is true: we have to go down that rabbit hole and rebase nested $ref)
2024-05-14 13:07:09 +00:00
s, err := expandSchema(*sch, parentRefs, resolver, basePath)
2024-05-14 13:07:09 +00:00
if resolver.shouldStopOnError(err) {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
if s != nil { // guard for when continuing on error
2024-05-14 13:07:09 +00:00
*sch = *s
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}