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

200 lines
3.8 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
package spec
import (
"fmt"
"github.com/go-openapi/swag"
)
func resolveAnyWithBase(root interface{}, ref *Ref, result interface{}, options *ExpandOptions) error {
2024-05-14 13:07:09 +00:00
options = optionsOrDefault(options)
2024-05-14 13:07:09 +00:00
resolver := defaultSchemaLoader(root, options, nil, nil)
if err := resolver.Resolve(ref, result, options.RelativeBase); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
return nil
2024-05-14 13:07:09 +00:00
}
// ResolveRefWithBase resolves a reference against a context root with preservation of base path
2024-05-14 13:07:09 +00:00
func ResolveRefWithBase(root interface{}, ref *Ref, options *ExpandOptions) (*Schema, error) {
2024-05-14 13:07:09 +00:00
result := new(Schema)
if err := resolveAnyWithBase(root, ref, result, options); err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
return result, nil
2024-05-14 13:07:09 +00:00
}
// ResolveRef resolves a reference for a schema against a context root
2024-05-14 13:07:09 +00:00
// ref is guaranteed to be in root (no need to go to external files)
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// ResolveRef is ONLY called from the code generation module
2024-05-14 13:07:09 +00:00
func ResolveRef(root interface{}, ref *Ref) (*Schema, error) {
2024-05-14 13:07:09 +00:00
res, _, err := ref.GetPointer().Get(root)
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
}
switch sch := res.(type) {
2024-05-14 13:07:09 +00:00
case Schema:
2024-05-14 13:07:09 +00:00
return &sch, nil
2024-05-14 13:07:09 +00:00
case *Schema:
2024-05-14 13:07:09 +00:00
return sch, nil
2024-05-14 13:07:09 +00:00
case map[string]interface{}:
2024-05-14 13:07:09 +00:00
newSch := new(Schema)
2024-05-14 13:07:09 +00:00
if err = swag.DynamicJSONToStruct(sch, newSch); 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 newSch, nil
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("type: %T: %w", sch, ErrUnknownTypeForReference)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
// ResolveParameterWithBase resolves a parameter reference against a context root and base path
2024-05-14 13:07:09 +00:00
func ResolveParameterWithBase(root interface{}, ref Ref, options *ExpandOptions) (*Parameter, error) {
2024-05-14 13:07:09 +00:00
result := new(Parameter)
if err := resolveAnyWithBase(root, &ref, result, options); err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
return result, nil
2024-05-14 13:07:09 +00:00
}
// ResolveParameter resolves a parameter reference against a context root
2024-05-14 13:07:09 +00:00
func ResolveParameter(root interface{}, ref Ref) (*Parameter, error) {
2024-05-14 13:07:09 +00:00
return ResolveParameterWithBase(root, ref, nil)
2024-05-14 13:07:09 +00:00
}
// ResolveResponseWithBase resolves response a reference against a context root and base path
2024-05-14 13:07:09 +00:00
func ResolveResponseWithBase(root interface{}, ref Ref, options *ExpandOptions) (*Response, error) {
2024-05-14 13:07:09 +00:00
result := new(Response)
err := resolveAnyWithBase(root, &ref, result, options)
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
}
return result, nil
2024-05-14 13:07:09 +00:00
}
// ResolveResponse resolves response a reference against a context root
2024-05-14 13:07:09 +00:00
func ResolveResponse(root interface{}, ref Ref) (*Response, error) {
2024-05-14 13:07:09 +00:00
return ResolveResponseWithBase(root, ref, nil)
2024-05-14 13:07:09 +00:00
}
// ResolvePathItemWithBase resolves response a path item against a context root and base path
2024-05-14 13:07:09 +00:00
func ResolvePathItemWithBase(root interface{}, ref Ref, options *ExpandOptions) (*PathItem, error) {
2024-05-14 13:07:09 +00:00
result := new(PathItem)
if err := resolveAnyWithBase(root, &ref, result, options); err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
return result, nil
2024-05-14 13:07:09 +00:00
}
// ResolvePathItem resolves response a path item against a context root and base path
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Deprecated: use ResolvePathItemWithBase instead
2024-05-14 13:07:09 +00:00
func ResolvePathItem(root interface{}, ref Ref, options *ExpandOptions) (*PathItem, error) {
2024-05-14 13:07:09 +00:00
return ResolvePathItemWithBase(root, ref, options)
2024-05-14 13:07:09 +00:00
}
// ResolveItemsWithBase resolves parameter items reference against a context root and base path.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// NOTE: stricly speaking, this construct is not supported by Swagger 2.0.
2024-05-14 13:07:09 +00:00
// Similarly, $ref are forbidden in response headers.
2024-05-14 13:07:09 +00:00
func ResolveItemsWithBase(root interface{}, ref Ref, options *ExpandOptions) (*Items, error) {
2024-05-14 13:07:09 +00:00
result := new(Items)
if err := resolveAnyWithBase(root, &ref, result, options); err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
return result, nil
2024-05-14 13:07:09 +00:00
}
// ResolveItems resolves parameter items reference against a context root and base path.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Deprecated: use ResolveItemsWithBase instead
2024-05-14 13:07:09 +00:00
func ResolveItems(root interface{}, ref Ref, options *ExpandOptions) (*Items, error) {
2024-05-14 13:07:09 +00:00
return ResolveItemsWithBase(root, ref, options)
2024-05-14 13:07:09 +00:00
}