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

fix: use '&&' for security pair(AND) #1659

Merged
merged 1 commit into from
Oct 21, 2024
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: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
8 changes: 4 additions & 4 deletions README_pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
3 changes: 1 addition & 2 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,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 @@ -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.
Expand Down Expand Up @@ -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, "]")
Expand Down
15 changes: 11 additions & 4 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "]")
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() {}
Loading