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

Feature/security or #1157

Merged
merged 5 commits into from
Mar 8, 2022
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,14 @@ Make it AND condition
// @Security OAuth2Application[write, admin]
```
Make it OR condition
```go
// @Security ApiKeyAuth || firebase
// @Security OAuth2Application[write, admin] || APIKeyAuth
```
### Add a description for enum items
```go
Expand Down
38 changes: 20 additions & 18 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,27 +631,29 @@ func (operation *Operation) ParseRouterComment(commentLine string) error {

// ParseSecurityComment parses comment for given `security` comment string.
func (operation *Operation) ParseSecurityComment(commentLine string) error {
//var securityMap map[string][]string = map[string][]string{}

var securityMap = make(map[string][]string)
securitySource := commentLine[strings.Index(commentLine, "@Security")+1:]
l := strings.Index(securitySource, "[")
r := strings.Index(securitySource, "]")
// exists scope
if !(l == -1 && r == -1) {
scopes := securitySource[l+1 : r]
var s []string
for _, scope := range strings.Split(scopes, ",") {
s = append(s, strings.TrimSpace(scope))
for _, securityOption := range strings.Split(securitySource, "||") {
securityOption = strings.TrimSpace(securityOption)
l := strings.Index(securityOption, "[")
r := strings.Index(securityOption, "]")
if !(l == -1 && r == -1) {
scopes := securityOption[l+1 : r]
var s []string
for _, scope := range strings.Split(scopes, ",") {
s = append(s, strings.TrimSpace(scope))
}
securityKey := securityOption[0:l]
securityMap[securityKey] = append(securityMap[securityKey], s...)

} else {
securityKey := strings.TrimSpace(securityOption)
securityMap[securityKey] = []string{}
}
securityKey := securitySource[0:l]
securityMap := map[string][]string{}
securityMap[securityKey] = append(securityMap[securityKey], s...)
operation.Security = append(operation.Security, securityMap)
} else {
securityKey := strings.TrimSpace(securitySource)
securityMap := map[string][]string{}
securityMap[securityKey] = []string{}
operation.Security = append(operation.Security, securityMap)
}

operation.Security = append(operation.Security, securityMap)
return nil
}

Expand Down
33 changes: 33 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,39 @@ func TestParseSecurityComment(t *testing.T) {
})
}

func TestParseSecurityCommentSimple(t *testing.T) {
t.Parallel()

comment := `@Security ApiKeyAuth`
operation := NewOperation(nil)

err := operation.ParseComment(comment, nil)
assert.NoError(t, err)

assert.Equal(t, operation.Security, []map[string][]string{
{
"ApiKeyAuth": {},
},
})
}

func TestParseSecurityCommentOr(t *testing.T) {
t.Parallel()

comment := `@Security OAuth2Implicit[read, write] || Firebase[]`
operation := NewOperation(nil)

err := operation.ParseComment(comment, nil)
assert.NoError(t, err)

assert.Equal(t, operation.Security, []map[string][]string{
{
"OAuth2Implicit": {"read", "write"},
"Firebase": {""},
},
})
}

func TestParseMultiDescription(t *testing.T) {
t.Parallel()

Expand Down