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

[security] feature add or-option #1151

Merged
merged 4 commits into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 18 additions & 10 deletions operation.go
Expand Up @@ -629,30 +629,38 @@ func (operation *Operation) ParseRouterComment(commentLine string) error {
return nil
}

type SecurityMap map[string][]string
Copy link
Contributor

Choose a reason for hiding this comment

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

Useless definition. Please remove.


// ParseSecurityComment parses comment for given `security` comment string.
func (operation *Operation) ParseSecurityComment(commentLine string) error {
var securityMap SecurityMap = SecurityMap{}
securitySource := commentLine[strings.Index(commentLine, "@Security")+1:]
l := strings.Index(securitySource, "[")
r := strings.Index(securitySource, "]")
for _, secOption := range strings.Split(securitySource, "||") {
secOption = strings.TrimSpace(secOption)
operation.ProcessSecurityOption(secOption, securityMap)
}
operation.Security = append(operation.Security, securityMap)
return nil
}

func (operation *Operation) ProcessSecurityOption(securityOption string, securityMap SecurityMap) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need additional export. Even make this a private function even move the code inside the for loop.

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

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

return nil
}

// findTypeDef attempts to find the *ast.TypeSpec for a specific type given the
Expand Down
17 changes: 17 additions & 0 deletions operation_test.go
Expand Up @@ -2036,6 +2036,23 @@ func TestParseSecurityComment(t *testing.T) {
})
}

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