Skip to content

Commit

Permalink
fix: use '&&' for security pair(AND) (#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
KKKIIO authored Oct 21, 2024
1 parent d649b90 commit 3975e5b
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
@@ -897,18 +897,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
```
8 changes: 4 additions & 4 deletions README_pt.md
Original file line number Diff line number Diff line change
@@ -870,18 +870,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
```
3 changes: 1 addition & 2 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
@@ -732,8 +732,7 @@ type Resp struct {
使用AND条件。

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

## 项目相关
3 changes: 2 additions & 1 deletion operation.go
Original file line number Diff line number Diff line change
@@ -50,6 +50,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.
@@ -743,7 +744,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, "]")
15 changes: 11 additions & 4 deletions operation_test.go
Original file line number Diff line number Diff line change
@@ -2314,21 +2314,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) {
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
@@ -842,7 +842,7 @@ loopline:
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, "]")
2 changes: 1 addition & 1 deletion testdata/global_security/main.go
Original file line number Diff line number Diff line change
@@ -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 3975e5b

Please sign in to comment.