Skip to content

Commit

Permalink
fix: use '&&' for security pair(AND)
Browse files Browse the repository at this point in the history
  • Loading branch information
KKKIIO authored Aug 26, 2023
1 parent 8ebf32f commit 30152c3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -874,18 +874,18 @@ Each API operation.
// @Security ApiKeyAuth
```
Make it AND condition
Make it OR condition
```go
// @Security ApiKeyAuth
// @Security OAuth2Application[write, admin]
```
Make it OR condition
Make it AND condition
```go
// @Security ApiKeyAuth || firebase
// @Security OAuth2Application[write, admin] || APIKeyAuth
// @Security ApiKeyAuth && firebase
// @Security OAuth2Application[write, admin] && APIKeyAuth
```
Expand Down
8 changes: 4 additions & 4 deletions README_pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -869,18 +869,18 @@ Cada operação API.
// @Security ApiKeyAuth
```
Faça-o AND condicione-o
Faça-o OR condicione-o
```go
// @Security ApiKeyAuth
// @Security OAuth2Application[write, admin]
```
Faça-o OR condição
Faça-o AND condição
```go
// @Security ApiKeyAuth || firebase
// @Security OAuth2Application[write, admin] || APIKeyAuth
// @Security ApiKeyAuth && firebase
// @Security OAuth2Application[write, admin] && APIKeyAuth
```
Expand Down
3 changes: 1 addition & 2 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,7 @@ type Resp struct {
使用AND条件。

```go
// @Security ApiKeyAuth
// @Security OAuth2Application[write, admin]
// @Security ApiKeyAuth && OAuth2Application[write, admin]
```

## 项目相关
Expand Down
3 changes: 2 additions & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var mimeTypeAliases = map[string]string{
}

var mimeTypePattern = regexp.MustCompile("^[^/]+/[^/]+$")
var securityPairSepPattern = regexp.MustCompile(`\|\||&&`) // || for compatibility with old version, && for clarity

// NewOperation creates a new Operation with default properties.
// map[int]Response.
Expand Down Expand Up @@ -731,7 +732,7 @@ func (operation *Operation) ParseSecurityComment(commentLine string) error {
securitySource = commentLine[strings.Index(commentLine, "@Security")+1:]
)

for _, securityOption := range strings.Split(securitySource, "||") {
for _, securityOption := range securityPairSepPattern.Split(securitySource, -1) {
securityOption = strings.TrimSpace(securityOption)

left, right := strings.Index(securityOption, "["), strings.Index(securityOption, "]")
Expand Down
15 changes: 11 additions & 4 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2132,21 +2132,28 @@ func TestParseSecurityCommentSimple(t *testing.T) {
})
}

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

comment := `@Security OAuth2Implicit[read, write] || Firebase[]`
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{
expect := []map[string][]string{
{
"OAuth2Implicit": {"read", "write"},
"Firebase": {""},
},
})
}
assert.Equal(t, operation.Security, expect)

oldVersionComment := `@Security OAuth2Implicit[read, write] || Firebase[]`
operation = NewOperation(nil)
err = operation.ParseComment(oldVersionComment, nil)
assert.NoError(t, err)
assert.Equal(t, operation.Security, expect)
}

func TestParseMultiDescription(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur
func parseSecurity(commentLine string) map[string][]string {
securityMap := make(map[string][]string)

for _, securityOption := range strings.Split(commentLine, "||") {
for _, securityOption := range securityPairSepPattern.Split(commentLine, -1) {
securityOption = strings.TrimSpace(securityOption)

left, right := strings.Index(securityOption, "["), strings.Index(securityOption, "]")
Expand Down
2 changes: 1 addition & 1 deletion testdata/global_security/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ package global_security
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @security APIKeyAuth || OAuth2Application
// @security APIKeyAuth && OAuth2Application
func main() {}

0 comments on commit 30152c3

Please sign in to comment.