vendor: move to go mod

Get rid of go dep finally ;)

Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
Peng Tao
2020-04-23 11:36:12 +00:00
parent 98e3e99843
commit e540648950
1031 changed files with 121454 additions and 215703 deletions

View File

@@ -75,14 +75,12 @@ type newRef struct {
type context struct {
newRefs map[string]*newRef
warnings []string
resolved map[string]string
}
func newContext() *context {
return &context{
newRefs: make(map[string]*newRef, 150),
warnings: make([]string, 0),
resolved: make(map[string]string, 50),
}
}
@@ -143,9 +141,8 @@ func Flatten(opts FlattenOpts) error {
opts.flattenContext = newContext()
// recursively expand responses, parameters, path items and items in simple schemas.
// This simplifies the spec and leaves $ref only into schema objects.
//
// recursively expand responses, parameters, path items and items in simple schemas
// TODO: we should not expand discriminated types
if err := swspec.ExpandSpec(opts.Swagger(), opts.ExpandOpts(!opts.Expand)); err != nil {
return err
}
@@ -166,13 +163,10 @@ func Flatten(opts FlattenOpts) error {
opts.Spec.reload() // re-analyze
// at this point there are no other references left but schemas
for imported := false; !imported; {
var err error
if imported, err = importExternalReferences(&opts); err != nil {
return err
}
opts.Spec.reload() // re-analyze
if err := importExternalReferences(&opts); err != nil {
return err
}
opts.Spec.reload() // re-analyze
if !opts.Minimal && !opts.Expand {
// full flattening: rewrite inline schemas (schemas that aren't simple types or arrays or maps)
@@ -182,6 +176,7 @@ func Flatten(opts FlattenOpts) error {
opts.Spec.reload() // re-analyze
}
// rewrite JSON pointers other than $ref to named definitions
// and attempts to resolve conflicting names
if err := stripPointersAndOAIGen(&opts); err != nil {
@@ -517,10 +512,7 @@ const (
definitionsPath = "#/definitions"
)
var (
ignoredKeys map[string]struct{}
validMethods map[string]struct{}
)
var ignoredKeys map[string]struct{}
func init() {
ignoredKeys = map[string]struct{}{
@@ -530,16 +522,6 @@ func init() {
"anyOf": {},
"oneOf": {},
}
validMethods = map[string]struct{}{
"GET": {},
"HEAD": {},
"OPTIONS": {},
"PATCH": {},
"POST": {},
"PUT": {},
"DELETE": {},
}
}
type splitKey []string
@@ -637,12 +619,26 @@ func (s splitKey) ResponseName() string {
return ""
}
var validMethods map[string]struct{}
func init() {
validMethods = map[string]struct{}{
"GET": {},
"HEAD": {},
"OPTIONS": {},
"PATCH": {},
"POST": {},
"PUT": {},
"DELETE": {},
}
}
func (s splitKey) PathItemRef() swspec.Ref {
if len(s) < 3 {
return swspec.Ref{}
}
pth, method := s[1], s[2]
if _, isValidMethod := validMethods[strings.ToUpper(method)]; !isValidMethod && !strings.HasPrefix(method, "x-") {
if _, validMethod := validMethods[strings.ToUpper(method)]; !validMethod && !strings.HasPrefix(method, "x-") {
return swspec.Ref{}
}
return swspec.MustCreateRef("#" + slashpath.Join("/", paths, jsonpointer.Escape(pth), strings.ToUpper(method)))
@@ -781,12 +777,9 @@ func cloneSchema(schema *swspec.Schema) (*swspec.Schema, error) {
return &sch, nil
}
func importExternalReferences(opts *FlattenOpts) (bool, error) {
func importExternalReferences(opts *FlattenOpts) error {
groupedRefs := reverseIndexForSchemaRefs(opts)
sortedRefStr := make([]string, 0, len(groupedRefs))
if opts.flattenContext == nil {
opts.flattenContext = newContext()
}
// sort $ref resolution to ensure deterministic name conflict resolution
for refStr := range groupedRefs {
@@ -794,53 +787,35 @@ func importExternalReferences(opts *FlattenOpts) (bool, error) {
}
sort.Strings(sortedRefStr)
complete := true
for _, refStr := range sortedRefStr {
entry := groupedRefs[refStr]
if !entry.Ref.HasFragmentOnly {
complete = false
var isOAIGen bool
debugLog("importing external schema for [%s] from %s", strings.Join(entry.Keys, ", "), refStr)
// resolve to actual schema
sch := new(swspec.Schema)
sch.Ref = entry.Ref
if err := swspec.ExpandSchemaWithBasePath(sch, nil, opts.ExpandOpts(false)); err != nil {
return err
}
if sch == nil {
return fmt.Errorf("no schema found at %s for [%s]", refStr, strings.Join(entry.Keys, ", "))
}
debugLog("importing external schema for [%s] from %s", strings.Join(entry.Keys, ", "), refStr)
newName := opts.flattenContext.resolved[refStr]
if newName != "" {
// rewrite ref with already resolved external ref (useful for cyclicali refs)
debugLog("resolving known ref [%s] to %s", refStr, newName)
// rewrite the external refs to local ones
for _, key := range entry.Keys {
if err := updateRef(opts.Swagger(), key,
swspec.MustCreateRef(slashpath.Join(definitionsPath, newName))); err != nil {
return false, err
}
// generate a unique name - isOAIGen means that a naming conflict was resolved by changing the name
newName, isOAIGen := uniqifyName(opts.Swagger().Definitions, nameFromRef(entry.Ref))
debugLog("new name for [%s]: %s - with name conflict:%t",
strings.Join(entry.Keys, ", "), newName, isOAIGen)
// rewrite the external refs to local ones
for _, key := range entry.Keys {
if err := updateRef(opts.Swagger(), key,
swspec.MustCreateRef(slashpath.Join(definitionsPath, newName))); err != nil {
return err
}
} else {
debugLog("importing external schema for [%s] from %s", strings.Join(entry.Keys, ", "), refStr)
// resolve to actual schema
sch := new(swspec.Schema)
sch.Ref = entry.Ref
if err := swspec.ExpandSchemaWithBasePath(sch, nil, opts.ExpandOpts(false)); err != nil {
return false, err
}
if sch == nil {
return false, fmt.Errorf("no schema found at %s for [%s]", refStr, strings.Join(entry.Keys, ", "))
}
debugLog("importing external schema for [%s] from %s", strings.Join(entry.Keys, ", "), refStr)
// generate a unique name - isOAIGen means that a naming conflict was resolved by changing the name
newName, isOAIGen = uniqifyName(opts.Swagger().Definitions, nameFromRef(entry.Ref))
debugLog("new name for [%s]: %s - with name conflict:%t",
strings.Join(entry.Keys, ", "), newName, isOAIGen)
opts.flattenContext.resolved[refStr] = newName
// rewrite the external refs to local ones
for _, key := range entry.Keys {
if err := updateRef(opts.Swagger(), key,
swspec.MustCreateRef(slashpath.Join(definitionsPath, newName))); err != nil {
return false, err
}
// keep track of created refs
// keep track of created refs
if opts.flattenContext != nil {
resolved := false
if _, ok := opts.flattenContext.newRefs[key]; ok {
resolved = opts.flattenContext.newRefs[key].resolved
@@ -854,13 +829,13 @@ func importExternalReferences(opts *FlattenOpts) (bool, error) {
schema: sch,
}
}
// add the resolved schema to the definitions
saveSchema(opts.Swagger(), newName, sch)
}
// add the resolved schema to the definitions
saveSchema(opts.Swagger(), newName, sch)
}
}
return complete, nil
return nil
}
type refRevIdx struct {
@@ -1284,6 +1259,7 @@ func croak(opts *FlattenOpts) {
func namePointers(opts *FlattenOpts) error {
debugLog("name pointers")
refsToReplace := make(map[string]SchemaRef, len(opts.Spec.references.schemas))
//for k, ref := range opts.Spec.references.schemas {
for k, ref := range opts.Spec.references.allRefs {
if slashpath.Dir(ref.String()) == definitionsPath {
// this a ref to a top-level definition: ok
@@ -1401,7 +1377,6 @@ func namePointers(opts *FlattenOpts) error {
// NOTE: all external $ref's are assumed to be already expanded at this stage.
func deepestRef(opts *FlattenOpts, ref swspec.Ref) (swspec.Ref, *swspec.Schema, error) {
if !ref.HasFragmentOnly {
// we found an external $ref, which is odd
// does nothing on external $refs
return ref, nil, nil
}