diff options
Diffstat (limited to 'vendor/github.com/go-swagger')
8 files changed, 83 insertions, 59 deletions
diff --git a/vendor/github.com/go-swagger/go-swagger/cmd/swagger/commands/generate/spec.go b/vendor/github.com/go-swagger/go-swagger/cmd/swagger/commands/generate/spec.go index ed47da338..58f6a945b 100644 --- a/vendor/github.com/go-swagger/go-swagger/cmd/swagger/commands/generate/spec.go +++ b/vendor/github.com/go-swagger/go-swagger/cmd/swagger/commands/generate/spec.go @@ -30,17 +30,18 @@ import ( // SpecFile command to generate a swagger spec from a go application type SpecFile struct { - WorkDir string `long:"work-dir" short:"w" description:"the base path to use" default:"."` - BuildTags string `long:"tags" short:"t" description:"build tags" default:""` - ScanModels bool `long:"scan-models" short:"m" description:"includes models that were annotated with 'swagger:model'"` - Compact bool `long:"compact" description:"when present, doesn't prettify the json"` - Output flags.Filename `long:"output" short:"o" description:"the file to write to"` - Input flags.Filename `long:"input" short:"i" description:"an input swagger file with which to merge"` - Include []string `long:"include" short:"c" description:"include packages matching pattern"` - Exclude []string `long:"exclude" short:"x" description:"exclude packages matching pattern"` - IncludeTags []string `long:"include-tag" short:"" description:"include routes having specified tags (can be specified many times)"` - ExcludeTags []string `long:"exclude-tag" short:"" description:"exclude routes having specified tags (can be specified many times)"` - ExcludeDeps bool `long:"exclude-deps" short:"" description:"exclude all dependencies of project"` + WorkDir string `long:"work-dir" short:"w" description:"the base path to use" default:"."` + BuildTags string `long:"tags" short:"t" description:"build tags" default:""` + ScanModels bool `long:"scan-models" short:"m" description:"includes models that were annotated with 'swagger:model'"` + Compact bool `long:"compact" description:"when present, doesn't prettify the json"` + Output flags.Filename `long:"output" short:"o" description:"the file to write to"` + Input flags.Filename `long:"input" short:"i" description:"an input swagger file with which to merge"` + Include []string `long:"include" short:"c" description:"include packages matching pattern"` + Exclude []string `long:"exclude" short:"x" description:"exclude packages matching pattern"` + IncludeTags []string `long:"include-tag" short:"" description:"include routes having specified tags (can be specified many times)"` + ExcludeTags []string `long:"exclude-tag" short:"" description:"exclude routes having specified tags (can be specified many times)"` + ExcludeDeps bool `long:"exclude-deps" short:"" description:"exclude all dependencies of project"` + SetXNullableForPointers bool `long:"nullable-pointers" short:"n" description:"set x-nullable extension to true automatically for fields of pointer types without 'omitempty'"` } // Execute runs this command @@ -65,6 +66,7 @@ func (s *SpecFile) Execute(args []string) error { opts.IncludeTags = s.IncludeTags opts.ExcludeTags = s.ExcludeTags opts.ExcludeDeps = s.ExcludeDeps + opts.SetXNullableForPointers = s.SetXNullableForPointers swspec, err := codescan.Run(&opts) if err != nil { return err diff --git a/vendor/github.com/go-swagger/go-swagger/codescan/application.go b/vendor/github.com/go-swagger/go-swagger/codescan/application.go index b7051ab85..ebaa6261a 100644 --- a/vendor/github.com/go-swagger/go-swagger/codescan/application.go +++ b/vendor/github.com/go-swagger/go-swagger/codescan/application.go @@ -42,16 +42,17 @@ const ( // Options for the scanner type Options struct { - Packages []string - InputSpec *spec.Swagger - ScanModels bool - WorkDir string - BuildTags string - ExcludeDeps bool - Include []string - Exclude []string - IncludeTags []string - ExcludeTags []string + Packages []string + InputSpec *spec.Swagger + ScanModels bool + WorkDir string + BuildTags string + ExcludeDeps bool + Include []string + Exclude []string + IncludeTags []string + ExcludeTags []string + SetXNullableForPointers bool } type scanCtx struct { @@ -94,7 +95,7 @@ func newScanCtx(opts *Options) (*scanCtx, error) { app, err := newTypeIndex(pkgs, opts.ExcludeDeps, sliceToSet(opts.IncludeTags), sliceToSet(opts.ExcludeTags), - opts.Include, opts.Exclude) + opts.Include, opts.Exclude, opts.SetXNullableForPointers) if err != nil { return nil, err } @@ -418,16 +419,17 @@ func (s *scanCtx) FindEnumValues(pkg *packages.Package, enumName string) (list [ return list, descList, true } -func newTypeIndex(pkgs []*packages.Package, excludeDeps bool, includeTags, excludeTags map[string]bool, includePkgs, excludePkgs []string) (*typeIndex, error) { +func newTypeIndex(pkgs []*packages.Package, excludeDeps bool, includeTags, excludeTags map[string]bool, includePkgs, excludePkgs []string, setXNullableForPointers bool) (*typeIndex, error) { ac := &typeIndex{ - AllPackages: make(map[string]*packages.Package), - Models: make(map[*ast.Ident]*entityDecl), - ExtraModels: make(map[*ast.Ident]*entityDecl), - excludeDeps: excludeDeps, - includeTags: includeTags, - excludeTags: excludeTags, - includePkgs: includePkgs, - excludePkgs: excludePkgs, + AllPackages: make(map[string]*packages.Package), + Models: make(map[*ast.Ident]*entityDecl), + ExtraModels: make(map[*ast.Ident]*entityDecl), + excludeDeps: excludeDeps, + includeTags: includeTags, + excludeTags: excludeTags, + includePkgs: includePkgs, + excludePkgs: excludePkgs, + setXNullableForPointers: setXNullableForPointers, } if err := ac.build(pkgs); err != nil { return nil, err @@ -436,19 +438,20 @@ func newTypeIndex(pkgs []*packages.Package, excludeDeps bool, includeTags, exclu } type typeIndex struct { - AllPackages map[string]*packages.Package - Models map[*ast.Ident]*entityDecl - ExtraModels map[*ast.Ident]*entityDecl - Meta []metaSection - Routes []parsedPathContent - Operations []parsedPathContent - Parameters []*entityDecl - Responses []*entityDecl - excludeDeps bool - includeTags map[string]bool - excludeTags map[string]bool - includePkgs []string - excludePkgs []string + AllPackages map[string]*packages.Package + Models map[*ast.Ident]*entityDecl + ExtraModels map[*ast.Ident]*entityDecl + Meta []metaSection + Routes []parsedPathContent + Operations []parsedPathContent + Parameters []*entityDecl + Responses []*entityDecl + excludeDeps bool + includeTags map[string]bool + excludeTags map[string]bool + includePkgs []string + excludePkgs []string + setXNullableForPointers bool } func (a *typeIndex) build(pkgs []*packages.Package) error { diff --git a/vendor/github.com/go-swagger/go-swagger/codescan/parameters.go b/vendor/github.com/go-swagger/go-swagger/codescan/parameters.go index 9a0b77ca0..1ee769ae8 100644 --- a/vendor/github.com/go-swagger/go-swagger/codescan/parameters.go +++ b/vendor/github.com/go-swagger/go-swagger/codescan/parameters.go @@ -339,7 +339,7 @@ func (p *parameterBuilder) buildFromStruct(decl *entityDecl, tpe *types.Struct, continue } - name, ignore, _, err := parseJSONTag(afld) + name, ignore, _, _, err := parseJSONTag(afld) if err != nil { return err } diff --git a/vendor/github.com/go-swagger/go-swagger/codescan/responses.go b/vendor/github.com/go-swagger/go-swagger/codescan/responses.go index 95dff0f85..39274baf0 100644 --- a/vendor/github.com/go-swagger/go-swagger/codescan/responses.go +++ b/vendor/github.com/go-swagger/go-swagger/codescan/responses.go @@ -333,7 +333,7 @@ func (r *responseBuilder) buildFromStruct(decl *entityDecl, tpe *types.Struct, r continue } - name, ignore, _, err := parseJSONTag(afld) + name, ignore, _, _, err := parseJSONTag(afld) if err != nil { return err } diff --git a/vendor/github.com/go-swagger/go-swagger/codescan/schema.go b/vendor/github.com/go-swagger/go-swagger/codescan/schema.go index 98bdecba6..640ac0830 100644 --- a/vendor/github.com/go-swagger/go-swagger/codescan/schema.go +++ b/vendor/github.com/go-swagger/go-swagger/codescan/schema.go @@ -365,6 +365,10 @@ func (s *schemaBuilder) buildFromType(tpe types.Type, tgt swaggerTypable) error return s.buildFromType(titpe.Underlying(), tgt) } + if titpe.TypeArgs() != nil && titpe.TypeArgs().Len() > 0 { + return s.buildFromType(titpe.Underlying(), tgt) + } + switch utitpe := tpe.Underlying().(type) { case *types.Struct: if decl, ok := s.ctx.FindModel(tio.Pkg().Path(), tio.Name()); ok { @@ -407,7 +411,7 @@ func (s *schemaBuilder) buildFromType(tpe types.Type, tgt swaggerTypable) error } if defaultName, ok := defaultName(cmt); ok { - debugLog(defaultName) + debugLog(defaultName) //nolint:govet return nil } @@ -651,6 +655,12 @@ func (s *schemaBuilder) buildFromInterface(decl *entityDecl, it *types.Interface ps.AddExtension("x-go-name", fld.Name()) } + if s.ctx.app.setXNullableForPointers { + if _, isPointer := fld.Type().(*types.Signature).Results().At(0).Type().(*types.Pointer); isPointer && (ps.Extensions == nil || (ps.Extensions["x-nullable"] == nil && ps.Extensions["x-isnullable"] == nil)) { + ps.AddExtension("x-nullable", true) + } + } + seen[name] = fld.Name() tgt.Properties[name] = ps } @@ -716,7 +726,7 @@ func (s *schemaBuilder) buildFromStruct(decl *entityDecl, st *types.Struct, sche continue } - _, ignore, _, err := parseJSONTag(afld) + _, ignore, _, _, err := parseJSONTag(afld) if err != nil { return err } @@ -816,7 +826,7 @@ func (s *schemaBuilder) buildFromStruct(decl *entityDecl, st *types.Struct, sche continue } - name, ignore, isString, err := parseJSONTag(afld) + name, ignore, isString, omitEmpty, err := parseJSONTag(afld) if err != nil { return err } @@ -853,6 +863,13 @@ func (s *schemaBuilder) buildFromStruct(decl *entityDecl, st *types.Struct, sche addExtension(&ps.VendorExtensible, "x-go-name", fld.Name()) } + if s.ctx.app.setXNullableForPointers { + if _, isPointer := fld.Type().(*types.Pointer); isPointer && !omitEmpty && + (ps.Extensions == nil || (ps.Extensions["x-nullable"] == nil && ps.Extensions["x-isnullable"] == nil)) { + ps.AddExtension("x-nullable", true) + } + } + // we have 2 cases: // 1. field with different name override tag // 2. field with different name removes tag @@ -1106,17 +1123,17 @@ func (t tagOptions) Name() string { return t[0] } -func parseJSONTag(field *ast.Field) (name string, ignore bool, isString bool, err error) { +func parseJSONTag(field *ast.Field) (name string, ignore, isString, omitEmpty bool, err error) { if len(field.Names) > 0 { name = field.Names[0].Name } if field.Tag == nil || len(strings.TrimSpace(field.Tag.Value)) == 0 { - return name, false, false, nil + return name, false, false, false, nil } tv, err := strconv.Unquote(field.Tag.Value) if err != nil { - return name, false, false, err + return name, false, false, false, err } if strings.TrimSpace(tv) != "" { @@ -1129,16 +1146,18 @@ func parseJSONTag(field *ast.Field) (name string, ignore bool, isString bool, er isString = isFieldStringable(field.Type) } + omitEmpty = jsonParts.Contain("omitempty") + switch jsonParts.Name() { case "-": - return name, true, isString, nil + return name, true, isString, omitEmpty, nil case "": - return name, false, isString, nil + return name, false, isString, omitEmpty, nil default: - return jsonParts.Name(), false, isString, nil + return jsonParts.Name(), false, isString, omitEmpty, nil } } - return name, false, false, nil + return name, false, false, false, nil } // isFieldStringable check if the field type is a scalar. If the field type is diff --git a/vendor/github.com/go-swagger/go-swagger/generator/operation.go b/vendor/github.com/go-swagger/go-swagger/generator/operation.go index 07ff8a230..a2098c375 100644 --- a/vendor/github.com/go-swagger/go-swagger/generator/operation.go +++ b/vendor/github.com/go-swagger/go-swagger/generator/operation.go @@ -1258,7 +1258,7 @@ func (b *codeGenOpBuilder) analyzeTags() (string, []string, bool) { return tag, intersected, len(filter) == 0 || len(filter) > 0 && len(intersected) > 0 } -var versionedPkgRex = regexp.MustCompile(`(?i)(v)([0-9]+)`) +var versionedPkgRex = regexp.MustCompile(`(?i)^(v)([0-9]+)$`) func maxInt(a, b int) int { if a > b { diff --git a/vendor/github.com/go-swagger/go-swagger/generator/shared.go b/vendor/github.com/go-swagger/go-swagger/generator/shared.go index e466a9301..75ed251fe 100644 --- a/vendor/github.com/go-swagger/go-swagger/generator/shared.go +++ b/vendor/github.com/go-swagger/go-swagger/generator/shared.go @@ -280,7 +280,7 @@ type TemplateOpts struct { Target string `mapstructure:"target"` FileName string `mapstructure:"file_name"` SkipExists bool `mapstructure:"skip_exists"` - SkipFormat bool `mapstructure:"skip_format"` + SkipFormat bool `mapstructure:"skip_format"` // not a feature, but for debugging. generated code before formatting might not work because of unused imports. } // SectionOpts allows for specifying options to customize the templates used for generation diff --git a/vendor/github.com/go-swagger/go-swagger/generator/types.go b/vendor/github.com/go-swagger/go-swagger/generator/types.go index 5cc00f24b..59057ca8c 100644 --- a/vendor/github.com/go-swagger/go-swagger/generator/types.go +++ b/vendor/github.com/go-swagger/go-swagger/generator/types.go @@ -24,8 +24,8 @@ import ( "github.com/go-openapi/loads" "github.com/go-openapi/spec" "github.com/go-openapi/swag" + "github.com/go-viper/mapstructure/v2" "github.com/kr/pretty" - "github.com/mitchellh/mapstructure" ) const ( |
