Skip to content

Commit

Permalink
refactor: remove dependency on gin (#800)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
akojo committed Nov 22, 2020
1 parent c9056f0 commit 3d083d3
Show file tree
Hide file tree
Showing 33 changed files with 217 additions and 373 deletions.
1 change: 0 additions & 1 deletion Makefile
Expand Up @@ -56,7 +56,6 @@ clean:
deps:
$(GOGET) github.com/swaggo/cli
$(GOGET) github.com/ghodss/yaml
$(GOGET) github.com/gin-gonic/gin
$(GOGET) github.com/KyleBanks/depth
$(GOGET) github.com/go-openapi/jsonreference
$(GOGET) github.com/go-openapi/spec
Expand Down
12 changes: 7 additions & 5 deletions example/basic/api/api.go
@@ -1,7 +1,9 @@
package api

import (
"github.com/gin-gonic/gin"
"encoding/json"
"net/http"

"github.com/swaggo/swag/example/basic/web"
)

Expand All @@ -17,9 +19,9 @@ import (
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /testapi/get-string-by-int/{some_id} [get]
func GetStringByInt(c *gin.Context) {
func GetStringByInt(w http.ResponseWriter, r *http.Request) {
var pet web.Pet
if err := c.ShouldBindJSON(&pet); err != nil {
if err := json.NewDecoder(r.Body).Decode(&pet); err != nil {
// write your code
return
}
Expand All @@ -39,7 +41,7 @@ func GetStringByInt(c *gin.Context) {
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /testapi/get-struct-array-by-string/{some_id} [get]
func GetStructArrayByString(c *gin.Context) {
func GetStructArrayByString(w http.ResponseWriter, r *http.Request) {
// write your code
}

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

Expand Down
13 changes: 6 additions & 7 deletions example/basic/main.go
@@ -1,7 +1,8 @@
package main

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/swaggo/swag/example/basic/api"
)

Expand All @@ -20,10 +21,8 @@ import (
// @host petstore.swagger.io
// @BasePath /v2
func main() {
r := gin.New()
r.GET("/testapi/get-string-by-int/:some_id", api.GetStringByInt)
r.GET("//testapi/get-struct-array-by-string/:some_id", api.GetStructArrayByString)
r.POST("/testapi/upload", api.Upload)
r.Run()

http.HandleFunc("/testapi/get-string-by-int/", api.GetStringByInt)
http.HandleFunc("//testapi/get-struct-array-by-string/", api.GetStructArrayByString)
http.HandleFunc("/testapi/upload", api.Upload)
http.ListenAndServe(":8080", nil)
}
19 changes: 3 additions & 16 deletions go.mod
Expand Up @@ -4,26 +4,13 @@ require (
github.com/KyleBanks/depth v1.2.1
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/ghodss/yaml v1.0.0
github.com/gin-gonic/gin v1.6.3
github.com/go-openapi/spec v0.19.14
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/gofrs/uuid v3.3.0+incompatible
github.com/shopspring/decimal v1.2.0
github.com/golang/protobuf v1.4.3 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/shopspring/decimal v1.2.0
github.com/stretchr/testify v1.6.1
github.com/swaggo/cli v1.22.2 // indirect
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/gin-swagger v1.3.0
github.com/urfave/cli/v2 v2.2.0
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
golang.org/x/net v0.0.0-20201027133719-8eef5233e2a1 // indirect
golang.org/x/sys v0.0.0-20201028094953-708e7fb298ac // indirect
github.com/urfave/cli/v2 v2.3.0
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/tools v0.0.0-20201118030313-598b068a9102
google.golang.org/protobuf v1.25.0 // indirect
golang.org/x/tools v0.0.0-20201120155355-20be4ac4bd6e
)

go 1.13
207 changes: 9 additions & 198 deletions go.sum

Large diffs are not rendered by default.

45 changes: 15 additions & 30 deletions parser.go
Expand Up @@ -1393,49 +1393,34 @@ func (parser *Parser) checkOperationIDUniqueness() error {
operationsIds[operationID] = currentPath
return nil
}

for path, itm := range parser.swagger.Paths.Paths {
getOperationID := func(itm spec.PathItem) (string, string) {
if itm.Get != nil {
currentPath := fmt.Sprintf("%s %s", "GET", path)
if err := saveOperationID(itm.Get.ID, currentPath); err != nil {
return err
}
return "GET", itm.Get.ID
}
if itm.Put != nil {
currentPath := fmt.Sprintf("%s %s", "PUT", path)
if err := saveOperationID(itm.Put.ID, currentPath); err != nil {
return err
}
return "PUT", itm.Put.ID
}
if itm.Post != nil {
currentPath := fmt.Sprintf("%s %s", "POST", path)
if err := saveOperationID(itm.Post.ID, currentPath); err != nil {
return err
}
return "POST", itm.Post.ID
}
if itm.Delete != nil {
currentPath := fmt.Sprintf("%s %s", "DELETE", path)
if err := saveOperationID(itm.Delete.ID, currentPath); err != nil {
return err
}
return "DELETE", itm.Delete.ID
}
if itm.Options != nil {
currentPath := fmt.Sprintf("%s %s", "OPTIONS", path)
if err := saveOperationID(itm.Options.ID, currentPath); err != nil {
return err
}
return "OPTIONS", itm.Options.ID
}
if itm.Head != nil {
currentPath := fmt.Sprintf("%s %s", "HEAD", path)
if err := saveOperationID(itm.Head.ID, currentPath); err != nil {
return err
}
return "HEAD", itm.Head.ID
}
if itm.Patch != nil {
currentPath := fmt.Sprintf("%s %s", "PATCH", path)
if err := saveOperationID(itm.Patch.ID, currentPath); err != nil {
return err
}
return "PATCH", itm.Patch.ID
}
return "", ""
}
for path, itm := range parser.swagger.Paths.Paths {
method, id := getOperationID(itm)
if err := saveOperationID(id, fmt.Sprintf("%s %s", method, path)); err != nil {
return err
}
}
return nil
Expand Down
9 changes: 9 additions & 0 deletions parser_test.go
Expand Up @@ -1563,6 +1563,15 @@ func TestParseDuplicated(t *testing.T) {
assert.Errorf(t, err, "duplicated @id declarations successfully found")
}

func TestParseDuplicatedOtherMethods(t *testing.T) {
searchDir := "testdata/duplicated2"
mainAPIFile := "main.go"
p := New()
p.ParseDependency = true
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.Errorf(t, err, "duplicated @id declarations successfully found")
}

func TestParseConflictSchemaName(t *testing.T) {
searchDir := "testdata/conflict_name"
mainAPIFile := "main.go"
Expand Down
7 changes: 4 additions & 3 deletions testdata/alias_import/api/api.go
@@ -1,10 +1,11 @@
package api

import (
"github.com/gin-gonic/gin"
"log"
"net/http"

"github.com/swaggo/swag/testdata/alias_import/data"
"github.com/swaggo/swag/testdata/alias_type/types"
"log"
)

// @Summary Get application
Expand All @@ -14,7 +15,7 @@ import (
// @Produce json
// @Success 200 {object} data.ApplicationResponse "ok"
// @Router /testapi/application [get]
func GetApplication(c *gin.Context) {
func GetApplication(w http.ResponseWriter, r *http.Request) {
var foo = data.ApplicationResponse{
Application: types.Application{
Name: "name",
Expand Down
8 changes: 4 additions & 4 deletions testdata/alias_import/main.go
@@ -1,7 +1,8 @@
package alias_import

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/swaggo/swag/testdata/alias_import/api"
)

Expand All @@ -20,7 +21,6 @@ import (
// @host petstore.swagger.io
// @BasePath /v2
func main() {
r := gin.New()
r.GET("/testapi/application", api.GetApplication)
r.Run()
http.HandleFunc("/testapi/application", api.GetApplication)
http.ListenAndServe(":8080", nil)
}
11 changes: 6 additions & 5 deletions testdata/alias_type/api/api.go
@@ -1,10 +1,11 @@
package api

import (
"github.com/gin-gonic/gin"
"github.com/swaggo/swag/testdata/alias_type/data"
"log"
"net/http"
"time"

"github.com/swaggo/swag/testdata/alias_type/data"
)

/*// @Summary Get time as string
Expand All @@ -14,7 +15,7 @@ import (
// @Produce json
// @Success 200 {object} data.StringAlias "ok"
// @Router /testapi/time-as-string [get]
func GetTimeAsStringAlias(c *gin.Context) {
func GetTimeAsStringAlias(w http.ResponseWriter, r *http.Request) {
var foo data.StringAlias = "test"
log.Println(foo)
//write your code
Expand All @@ -27,7 +28,7 @@ func GetTimeAsStringAlias(c *gin.Context) {
// @Produce json
// @Success 200 {object} data.DateOnly "ok"
// @Router /testapi/time-as-time [get]
func GetTimeAsTimeAlias(c *gin.Context) {
func GetTimeAsTimeAlias(w http.ResponseWriter, r *http.Request) {
var foo = data.DateOnly(time.Now())
log.Println(foo)
//write your code
Expand All @@ -40,7 +41,7 @@ func GetTimeAsTimeAlias(c *gin.Context) {
// @Produce json
// @Success 200 {object} data.TimeContainer "ok"
// @Router /testapi/time-as-time-container [get]
func GetTimeAsTimeContainer(c *gin.Context) {
func GetTimeAsTimeContainer(w http.ResponseWriter, r *http.Request) {
now := time.Now()
var foo = data.TimeContainer{
Name: "test",
Expand Down
8 changes: 4 additions & 4 deletions testdata/alias_type/main.go
@@ -1,7 +1,8 @@
package alias_type

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/swaggo/swag/testdata/alias_type/api"
)

Expand All @@ -20,7 +21,6 @@ import (
// @host petstore.swagger.io
// @BasePath /v2
func main() {
r := gin.New()
r.GET("/testapi/time-as-time-container", api.GetTimeAsTimeContainer)
r.Run()
http.HandleFunc("/testapi/time-as-time-container", api.GetTimeAsTimeContainer)
http.ListenAndServe(":8080", nil)
}
15 changes: 8 additions & 7 deletions testdata/composition/api/api.go
@@ -1,7 +1,8 @@
package api

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/swaggo/swag/testdata/composition/common"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ type MapValue struct {
// @Produce json
// @Success 200 {object} api.Foo
// @Router /testapi/get-foo [get]
func GetFoo(c *gin.Context) {
func GetFoo(w http.ResponseWriter, r *http.Request) {
//write your code
var _ = Foo{}
}
Expand All @@ -63,7 +64,7 @@ func GetFoo(c *gin.Context) {
// @Produce json
// @Success 200 {object} api.Bar
// @Router /testapi/get-bar [get]
func GetBar(c *gin.Context) {
func GetBar(w http.ResponseWriter, r *http.Request) {
//write your code
var _ = Bar{}
}
Expand All @@ -74,7 +75,7 @@ func GetBar(c *gin.Context) {
// @Produce json
// @Success 200 {object} api.FooBar
// @Router /testapi/get-foobar [get]
func GetFooBar(c *gin.Context) {
func GetFooBar(w http.ResponseWriter, r *http.Request) {
//write your code
var _ = FooBar{}
}
Expand All @@ -85,7 +86,7 @@ func GetFooBar(c *gin.Context) {
// @Produce json
// @Success 200 {object} api.FooBarPointer
// @Router /testapi/get-foobar-pointer [get]
func GetFooBarPointer(c *gin.Context) {
func GetFooBarPointer(w http.ResponseWriter, r *http.Request) {
//write your code
var _ = FooBarPointer{}
}
Expand All @@ -96,7 +97,7 @@ func GetFooBarPointer(c *gin.Context) {
// @Produce json
// @Success 200 {object} api.BarMap
// @Router /testapi/get-barmap [get]
func GetBarMap(c *gin.Context) {
func GetBarMap(w http.ResponseWriter, r *http.Request) {
//write your code
var _ = BarMap{}
}
Expand All @@ -107,7 +108,7 @@ func GetBarMap(c *gin.Context) {
// @Produce json
// @Success 200 {object} api.FooBarMap
// @Router /testapi/get-foobarmap [get]
func GetFooBarMap(c *gin.Context) {
func GetFooBarMap(w http.ResponseWriter, r *http.Request) {
//write your code
var _ = FooBarMap{}
}
16 changes: 8 additions & 8 deletions testdata/composition/main.go
@@ -1,7 +1,8 @@
package composition

import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/swaggo/swag/testdata/composition/api"
)

Expand All @@ -14,11 +15,10 @@ import (
// @BasePath /v2

func main() {
r := gin.New()
r.GET("/testapi/get-foo", api.GetFoo)
r.GET("/testapi/get-bar", api.GetBar)
r.GET("/testapi/get-foobar", api.GetFooBar)
r.GET("/testapi/get-foobar-pointer", api.GetFooBarPointer)
r.GET("/testapi/get-barmap", api.GetBarMap)
r.Run()
http.handleFunc("/testapi/get-foo", api.GetFoo)
http.handleFunc("/testapi/get-bar", api.GetBar)
http.handleFunc("/testapi/get-foobar", api.GetFooBar)
http.handleFunc("/testapi/get-foobar-pointer", api.GetFooBarPointer)
http.handleFunc("/testapi/get-barmap", api.GetBarMap)
http.ListenAndServe(":8080", nil)
}

0 comments on commit 3d083d3

Please sign in to comment.