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

Allow different names for swagger specs #885

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,6 @@ func (s *s) ReadDoc() string {
}

func init() {
swag.Register(swag.Name, &s{})
swag.Register({{ printf "%q + %q" .Host .BasePath}}, &s{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the expected behavior In the event of many API applications don't have a Host and Basepath defined?

}
`
24 changes: 19 additions & 5 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package swag

import (
"errors"
"fmt"
"sync"
)

Expand All @@ -10,7 +11,7 @@ const Name = "swagger"

var (
swaggerMu sync.RWMutex
swag Swagger
swags map[string]Swagger
)

// Swagger is a interface to read swagger document.
Expand All @@ -22,20 +23,33 @@ type Swagger interface {
func Register(name string, swagger Swagger) {
swaggerMu.Lock()
defer swaggerMu.Unlock()

if swagger == nil {
panic("swagger is nil")
}

if swag != nil {
if swags == nil {
swags = make(map[string]Swagger)
}

if _, ok := swags[name]; ok {
panic("Register called twice for swag: " + name)
}
swag = swagger
swags[name] = swagger
}

// ReadDoc reads swagger document.
func ReadDoc() (string, error) {
if swag != nil {
return ReadDocName(Name)
}

// ReadDocName reads swagger document with specific name.
func ReadDocName(name string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the function signature is not an acceptable solution. This is a breaking change and breaks all other projects.
func ReadDoc() (string, error) {
to
func ReadDocName(name string) (string, error) {

if swags == nil {
return "", errors.New("no swag has yet been registered")
} else if swag, ok := swags[name]; !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race condition: searching in the swags map without a mutex !

return "", fmt.Errorf("no swag named \"%s\" was registered", name)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use return instead if nested else.

return swag.ReadDoc(), nil
}
return "", errors.New("not yet registered swag")
}
2 changes: 1 addition & 1 deletion swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ func TestCalledTwicelRegister(t *testing.T) {
}

func setup() {
swag = nil
swags = make(map[string]Swagger)
}