Skip to content

Commit 3d083d3

Browse files
authored
refactor: remove dependency on gin (#800)
* Remove dependency on gin from tests Since tests only test parsing of go source and swaggo special comments, an advanced router is not really necessary * Clean up modules * Add inner package to testdata/nested2 Since testdata/nested doesn't point to outside packages anymore, this caused nested parsing test to skip exercising one if-branch. Add more nesting to nested2 to regain that. * Refactor Parser.checkOperationIDUniqueness Remove duplication in code that checks for duplicate operation ids * Better duplicate id detection test coverage Also exercise rest of HTTP methods when testing for duplicate @id detection
1 parent c9056f0 commit 3d083d3

File tree

33 files changed

+217
-373
lines changed

33 files changed

+217
-373
lines changed

Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ clean:
5656
deps:
5757
$(GOGET) github.com/swaggo/cli
5858
$(GOGET) github.com/ghodss/yaml
59-
$(GOGET) github.com/gin-gonic/gin
6059
$(GOGET) github.com/KyleBanks/depth
6160
$(GOGET) github.com/go-openapi/jsonreference
6261
$(GOGET) github.com/go-openapi/spec

example/basic/api/api.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package api
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"encoding/json"
5+
"net/http"
6+
57
"github.com/swaggo/swag/example/basic/web"
68
)
79

@@ -17,9 +19,9 @@ import (
1719
// @Failure 400 {object} web.APIError "We need ID!!"
1820
// @Failure 404 {object} web.APIError "Can not find ID"
1921
// @Router /testapi/get-string-by-int/{some_id} [get]
20-
func GetStringByInt(c *gin.Context) {
22+
func GetStringByInt(w http.ResponseWriter, r *http.Request) {
2123
var pet web.Pet
22-
if err := c.ShouldBindJSON(&pet); err != nil {
24+
if err := json.NewDecoder(r.Body).Decode(&pet); err != nil {
2325
// write your code
2426
return
2527
}
@@ -39,7 +41,7 @@ func GetStringByInt(c *gin.Context) {
3941
// @Failure 400 {object} web.APIError "We need ID!!"
4042
// @Failure 404 {object} web.APIError "Can not find ID"
4143
// @Router /testapi/get-struct-array-by-string/{some_id} [get]
42-
func GetStructArrayByString(c *gin.Context) {
44+
func GetStructArrayByString(w http.ResponseWriter, r *http.Request) {
4345
// write your code
4446
}
4547

@@ -54,7 +56,7 @@ func GetStructArrayByString(c *gin.Context) {
5456
// @Failure 400 {object} web.APIError "We need ID!!"
5557
// @Failure 404 {object} web.APIError "Can not find ID"
5658
// @Router /file/upload [post]
57-
func Upload(ctx *gin.Context) {
59+
func Upload(w http.ResponseWriter, r *http.Request) {
5860
// write your code
5961
}
6062

example/basic/main.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package main
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"net/http"
5+
56
"github.com/swaggo/swag/example/basic/api"
67
)
78

@@ -20,10 +21,8 @@ import (
2021
// @host petstore.swagger.io
2122
// @BasePath /v2
2223
func main() {
23-
r := gin.New()
24-
r.GET("/testapi/get-string-by-int/:some_id", api.GetStringByInt)
25-
r.GET("//testapi/get-struct-array-by-string/:some_id", api.GetStructArrayByString)
26-
r.POST("/testapi/upload", api.Upload)
27-
r.Run()
28-
24+
http.HandleFunc("/testapi/get-string-by-int/", api.GetStringByInt)
25+
http.HandleFunc("//testapi/get-struct-array-by-string/", api.GetStructArrayByString)
26+
http.HandleFunc("/testapi/upload", api.Upload)
27+
http.ListenAndServe(":8080", nil)
2928
}

go.mod

+3-16
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,13 @@ require (
44
github.com/KyleBanks/depth v1.2.1
55
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
66
github.com/ghodss/yaml v1.0.0
7-
github.com/gin-gonic/gin v1.6.3
87
github.com/go-openapi/spec v0.19.14
9-
github.com/go-playground/validator/v10 v10.4.1 // indirect
108
github.com/gofrs/uuid v3.3.0+incompatible
11-
github.com/shopspring/decimal v1.2.0
12-
github.com/golang/protobuf v1.4.3 // indirect
13-
github.com/json-iterator/go v1.1.10 // indirect
14-
github.com/mailru/easyjson v0.7.6 // indirect
15-
github.com/shopspring/decimal v1.2.0
169
github.com/stretchr/testify v1.6.1
17-
github.com/swaggo/cli v1.22.2 // indirect
18-
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
19-
github.com/swaggo/gin-swagger v1.3.0
20-
github.com/urfave/cli/v2 v2.2.0
21-
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
22-
golang.org/x/net v0.0.0-20201027133719-8eef5233e2a1 // indirect
23-
golang.org/x/sys v0.0.0-20201028094953-708e7fb298ac // indirect
10+
github.com/urfave/cli/v2 v2.3.0
11+
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
2412
golang.org/x/text v0.3.4 // indirect
25-
golang.org/x/tools v0.0.0-20201118030313-598b068a9102
26-
google.golang.org/protobuf v1.25.0 // indirect
13+
golang.org/x/tools v0.0.0-20201120155355-20be4ac4bd6e
2714
)
2815

2916
go 1.13

go.sum

+9-198
Large diffs are not rendered by default.

parser.go

+15-30
Original file line numberDiff line numberDiff line change
@@ -1393,49 +1393,34 @@ func (parser *Parser) checkOperationIDUniqueness() error {
13931393
operationsIds[operationID] = currentPath
13941394
return nil
13951395
}
1396-
1397-
for path, itm := range parser.swagger.Paths.Paths {
1396+
getOperationID := func(itm spec.PathItem) (string, string) {
13981397
if itm.Get != nil {
1399-
currentPath := fmt.Sprintf("%s %s", "GET", path)
1400-
if err := saveOperationID(itm.Get.ID, currentPath); err != nil {
1401-
return err
1402-
}
1398+
return "GET", itm.Get.ID
14031399
}
14041400
if itm.Put != nil {
1405-
currentPath := fmt.Sprintf("%s %s", "PUT", path)
1406-
if err := saveOperationID(itm.Put.ID, currentPath); err != nil {
1407-
return err
1408-
}
1401+
return "PUT", itm.Put.ID
14091402
}
14101403
if itm.Post != nil {
1411-
currentPath := fmt.Sprintf("%s %s", "POST", path)
1412-
if err := saveOperationID(itm.Post.ID, currentPath); err != nil {
1413-
return err
1414-
}
1404+
return "POST", itm.Post.ID
14151405
}
14161406
if itm.Delete != nil {
1417-
currentPath := fmt.Sprintf("%s %s", "DELETE", path)
1418-
if err := saveOperationID(itm.Delete.ID, currentPath); err != nil {
1419-
return err
1420-
}
1407+
return "DELETE", itm.Delete.ID
14211408
}
14221409
if itm.Options != nil {
1423-
currentPath := fmt.Sprintf("%s %s", "OPTIONS", path)
1424-
if err := saveOperationID(itm.Options.ID, currentPath); err != nil {
1425-
return err
1426-
}
1410+
return "OPTIONS", itm.Options.ID
14271411
}
14281412
if itm.Head != nil {
1429-
currentPath := fmt.Sprintf("%s %s", "HEAD", path)
1430-
if err := saveOperationID(itm.Head.ID, currentPath); err != nil {
1431-
return err
1432-
}
1413+
return "HEAD", itm.Head.ID
14331414
}
14341415
if itm.Patch != nil {
1435-
currentPath := fmt.Sprintf("%s %s", "PATCH", path)
1436-
if err := saveOperationID(itm.Patch.ID, currentPath); err != nil {
1437-
return err
1438-
}
1416+
return "PATCH", itm.Patch.ID
1417+
}
1418+
return "", ""
1419+
}
1420+
for path, itm := range parser.swagger.Paths.Paths {
1421+
method, id := getOperationID(itm)
1422+
if err := saveOperationID(id, fmt.Sprintf("%s %s", method, path)); err != nil {
1423+
return err
14391424
}
14401425
}
14411426
return nil

parser_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,15 @@ func TestParseDuplicated(t *testing.T) {
15631563
assert.Errorf(t, err, "duplicated @id declarations successfully found")
15641564
}
15651565

1566+
func TestParseDuplicatedOtherMethods(t *testing.T) {
1567+
searchDir := "testdata/duplicated2"
1568+
mainAPIFile := "main.go"
1569+
p := New()
1570+
p.ParseDependency = true
1571+
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
1572+
assert.Errorf(t, err, "duplicated @id declarations successfully found")
1573+
}
1574+
15661575
func TestParseConflictSchemaName(t *testing.T) {
15671576
searchDir := "testdata/conflict_name"
15681577
mainAPIFile := "main.go"

testdata/alias_import/api/api.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package api
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"log"
5+
"net/http"
6+
57
"github.com/swaggo/swag/testdata/alias_import/data"
68
"github.com/swaggo/swag/testdata/alias_type/types"
7-
"log"
89
)
910

1011
// @Summary Get application
@@ -14,7 +15,7 @@ import (
1415
// @Produce json
1516
// @Success 200 {object} data.ApplicationResponse "ok"
1617
// @Router /testapi/application [get]
17-
func GetApplication(c *gin.Context) {
18+
func GetApplication(w http.ResponseWriter, r *http.Request) {
1819
var foo = data.ApplicationResponse{
1920
Application: types.Application{
2021
Name: "name",

testdata/alias_import/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package alias_import
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"net/http"
5+
56
"github.com/swaggo/swag/testdata/alias_import/api"
67
)
78

@@ -20,7 +21,6 @@ import (
2021
// @host petstore.swagger.io
2122
// @BasePath /v2
2223
func main() {
23-
r := gin.New()
24-
r.GET("/testapi/application", api.GetApplication)
25-
r.Run()
24+
http.HandleFunc("/testapi/application", api.GetApplication)
25+
http.ListenAndServe(":8080", nil)
2626
}

testdata/alias_type/api/api.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package api
22

33
import (
4-
"github.com/gin-gonic/gin"
5-
"github.com/swaggo/swag/testdata/alias_type/data"
64
"log"
5+
"net/http"
76
"time"
7+
8+
"github.com/swaggo/swag/testdata/alias_type/data"
89
)
910

1011
/*// @Summary Get time as string
@@ -14,7 +15,7 @@ import (
1415
// @Produce json
1516
// @Success 200 {object} data.StringAlias "ok"
1617
// @Router /testapi/time-as-string [get]
17-
func GetTimeAsStringAlias(c *gin.Context) {
18+
func GetTimeAsStringAlias(w http.ResponseWriter, r *http.Request) {
1819
var foo data.StringAlias = "test"
1920
log.Println(foo)
2021
//write your code
@@ -27,7 +28,7 @@ func GetTimeAsStringAlias(c *gin.Context) {
2728
// @Produce json
2829
// @Success 200 {object} data.DateOnly "ok"
2930
// @Router /testapi/time-as-time [get]
30-
func GetTimeAsTimeAlias(c *gin.Context) {
31+
func GetTimeAsTimeAlias(w http.ResponseWriter, r *http.Request) {
3132
var foo = data.DateOnly(time.Now())
3233
log.Println(foo)
3334
//write your code
@@ -40,7 +41,7 @@ func GetTimeAsTimeAlias(c *gin.Context) {
4041
// @Produce json
4142
// @Success 200 {object} data.TimeContainer "ok"
4243
// @Router /testapi/time-as-time-container [get]
43-
func GetTimeAsTimeContainer(c *gin.Context) {
44+
func GetTimeAsTimeContainer(w http.ResponseWriter, r *http.Request) {
4445
now := time.Now()
4546
var foo = data.TimeContainer{
4647
Name: "test",

testdata/alias_type/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package alias_type
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"net/http"
5+
56
"github.com/swaggo/swag/testdata/alias_type/api"
67
)
78

@@ -20,7 +21,6 @@ import (
2021
// @host petstore.swagger.io
2122
// @BasePath /v2
2223
func main() {
23-
r := gin.New()
24-
r.GET("/testapi/time-as-time-container", api.GetTimeAsTimeContainer)
25-
r.Run()
24+
http.HandleFunc("/testapi/time-as-time-container", api.GetTimeAsTimeContainer)
25+
http.ListenAndServe(":8080", nil)
2626
}

testdata/composition/api/api.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package api
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"net/http"
5+
56
"github.com/swaggo/swag/testdata/composition/common"
67
)
78

@@ -52,7 +53,7 @@ type MapValue struct {
5253
// @Produce json
5354
// @Success 200 {object} api.Foo
5455
// @Router /testapi/get-foo [get]
55-
func GetFoo(c *gin.Context) {
56+
func GetFoo(w http.ResponseWriter, r *http.Request) {
5657
//write your code
5758
var _ = Foo{}
5859
}
@@ -63,7 +64,7 @@ func GetFoo(c *gin.Context) {
6364
// @Produce json
6465
// @Success 200 {object} api.Bar
6566
// @Router /testapi/get-bar [get]
66-
func GetBar(c *gin.Context) {
67+
func GetBar(w http.ResponseWriter, r *http.Request) {
6768
//write your code
6869
var _ = Bar{}
6970
}
@@ -74,7 +75,7 @@ func GetBar(c *gin.Context) {
7475
// @Produce json
7576
// @Success 200 {object} api.FooBar
7677
// @Router /testapi/get-foobar [get]
77-
func GetFooBar(c *gin.Context) {
78+
func GetFooBar(w http.ResponseWriter, r *http.Request) {
7879
//write your code
7980
var _ = FooBar{}
8081
}
@@ -85,7 +86,7 @@ func GetFooBar(c *gin.Context) {
8586
// @Produce json
8687
// @Success 200 {object} api.FooBarPointer
8788
// @Router /testapi/get-foobar-pointer [get]
88-
func GetFooBarPointer(c *gin.Context) {
89+
func GetFooBarPointer(w http.ResponseWriter, r *http.Request) {
8990
//write your code
9091
var _ = FooBarPointer{}
9192
}
@@ -96,7 +97,7 @@ func GetFooBarPointer(c *gin.Context) {
9697
// @Produce json
9798
// @Success 200 {object} api.BarMap
9899
// @Router /testapi/get-barmap [get]
99-
func GetBarMap(c *gin.Context) {
100+
func GetBarMap(w http.ResponseWriter, r *http.Request) {
100101
//write your code
101102
var _ = BarMap{}
102103
}
@@ -107,7 +108,7 @@ func GetBarMap(c *gin.Context) {
107108
// @Produce json
108109
// @Success 200 {object} api.FooBarMap
109110
// @Router /testapi/get-foobarmap [get]
110-
func GetFooBarMap(c *gin.Context) {
111+
func GetFooBarMap(w http.ResponseWriter, r *http.Request) {
111112
//write your code
112113
var _ = FooBarMap{}
113114
}

testdata/composition/main.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package composition
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"net/http"
5+
56
"github.com/swaggo/swag/testdata/composition/api"
67
)
78

@@ -14,11 +15,10 @@ import (
1415
// @BasePath /v2
1516

1617
func main() {
17-
r := gin.New()
18-
r.GET("/testapi/get-foo", api.GetFoo)
19-
r.GET("/testapi/get-bar", api.GetBar)
20-
r.GET("/testapi/get-foobar", api.GetFooBar)
21-
r.GET("/testapi/get-foobar-pointer", api.GetFooBarPointer)
22-
r.GET("/testapi/get-barmap", api.GetBarMap)
23-
r.Run()
18+
http.handleFunc("/testapi/get-foo", api.GetFoo)
19+
http.handleFunc("/testapi/get-bar", api.GetBar)
20+
http.handleFunc("/testapi/get-foobar", api.GetFooBar)
21+
http.handleFunc("/testapi/get-foobar-pointer", api.GetFooBarPointer)
22+
http.handleFunc("/testapi/get-barmap", api.GetBarMap)
23+
http.ListenAndServe(":8080", nil)
2424
}

0 commit comments

Comments
 (0)