-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package swag | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
|
@@ -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. | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
if swags == nil { | ||
return "", errors.New("no swag has yet been registered") | ||
} else if swag, ok := swags[name]; !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Race condition: searching in the |
||
return "", fmt.Errorf("no swag named \"%s\" was registered", name) | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,5 +185,5 @@ func TestCalledTwicelRegister(t *testing.T) { | |
} | ||
|
||
func setup() { | ||
swag = nil | ||
swags = make(map[string]Swagger) | ||
} |
There was a problem hiding this comment.
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?