Skip to content

Commit

Permalink
feat: [security] feature add or-option (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehp authored Mar 8, 2022
1 parent 334111b commit 6686f54
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
38 changes: 20 additions & 18 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,27 +649,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 @@ -2081,6 +2081,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

0 comments on commit 6686f54

Please sign in to comment.