summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/go-openapi/spec/expander.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/go-openapi/spec/expander.go')
-rw-r--r--vendor/github.com/go-openapi/spec/expander.go506
1 files changed, 223 insertions, 283 deletions
diff --git a/vendor/github.com/go-openapi/spec/expander.go b/vendor/github.com/go-openapi/spec/expander.go
index 1f30e7f8b1..8a80e2bb81 100644
--- a/vendor/github.com/go-openapi/spec/expander.go
+++ b/vendor/github.com/go-openapi/spec/expander.go
@@ -17,140 +17,21 @@ package spec
import (
"encoding/json"
"fmt"
- "strings"
)
-// ExpandOptions provides options for spec expand
+// ExpandOptions provides options for the spec expander.
type ExpandOptions struct {
- RelativeBase string
- SkipSchemas bool
- ContinueOnError bool
- AbsoluteCircularRef bool
-}
+ RelativeBase string
+ SkipSchemas bool
+ ContinueOnError bool
+ PathLoader func(string) (json.RawMessage, error) `json:"-"`
-// ResolveRefWithBase resolves a reference against a context root with preservation of base path
-func ResolveRefWithBase(root interface{}, ref *Ref, opts *ExpandOptions) (*Schema, error) {
- resolver, err := defaultSchemaLoader(root, opts, nil, nil)
- if err != nil {
- return nil, err
- }
- specBasePath := ""
- if opts != nil && opts.RelativeBase != "" {
- specBasePath, _ = absPath(opts.RelativeBase)
- }
-
- result := new(Schema)
- if err := resolver.Resolve(ref, result, specBasePath); err != nil {
- return nil, err
- }
- return result, nil
-}
-
-// ResolveRef resolves a reference against a context root
-// ref is guaranteed to be in root (no need to go to external files)
-// ResolveRef is ONLY called from the code generation module
-func ResolveRef(root interface{}, ref *Ref) (*Schema, error) {
- res, _, err := ref.GetPointer().Get(root)
- if err != nil {
- panic(err)
- }
- switch sch := res.(type) {
- case Schema:
- return &sch, nil
- case *Schema:
- return sch, nil
- case map[string]interface{}:
- b, _ := json.Marshal(sch)
- newSch := new(Schema)
- _ = json.Unmarshal(b, newSch)
- return newSch, nil
- default:
- return nil, fmt.Errorf("unknown type for the resolved reference")
- }
-}
-
-// ResolveParameter resolves a parameter reference against a context root
-func ResolveParameter(root interface{}, ref Ref) (*Parameter, error) {
- return ResolveParameterWithBase(root, ref, nil)
-}
-
-// ResolveParameterWithBase resolves a parameter reference against a context root and base path
-func ResolveParameterWithBase(root interface{}, ref Ref, opts *ExpandOptions) (*Parameter, error) {
- resolver, err := defaultSchemaLoader(root, opts, nil, nil)
- if err != nil {
- return nil, err
- }
-
- result := new(Parameter)
- if err := resolver.Resolve(&ref, result, ""); err != nil {
- return nil, err
- }
- return result, nil
-}
-
-// ResolveResponse resolves response a reference against a context root
-func ResolveResponse(root interface{}, ref Ref) (*Response, error) {
- return ResolveResponseWithBase(root, ref, nil)
-}
-
-// ResolveResponseWithBase resolves response a reference against a context root and base path
-func ResolveResponseWithBase(root interface{}, ref Ref, opts *ExpandOptions) (*Response, error) {
- resolver, err := defaultSchemaLoader(root, opts, nil, nil)
- if err != nil {
- return nil, err
- }
-
- result := new(Response)
- if err := resolver.Resolve(&ref, result, ""); err != nil {
- return nil, err
- }
- return result, nil
-}
-
-// ResolveItems resolves parameter items reference against a context root and base path.
-//
-// NOTE: stricly speaking, this construct is not supported by Swagger 2.0.
-// Similarly, $ref are forbidden in response headers.
-func ResolveItems(root interface{}, ref Ref, opts *ExpandOptions) (*Items, error) {
- resolver, err := defaultSchemaLoader(root, opts, nil, nil)
- if err != nil {
- return nil, err
- }
- basePath := ""
- if opts.RelativeBase != "" {
- basePath = opts.RelativeBase
- }
- result := new(Items)
- if err := resolver.Resolve(&ref, result, basePath); err != nil {
- return nil, err
- }
- return result, nil
-}
-
-// ResolvePathItem resolves response a path item against a context root and base path
-func ResolvePathItem(root interface{}, ref Ref, opts *ExpandOptions) (*PathItem, error) {
- resolver, err := defaultSchemaLoader(root, opts, nil, nil)
- if err != nil {
- return nil, err
- }
- basePath := ""
- if opts.RelativeBase != "" {
- basePath = opts.RelativeBase
- }
- result := new(PathItem)
- if err := resolver.Resolve(&ref, result, basePath); err != nil {
- return nil, err
- }
- return result, nil
+ AbsoluteCircularRef bool
}
// ExpandSpec expands the references in a swagger spec
func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
- resolver, err := defaultSchemaLoader(spec, options, nil, nil)
- // Just in case this ever returns an error.
- if resolver.shouldStopOnError(err) {
- return err
- }
+ resolver := defaultSchemaLoader(spec, options, nil, nil)
// getting the base path of the spec to adjust all subsequent reference resolutions
specBasePath := ""
@@ -160,9 +41,10 @@ func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
if options == nil || !options.SkipSchemas {
for key, definition := range spec.Definitions {
- var def *Schema
- var err error
- if def, err = expandSchema(definition, []string{fmt.Sprintf("#/definitions/%s", key)}, resolver, specBasePath); resolver.shouldStopOnError(err) {
+ parentRefs := make([]string, 0, 10)
+ parentRefs = append(parentRefs, fmt.Sprintf("#/definitions/%s", key))
+ def, err := expandSchema(definition, parentRefs, resolver, specBasePath)
+ if resolver.shouldStopOnError(err) {
return err
}
if def != nil {
@@ -189,157 +71,140 @@ func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
if spec.Paths != nil {
for key := range spec.Paths.Paths {
- path := spec.Paths.Paths[key]
- if err := expandPathItem(&path, resolver, specBasePath); resolver.shouldStopOnError(err) {
+ pth := spec.Paths.Paths[key]
+ if err := expandPathItem(&pth, resolver, specBasePath); resolver.shouldStopOnError(err) {
return err
}
- spec.Paths.Paths[key] = path
+ spec.Paths.Paths[key] = pth
}
}
return nil
}
-const rootBase = "root"
-// baseForRoot loads in the cache the root document and produces a fake "root" base path entry
+const rootBase = ".root"
+
+// baseForRoot loads in the cache the root document and produces a fake ".root" base path entry
// for further $ref resolution
+//
+// Setting the cache is optional and this parameter may safely be left to nil.
func baseForRoot(root interface{}, cache ResolutionCache) string {
- // cache the root document to resolve $ref's
- if root != nil {
- base, _ := absPath(rootBase)
- normalizedBase := normalizeAbsPath(base)
- debugLog("setting root doc in cache at: %s", normalizedBase)
- if cache == nil {
- cache = resCache
- }
- cache.Set(normalizedBase, root)
- return rootBase
+ if root == nil {
+ return ""
}
- return ""
+
+ // cache the root document to resolve $ref's
+ base, _ := absPath(rootBase)
+ normalizedBase := normalizeAbsPath(base)
+ cache.Set(normalizedBase, root)
+
+ return normalizedBase
}
-// ExpandSchema expands the refs in the schema object with reference to the root object
-// go-openapi/validate uses this function
-// notice that it is impossible to reference a json schema in a different file other than root
+// ExpandSchema expands the refs in the schema object with reference to the root object.
+//
+// go-openapi/validate uses this function.
+//
+// Notice that it is impossible to reference a json schema in a different document other than root
+// (use ExpandSchemaWithBasePath to resolve external references).
+//
+// Setting the cache is optional and this parameter may safely be left to nil.
func ExpandSchema(schema *Schema, root interface{}, cache ResolutionCache) error {
+ cache = cacheOrDefault(cache)
+ if root == nil {
+ root = schema
+ }
+
opts := &ExpandOptions{
// when a root is specified, cache the root as an in-memory document for $ref retrieval
RelativeBase: baseForRoot(root, cache),
SkipSchemas: false,
ContinueOnError: false,
- // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
- AbsoluteCircularRef: true,
}
+
return ExpandSchemaWithBasePath(schema, cache, opts)
}
-// ExpandSchemaWithBasePath expands the refs in the schema object, base path configured through expand options
+// ExpandSchemaWithBasePath expands the refs in the schema object, base path configured through expand options.
+//
+// Setting the cache is optional and this parameter may safely be left to nil.
func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *ExpandOptions) error {
if schema == nil {
return nil
}
+ cache = cacheOrDefault(cache)
+
var basePath string
if opts.RelativeBase != "" {
basePath, _ = absPath(opts.RelativeBase)
}
- resolver, err := defaultSchemaLoader(nil, opts, cache, nil)
+ resolver := defaultSchemaLoader(nil, opts, cache, nil)
+
+ parentRefs := make([]string, 0, 10)
+ s, err := expandSchema(*schema, parentRefs, resolver, basePath)
if err != nil {
return err
}
-
- refs := []string{""}
- var s *Schema
- if s, err = expandSchema(*schema, refs, resolver, basePath); err != nil {
- return err
+ if s != nil {
+ // guard for when continuing on error
+ *schema = *s
}
- *schema = *s
+
return nil
}
func expandItems(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
- if target.Items != nil {
- if target.Items.Schema != nil {
- t, err := expandSchema(*target.Items.Schema, parentRefs, resolver, basePath)
- if err != nil {
- return nil, err
- }
- *target.Items.Schema = *t
+ if target.Items == nil {
+ return &target, nil
+ }
+
+ // array
+ if target.Items.Schema != nil {
+ t, err := expandSchema(*target.Items.Schema, parentRefs, resolver, basePath)
+ if err != nil {
+ return nil, err
}
- for i := range target.Items.Schemas {
- t, err := expandSchema(target.Items.Schemas[i], parentRefs, resolver, basePath)
- if err != nil {
- return nil, err
- }
- target.Items.Schemas[i] = *t
+ *target.Items.Schema = *t
+ }
+
+ // tuple
+ for i := range target.Items.Schemas {
+ t, err := expandSchema(target.Items.Schemas[i], parentRefs, resolver, basePath)
+ if err != nil {
+ return nil, err
}
+ target.Items.Schemas[i] = *t
}
+
return &target, nil
}
func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
if target.Ref.String() == "" && target.Ref.IsRoot() {
- // normalizing is important
newRef := normalizeFileRef(&target.Ref, basePath)
target.Ref = *newRef
return &target, nil
-
}
// change the base path of resolution when an ID is encountered
// otherwise the basePath should inherit the parent's
- // important: ID can be relative path
if target.ID != "" {
- debugLog("schema has ID: %s", target.ID)
- // handling the case when id is a folder
- // remember that basePath has to be a file
- refPath := target.ID
- if strings.HasSuffix(target.ID, "/") {
- // path.Clean here would not work correctly if basepath is http
- refPath = fmt.Sprintf("%s%s", refPath, "placeholder.json")
- }
- basePath = normalizePaths(refPath, basePath)
+ basePath, _ = resolver.setSchemaID(target, target.ID, basePath)
}
- var t *Schema
- // if Ref is found, everything else doesn't matter
- // Ref also changes the resolution scope of children expandSchema
if target.Ref.String() != "" {
- // here the resolution scope is changed because a $ref was encountered
- normalizedRef := normalizeFileRef(&target.Ref, basePath)
- normalizedBasePath := normalizedRef.RemoteURI()
-
- if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
- // this means there is a cycle in the recursion tree: return the Ref
- // - circular refs cannot be expanded. We leave them as ref.
- // - denormalization means that a new local file ref is set relative to the original basePath
- debugLog("shortcut circular ref: basePath: %s, normalizedPath: %s, normalized ref: %s",
- basePath, normalizedBasePath, normalizedRef.String())
- if !resolver.options.AbsoluteCircularRef {
- target.Ref = *denormalizeFileRef(normalizedRef, normalizedBasePath, resolver.context.basePath)
- } else {
- target.Ref = *normalizedRef
- }
- return &target, nil
- }
+ return expandSchemaRef(target, parentRefs, resolver, basePath)
+ }
- debugLog("basePath: %s: calling Resolve with target: %#v", basePath, target)
- if err := resolver.Resolve(&target.Ref, &t, basePath); resolver.shouldStopOnError(err) {
- return nil, err
+ for k := range target.Definitions {
+ tt, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
+ if resolver.shouldStopOnError(err) {
+ return &target, err
}
-
- if t != nil {
- parentRefs = append(parentRefs, normalizedRef.String())
- var err error
- transitiveResolver, err := resolver.transitiveResolver(basePath, target.Ref)
- if transitiveResolver.shouldStopOnError(err) {
- return nil, err
- }
-
- basePath = resolver.updateBasePath(transitiveResolver, normalizedBasePath)
-
- return expandSchema(*t, parentRefs, transitiveResolver, basePath)
+ if tt != nil {
+ target.Definitions[k] = *tt
}
}
@@ -356,15 +221,21 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
if resolver.shouldStopOnError(err) {
return &target, err
}
- target.AllOf[i] = *t
+ if t != nil {
+ target.AllOf[i] = *t
+ }
}
+
for i := range target.AnyOf {
t, err := expandSchema(target.AnyOf[i], parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
return &target, err
}
- target.AnyOf[i] = *t
+ if t != nil {
+ target.AnyOf[i] = *t
+ }
}
+
for i := range target.OneOf {
t, err := expandSchema(target.OneOf[i], parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
@@ -374,6 +245,7 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
target.OneOf[i] = *t
}
}
+
if target.Not != nil {
t, err := expandSchema(*target.Not, parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
@@ -383,6 +255,7 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
*target.Not = *t
}
}
+
for k := range target.Properties {
t, err := expandSchema(target.Properties[k], parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
@@ -392,6 +265,7 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
target.Properties[k] = *t
}
}
+
if target.AdditionalProperties != nil && target.AdditionalProperties.Schema != nil {
t, err := expandSchema(*target.AdditionalProperties.Schema, parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
@@ -401,6 +275,7 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
*target.AdditionalProperties.Schema = *t
}
}
+
for k := range target.PatternProperties {
t, err := expandSchema(target.PatternProperties[k], parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
@@ -410,6 +285,7 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
target.PatternProperties[k] = *t
}
}
+
for k := range target.Dependencies {
if target.Dependencies[k].Schema != nil {
t, err := expandSchema(*target.Dependencies[k].Schema, parentRefs, resolver, basePath)
@@ -421,6 +297,7 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
}
}
}
+
if target.AdditionalItems != nil && target.AdditionalItems.Schema != nil {
t, err := expandSchema(*target.AdditionalItems.Schema, parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
@@ -430,16 +307,48 @@ func expandSchema(target Schema, parentRefs []string, resolver *schemaLoader, ba
*target.AdditionalItems.Schema = *t
}
}
- for k := range target.Definitions {
- t, err := expandSchema(target.Definitions[k], parentRefs, resolver, basePath)
- if resolver.shouldStopOnError(err) {
- return &target, err
- }
- if t != nil {
- target.Definitions[k] = *t
+ return &target, nil
+}
+
+func expandSchemaRef(target Schema, parentRefs []string, resolver *schemaLoader, basePath string) (*Schema, error) {
+ // if a Ref is found, all sibling fields are skipped
+ // Ref also changes the resolution scope of children expandSchema
+
+ // here the resolution scope is changed because a $ref was encountered
+ normalizedRef := normalizeFileRef(&target.Ref, basePath)
+ normalizedBasePath := normalizedRef.RemoteURI()
+
+ if resolver.isCircular(normalizedRef, basePath, parentRefs...) {
+ // this means there is a cycle in the recursion tree: return the Ref
+ // - circular refs cannot be expanded. We leave them as ref.
+ // - denormalization means that a new local file ref is set relative to the original basePath
+ debugLog("short circuit circular ref: basePath: %s, normalizedPath: %s, normalized ref: %s",
+ basePath, normalizedBasePath, normalizedRef.String())
+ if !resolver.options.AbsoluteCircularRef {
+ target.Ref = *denormalizeFileRef(normalizedRef, normalizedBasePath, resolver.context.basePath)
+ } else {
+ target.Ref = *normalizedRef
}
+ return &target, nil
}
- return &target, nil
+
+ var t *Schema
+ err := resolver.Resolve(&target.Ref, &t, basePath)
+ if resolver.shouldStopOnError(err) {
+ return nil, err
+ }
+
+ if t == nil {
+ // guard for when continuing on error
+ return &target, nil
+ }
+
+ parentRefs = append(parentRefs, normalizedRef.String())
+ transitiveResolver := resolver.transitiveResolver(basePath, target.Ref)
+
+ basePath = resolver.updateBasePath(transitiveResolver, normalizedBasePath)
+
+ return expandSchema(*t, parentRefs, transitiveResolver, basePath)
}
func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string) error {
@@ -447,25 +356,24 @@ func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string)
return nil
}
- parentRefs := []string{}
+ parentRefs := make([]string, 0, 10)
if err := resolver.deref(pathItem, parentRefs, basePath); resolver.shouldStopOnError(err) {
return err
}
+
if pathItem.Ref.String() != "" {
- transitiveResolver, err := resolver.transitiveResolver(basePath, pathItem.Ref)
- if transitiveResolver.shouldStopOnError(err) {
- return err
- }
+ transitiveResolver := resolver.transitiveResolver(basePath, pathItem.Ref)
basePath = transitiveResolver.updateBasePath(resolver, basePath)
resolver = transitiveResolver
}
- pathItem.Ref = Ref{}
- for idx := range pathItem.Parameters {
- if err := expandParameterOrResponse(&(pathItem.Parameters[idx]), resolver, basePath); resolver.shouldStopOnError(err) {
+ pathItem.Ref = Ref{}
+ for i := range pathItem.Parameters {
+ if err := expandParameterOrResponse(&(pathItem.Parameters[i]), resolver, basePath); resolver.shouldStopOnError(err) {
return err
}
}
+
ops := []*Operation{
pathItem.Get,
pathItem.Head,
@@ -480,6 +388,7 @@ func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string)
return err
}
}
+
return nil
}
@@ -496,42 +405,47 @@ func expandOperation(op *Operation, resolver *schemaLoader, basePath string) err
op.Parameters[i] = param
}
- if op.Responses != nil {
- responses := op.Responses
- if err := expandParameterOrResponse(responses.Default, resolver, basePath); resolver.shouldStopOnError(err) {
+ if op.Responses == nil {
+ return nil
+ }
+
+ responses := op.Responses
+ if err := expandParameterOrResponse(responses.Default, resolver, basePath); resolver.shouldStopOnError(err) {
+ return err
+ }
+
+ for code := range responses.StatusCodeResponses {
+ response := responses.StatusCodeResponses[code]
+ if err := expandParameterOrResponse(&response, resolver, basePath); resolver.shouldStopOnError(err) {
return err
}
- for code := range responses.StatusCodeResponses {
- response := responses.StatusCodeResponses[code]
- if err := expandParameterOrResponse(&response, resolver, basePath); resolver.shouldStopOnError(err) {
- return err
- }
- responses.StatusCodeResponses[code] = response
- }
+ responses.StatusCodeResponses[code] = response
}
+
return nil
}
// ExpandResponseWithRoot expands a response based on a root document, not a fetchable document
+//
+// Notice that it is impossible to reference a json schema in a different document other than root
+// (use ExpandResponse to resolve external references).
+//
+// Setting the cache is optional and this parameter may safely be left to nil.
func ExpandResponseWithRoot(response *Response, root interface{}, cache ResolutionCache) error {
+ cache = cacheOrDefault(cache)
opts := &ExpandOptions{
RelativeBase: baseForRoot(root, cache),
SkipSchemas: false,
ContinueOnError: false,
- // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
- AbsoluteCircularRef: true,
- }
- resolver, err := defaultSchemaLoader(root, opts, nil, nil)
- if err != nil {
- return err
}
+ resolver := defaultSchemaLoader(root, opts, cache, nil)
return expandParameterOrResponse(response, resolver, opts.RelativeBase)
}
// ExpandResponse expands a response based on a basepath
-// This is the exported version of expandResponse
-// all refs inside response will be resolved relative to basePath
+//
+// All refs inside response will be resolved relative to basePath
func ExpandResponse(response *Response, basePath string) error {
var specBasePath string
if basePath != "" {
@@ -540,27 +454,23 @@ func ExpandResponse(response *Response, basePath string) error {
opts := &ExpandOptions{
RelativeBase: specBasePath,
}
- resolver, err := defaultSchemaLoader(nil, opts, nil, nil)
- if err != nil {
- return err
- }
+ resolver := defaultSchemaLoader(nil, opts, nil, nil)
return expandParameterOrResponse(response, resolver, opts.RelativeBase)
}
-// ExpandParameterWithRoot expands a parameter based on a root document, not a fetchable document
+// ExpandParameterWithRoot expands a parameter based on a root document, not a fetchable document.
+//
+// Notice that it is impossible to reference a json schema in a different document other than root
+// (use ExpandParameter to resolve external references).
func ExpandParameterWithRoot(parameter *Parameter, root interface{}, cache ResolutionCache) error {
+ cache = cacheOrDefault(cache)
opts := &ExpandOptions{
RelativeBase: baseForRoot(root, cache),
SkipSchemas: false,
ContinueOnError: false,
- // when no base path is specified, remaining $ref (circular) are rendered with an absolute path
- AbsoluteCircularRef: true,
- }
- resolver, err := defaultSchemaLoader(root, opts, nil, nil)
- if err != nil {
- return err
}
+ resolver := defaultSchemaLoader(root, opts, cache, nil)
return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
}
@@ -576,17 +486,17 @@ func ExpandParameter(parameter *Parameter, basePath string) error {
opts := &ExpandOptions{
RelativeBase: specBasePath,
}
- resolver, err := defaultSchemaLoader(nil, opts, nil, nil)
- if err != nil {
- return err
- }
+ resolver := defaultSchemaLoader(nil, opts, nil, nil)
return expandParameterOrResponse(parameter, resolver, opts.RelativeBase)
}
func getRefAndSchema(input interface{}) (*Ref, *Schema, error) {
- var ref *Ref
- var sch *Schema
+ var (
+ ref *Ref
+ sch *Schema
+ )
+
switch refable := input.(type) {
case *Parameter:
if refable == nil {
@@ -601,8 +511,9 @@ func getRefAndSchema(input interface{}) (*Ref, *Schema, error) {
ref = &refable.Ref
sch = refable.Schema
default:
- return nil, nil, fmt.Errorf("expand: unsupported type %T. Input should be of type *Parameter or *Response", input)
+ return nil, nil, fmt.Errorf("unsupported type: %T: %w", input, ErrExpandUnsupportedType)
}
+
return ref, sch, nil
}
@@ -611,41 +522,70 @@ func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePa
if err != nil {
return err
}
+
if ref == nil {
return nil
}
- parentRefs := []string{}
- if err := resolver.deref(input, parentRefs, basePath); resolver.shouldStopOnError(err) {
+
+ parentRefs := make([]string, 0, 10)
+ if err = resolver.deref(input, parentRefs, basePath); resolver.shouldStopOnError(err) {
return err
}
+
ref, sch, _ := getRefAndSchema(input)
if ref.String() != "" {
- transitiveResolver, err := resolver.transitiveResolver(basePath, *ref)
- if transitiveResolver.shouldStopOnError(err) {
- return err
- }
+ transitiveResolver := resolver.transitiveResolver(basePath, *ref)
basePath = resolver.updateBasePath(transitiveResolver, basePath)
resolver = transitiveResolver
}
- if sch != nil && sch.Ref.String() != "" {
- // schema expanded to a $ref in another root
- var ern error
- sch.Ref, ern = NewRef(normalizePaths(sch.Ref.String(), ref.RemoteURI()))
+ if sch == nil {
+ // nothing to be expanded
+ if ref != nil {
+ *ref = Ref{}
+ }
+ return nil
+ }
+
+ if sch.Ref.String() != "" {
+ rebasedRef, ern := NewRef(normalizePaths(sch.Ref.String(), basePath))
if ern != nil {
return ern
}
+
+ switch {
+ case resolver.isCircular(&rebasedRef, basePath, parentRefs...):
+ // this is a circular $ref: stop expansion
+ if !resolver.options.AbsoluteCircularRef {
+ sch.Ref = *denormalizeFileRef(&rebasedRef, basePath, resolver.context.basePath)
+ } else {
+ sch.Ref = rebasedRef
+ }
+ case !resolver.options.SkipSchemas:
+ // schema expanded to a $ref in another root
+ sch.Ref = rebasedRef
+ default:
+ // skip schema expansion but rebase $ref to schema
+ sch.Ref = *denormalizeFileRef(&rebasedRef, basePath, resolver.context.basePath)
+ }
}
+
if ref != nil {
*ref = Ref{}
}
- if !resolver.options.SkipSchemas && sch != nil {
+ // expand schema
+ if !resolver.options.SkipSchemas {
s, err := expandSchema(*sch, parentRefs, resolver, basePath)
if resolver.shouldStopOnError(err) {
return err
}
+ if s == nil {
+ // guard for when continuing on error
+ return nil
+ }
*sch = *s
}
+
return nil
}