Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operationId uniqueness check #732

Merged
merged 7 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Parser struct {

// excludes excludes dirs and files in SearchDir
excludes map[string]bool

// operationsIds contains all operationId annotations to check it's unique
operationsIds map[string]string
}

// New creates a new Parser with default properties.
Expand All @@ -100,6 +103,7 @@ func New(options ...func(*Parser)) *Parser {
CustomPrimitiveTypes: make(map[string]string),
registerTypes: make(map[string]*ast.TypeSpec),
excludes: make(map[string]bool),
operationsIds: make(map[string]string),
}

for _, option := range options {
Expand Down Expand Up @@ -174,6 +178,10 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
}
}

if err := parser.checkOperationIDUniqueness(); err != nil {
return err
}

return parser.parseDefinitions()
}

Expand Down Expand Up @@ -1500,6 +1508,67 @@ func (parser *Parser) parseFile(path string) error {
return nil
}

func (parser *Parser) checkOperationIDUniqueness() error {
for path, itm := range parser.swagger.Paths.Paths {
if itm.Get != nil {
currentPath := fmt.Sprintf("%s %s", "GET", path)
if err := parser.saveOperationID(itm.Get.ID, currentPath); err != nil {
return err
}
}
if itm.Put != nil {
currentPath := fmt.Sprintf("%s %s", "PUT", path)
if err := parser.saveOperationID(itm.Put.ID, currentPath); err != nil {
return err
}
}
if itm.Post != nil {
currentPath := fmt.Sprintf("%s %s", "POST", path)
if err := parser.saveOperationID(itm.Post.ID, currentPath); err != nil {
return err
}
}
if itm.Delete != nil {
currentPath := fmt.Sprintf("%s %s", "DELETE", path)
if err := parser.saveOperationID(itm.Delete.ID, currentPath); err != nil {
return err
}
}
if itm.Options != nil {
currentPath := fmt.Sprintf("%s %s", "OPTIONS", path)
if err := parser.saveOperationID(itm.Options.ID, currentPath); err != nil {
return err
}
}
if itm.Head != nil {
currentPath := fmt.Sprintf("%s %s", "HEAD", path)
if err := parser.saveOperationID(itm.Head.ID, currentPath); err != nil {
return err
}
}
if itm.Patch != nil {
currentPath := fmt.Sprintf("%s %s", "PATCH", path)
if err := parser.saveOperationID(itm.Patch.ID, currentPath); err != nil {
return err
}
}
}
return nil
}

func (parser *Parser) saveOperationID(operationID, currentPath string) error {
if operationID == "" {
return nil
}
if previousPath, ok := parser.operationsIds[operationID]; ok {
return fmt.Errorf(
"duplicated @id annotation '%s' found in '%s', previously declared in: '%s'",
operationID, currentPath, previousPath)
}
parser.operationsIds[operationID] = currentPath
return nil
}

// Skip returns filepath.SkipDir error if match vendor and hidden folder
func (parser *Parser) Skip(path string, f os.FileInfo) error {
if f.IsDir() {
Expand Down
9 changes: 9 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,15 @@ func TestParseNested(t *testing.T) {
assert.Equal(t, string(expected), string(b))
}

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

func TestParser_ParseStructArrayObject(t *testing.T) {
src := `
package api
Expand Down
17 changes: 17 additions & 0 deletions testdata/duplicated/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package api

import (
"github.com/gin-gonic/gin"
)

// @Description get Foo
// @ID get-foo
// @Success 200 {string} string
// @Router /testapi/get-foo [get]
func GetFoo(c *gin.Context) {}

// @Description post Bar
// @ID get-foo
// @Success 200 {string} string
// @Router /testapi/post-bar [post]
func PostBar(c *gin.Context) {}
22 changes: 22 additions & 0 deletions testdata/duplicated/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package composition

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

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

// @title Swagger Example API
// @version 1.0
// @description This is a sample server
// @termsOfService http://swagger.io/terms/

// @host petstore.swagger.io
// @BasePath /v2

func main() {
r := gin.New()
r.GET("/testapi/get-foo", api.GetFoo)
r.POST("/testapi/post-bar", api.PostBar)
r.Run()
}