Skip to content

Commit 36c14a4

Browse files
committed
fix some issues
1 parent 5b930d4 commit 36c14a4

File tree

10 files changed

+82
-69
lines changed

10 files changed

+82
-69
lines changed

cmd/swag/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
tagsFlag = "tags"
3939
parseExtensionFlag = "parseExtension"
4040
templateDelimsFlag = "templateDelims"
41-
openAPIVersionFlag = "v3.1"
41+
openAPIVersionFlag = "v3.1"
4242
packageName = "packageName"
4343
collectionFormatFlag = "collectionFormat"
4444
packagePrefixFlag = "packagePrefix"

enums_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
package swag
22

33
import (
4-
54
"encoding/json"
65
"math/bits"
76
"os"
87
"path/filepath"
98
"testing"
109

1110
"github.com/stretchr/testify/assert"
12-
"github.com/stretchr/testify/require"
1311
)
1412

1513
func TestParseGlobalEnums(t *testing.T) {
1614
searchDir := "testdata/enums"
15+
expected, err := os.ReadFile(filepath.Join(searchDir, "expected.json"))
16+
assert.NoError(t, err)
1717

1818
p := New()
1919
err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
2020
assert.NoError(t, err)
21-
21+
2222
b, err := json.MarshalIndent(p.swagger, "", " ")
2323
assert.NoError(t, err)
2424
assert.Equal(t, string(expected), string(b))
25-
25+
2626
constsPath := "github.com/swaggo/swag/v2/testdata/enums/consts"
27-
27+
2828
assert.Equal(t, bits.UintSize, p.packages.packages[constsPath].ConstTable["uintSize"].Value)
2929
assert.Equal(t, int32(62), p.packages.packages[constsPath].ConstTable["maxBase"].Value)
3030
assert.Equal(t, 8, p.packages.packages[constsPath].ConstTable["shlByLen"].Value)

field_parser_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,9 @@ func TestValidTags(t *testing.T) {
725725
assert.Equal(t, 2, len(fieldnames))
726726
assert.Equal(t, "x", fieldnames[0])
727727
assert.Equal(t, "y", fieldnames[1])
728+
})
728729

729-
t.Run("Pattern tag", func(t *testing.T) {
730+
t.Run("Pattern tag", func(t *testing.T) {
730731
t.Parallel()
731732

732733
schema := spec.Schema{}

gen/gen.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import (
1616
"text/template"
1717
"time"
1818

19-
2019
jsoniter "github.com/json-iterator/go"
2120

2221
v2 "github.com/go-openapi/spec"
2322
v3 "github.com/sv-tools/openapi/spec"
2423

2524
"github.com/swaggo/swag/v2"
25+
"golang.org/x/text/cases"
26+
"golang.org/x/text/language"
2627
"sigs.k8s.io/yaml"
2728
)
2829

@@ -538,7 +539,6 @@ func (g *Gen) writeGoDoc(packageName string, output io.Writer, swagger *v2.Swagg
538539
return err
539540
}
540541

541-
542542
func (g *Gen) writeGoDocV3(packageName string, output io.Writer, openAPI *v3.OpenAPI, config *Config) error {
543543
generator, err := template.New("oas3.tmpl").Funcs(template.FuncMap{
544544
"printDoc": func(v string) string {
@@ -611,7 +611,6 @@ func (g *Gen) writeGoDocV3(packageName string, output io.Writer, openAPI *v3.Ope
611611
return err
612612
}
613613

614-
615614
code := g.formatSource(buffer.Bytes())
616615

617616
// write

gen/gen_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ func TestGen_BuildDocCustomDelims(t *testing.T) {
284284
}
285285
}
286286

287-
288287
cmd := exec.Command("go", "build", "-buildmode=plugin", "github.com/swaggo/swag/v2/testdata/delims")
289288

290289
cmd.Dir = config.SearchDir

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/modern-go/reflect2 v1.0.2 // indirect
2020
github.com/russross/blackfriday/v2 v2.1.0 // indirect
2121
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
22+
golang.org/x/text v0.14.0 // indirect
2223
)
2324

2425
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr
6767
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
6868
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
6969
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
70+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
71+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
7072
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
7173
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
7274
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

packages.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ func (pkgDefs *PackagesDefinitions) parseTypesFromFile(astFile *ast.File, packag
133133
if !ok {
134134
continue
135135
}
136-
137136
if generalDeclaration.Tok == token.TYPE {
138137
for _, astSpec := range generalDeclaration.Specs {
139138
if typeSpec, ok := astSpec.(*ast.TypeSpec); ok {
@@ -581,20 +580,20 @@ func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File
581580
}
582581
}
583582
typeDef := pkgDefs.findTypeSpecFromPackagePaths(pkgPaths, externalPkgPaths, parts[1])
584-
/*
585-
TODO : remove
586-
if len(pkgPaths) == 0 && len(externalPkgPaths) == 0 {
587-
pkgDefinition := pkgDefs.packages["pkg/"+parts[0]]
588-
if pkgDefinition == nil {
589-
return pkgDefs.findTypeSpec("", parts[1])
590-
}
591-
592-
typeDef = pkgDefinition.TypeDefinitions[parts[1]]
593-
} else {
594-
typeDef = pkgDefs.findTypeSpecFromPackagePaths(pkgPaths, externalPkgPaths, parts[1])
595-
}
596-
597-
*/
583+
/*
584+
TODO : remove
585+
if len(pkgPaths) == 0 && len(externalPkgPaths) == 0 {
586+
pkgDefinition := pkgDefs.packages["pkg/"+parts[0]]
587+
if pkgDefinition == nil {
588+
return pkgDefs.findTypeSpec("", parts[1])
589+
}
590+
591+
typeDef = pkgDefinition.TypeDefinitions[parts[1]]
592+
} else {
593+
typeDef = pkgDefs.findTypeSpecFromPackagePaths(pkgPaths, externalPkgPaths, parts[1])
594+
}
595+
596+
*/
598597
return pkgDefs.parametrizeGenericType(file, typeDef, typeName)
599598
}
600599

parser.go

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,44 @@ const (
3535
// SnakeCase indicates using SnakeCase strategy for struct field.
3636
SnakeCase = "snakecase"
3737

38-
idAttr = "@id"
39-
acceptAttr = "@accept"
40-
produceAttr = "@produce"
41-
paramAttr = "@param"
42-
successAttr = "@success"
43-
failureAttr = "@failure"
44-
responseAttr = "@response"
45-
headerAttr = "@header"
46-
tagsAttr = "@tags"
47-
routerAttr = "@router"
48-
deprecatedRouterAttr = "@deprecatedrouter"
49-
50-
summaryAttr = "@summary"
51-
deprecatedAttr = "@deprecated"
52-
securityAttr = "@security"
53-
titleAttr = "@title"
54-
conNameAttr = "@contact.name"
55-
conURLAttr = "@contact.url"
56-
conEmailAttr = "@contact.email"
57-
licNameAttr = "@license.name"
58-
licURLAttr = "@license.url"
59-
versionAttr = "@version"
60-
descriptionAttr = "@description"
61-
descriptionMarkdownAttr = "@description.markdown"
62-
secBasicAttr = "@securitydefinitions.basic"
63-
secAPIKeyAttr = "@securitydefinitions.apikey"
64-
secApplicationAttr = "@securitydefinitions.oauth2.application"
65-
secImplicitAttr = "@securitydefinitions.oauth2.implicit"
66-
secPasswordAttr = "@securitydefinitions.oauth2.password"
67-
secAccessCodeAttr = "@securitydefinitions.oauth2.accesscode"
68-
tosAttr = "@termsofservice"
69-
extDocsDescAttr = "@externaldocs.description"
70-
extDocsURLAttr = "@externaldocs.url"
71-
xCodeSamplesAttr = "@x-codesamples"
72-
scopeAttrPrefix = "@scope."
73-
stateAttr = "@state"
38+
idAttr = "@id"
39+
acceptAttr = "@accept"
40+
produceAttr = "@produce"
41+
paramAttr = "@param"
42+
successAttr = "@success"
43+
failureAttr = "@failure"
44+
responseAttr = "@response"
45+
headerAttr = "@header"
46+
tagsAttr = "@tags"
47+
routerAttr = "@router"
48+
deprecatedRouterAttr = "@deprecatedrouter"
49+
50+
summaryAttr = "@summary"
51+
deprecatedAttr = "@deprecated"
52+
securityAttr = "@security"
53+
titleAttr = "@title"
54+
conNameAttr = "@contact.name"
55+
conURLAttr = "@contact.url"
56+
conEmailAttr = "@contact.email"
57+
licNameAttr = "@license.name"
58+
licURLAttr = "@license.url"
59+
versionAttr = "@version"
60+
descriptionAttr = "@description"
61+
descriptionMarkdownAttr = "@description.markdown"
62+
secBasicAttr = "@securitydefinitions.basic"
63+
secAPIKeyAttr = "@securitydefinitions.apikey"
64+
secBearerAuthAttr = "@securitydefinitions.bearerauth"
65+
secApplicationAttr = "@securitydefinitions.oauth2.application"
66+
secImplicitAttr = "@securitydefinitions.oauth2.implicit"
67+
secPasswordAttr = "@securitydefinitions.oauth2.password"
68+
secAccessCodeAttr = "@securitydefinitions.oauth2.accesscode"
69+
tosAttr = "@termsofservice"
70+
extDocsDescAttr = "@externaldocs.description"
71+
extDocsURLAttr = "@externaldocs.url"
72+
xCodeSamplesAttr = "@x-codesamples"
73+
xCodeSamplesAttrOriginal = "@x-codeSamples"
74+
scopeAttrPrefix = "@scope."
75+
stateAttr = "@state"
7476
)
7577

7678
// ParseFlag determine what to parse
@@ -198,7 +200,7 @@ type Parser struct {
198200
// ParseFuncBody whether swag should parse api info inside of funcs
199201
ParseFuncBody bool
200202

201-
// use new openAPI version
203+
// use new openAPI version
202204
openAPIVersion bool
203205
}
204206

@@ -517,7 +519,7 @@ func (parser *Parser) parseDeps(absMainAPIFilePath string, parseDepth int) error
517519

518520
length := len(pkgs)
519521
for i := 0; i < length; i++ {
520-
err := parser.getAllGoFileInfoFromDepsByList(pkgs[i])
522+
err := parser.getAllGoFileInfoFromDepsByList(pkgs[i], parser.ParseDependency)
521523
if err != nil {
522524
return err
523525
}
@@ -537,7 +539,7 @@ func (parser *Parser) parseDeps(absMainAPIFilePath string, parseDepth int) error
537539
}
538540

539541
for i := 0; i < len(t.Root.Deps); i++ {
540-
if err := parser.getAllGoFileInfoFromDeps(&t.Root.Deps[i]); err != nil {
542+
if err := parser.getAllGoFileInfoFromDeps(&t.Root.Deps[i], parser.ParseDependency); err != nil {
541543
return errors.Wrap(err, "could not parse dependencies")
542544
}
543545
}

parserv3.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ func (p *Parser) ParseDefinitionV3(typeSpecDef *TypeSpecDef) (*SchemaV3, error)
722722
}
723723

724724
if definition.Spec.Description == "" {
725-
fillDefinitionDescriptionV3(definition.Spec, typeSpecDef.File, typeSpecDef)
725+
fillDefinitionDescriptionV3(p, definition.Spec, typeSpecDef.File, typeSpecDef)
726726
}
727727

728728
if len(typeSpecDef.Enums) > 0 {
@@ -764,7 +764,7 @@ func (p *Parser) ParseDefinitionV3(typeSpecDef *TypeSpecDef) (*SchemaV3, error)
764764

765765
// fillDefinitionDescription additionally fills fields in definition (spec.Schema)
766766
// TODO: If .go file contains many types, it may work for a long time
767-
func fillDefinitionDescriptionV3(definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) {
767+
func fillDefinitionDescriptionV3(parser *Parser, definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) {
768768
for _, astDeclaration := range file.Decls {
769769
generalDeclaration, ok := astDeclaration.(*ast.GenDecl)
770770
if !ok || generalDeclaration.Tok != token.TYPE {
@@ -777,8 +777,18 @@ func fillDefinitionDescriptionV3(definition *spec.Schema, file *ast.File, typeSp
777777
continue
778778
}
779779

780-
definition.Description =
781-
extractDeclarationDescription(typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc)
780+
var typeName string
781+
if typeSpec.Name != nil {
782+
typeName = typeSpec.Name.Name
783+
}
784+
785+
text, err := parser.extractDeclarationDescription(typeName, typeSpec.Comment, generalDeclaration.Doc)
786+
if err != nil {
787+
parser.debug.Printf("Error extracting declaration description: %s", err)
788+
continue
789+
}
790+
791+
definition.Description = text
782792
}
783793
}
784794
}

0 commit comments

Comments
 (0)