From 0c855ffc2ade241868540c96541d9a7d5e435e99 Mon Sep 17 00:00:00 2001 From: PIL Date: Mon, 28 Feb 2022 16:20:36 +0100 Subject: [PATCH 1/8] [security] feature add or-option --- operation.go | 28 ++++++++++++++++++---------- operation_test.go | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/operation.go b/operation.go index 16d748891..c1e2031a5 100644 --- a/operation.go +++ b/operation.go @@ -629,30 +629,38 @@ func (operation *Operation) ParseRouterComment(commentLine string) error { return nil } +type SecurityMap map[string][]string + // 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) { // 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 diff --git a/operation_test.go b/operation_test.go index 8eb89b5f0..eacf63ea8 100644 --- a/operation_test.go +++ b/operation_test.go @@ -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() From 0dd3c30a8c2cf7995c37e481e2a268d69b89c446 Mon Sep 17 00:00:00 2001 From: PIL Date: Fri, 4 Mar 2022 16:19:01 +0100 Subject: [PATCH 2/8] run fmt --- operation_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operation_test.go b/operation_test.go index eacf63ea8..6068f3d3e 100644 --- a/operation_test.go +++ b/operation_test.go @@ -2048,7 +2048,7 @@ func TestParseSecurityCommentOr(t *testing.T) { assert.Equal(t, operation.Security, []map[string][]string{ { "OAuth2Implicit": {"read", "write"}, - "Firebase": {""}, + "Firebase": {""}, }, }) } From 4b990b50cfa35544b6b78f95090f1cf7568407d1 Mon Sep 17 00:00:00 2001 From: PIL Date: Mon, 7 Mar 2022 09:19:15 +0100 Subject: [PATCH 3/8] [pr changes] redesign code --- operation.go | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/operation.go b/operation.go index c1e2031a5..948d58bbd 100644 --- a/operation.go +++ b/operation.go @@ -629,38 +629,31 @@ func (operation *Operation) ParseRouterComment(commentLine string) error { return nil } -type SecurityMap map[string][]string - // ParseSecurityComment parses comment for given `security` comment string. func (operation *Operation) ParseSecurityComment(commentLine string) error { - var securityMap SecurityMap = SecurityMap{} + var securityMap map[string][]string = map[string][]string{} securitySource := commentLine[strings.Index(commentLine, "@Security")+1:] - for _, secOption := range strings.Split(securitySource, "||") { - secOption = strings.TrimSpace(secOption) - operation.ProcessSecurityOption(secOption, securityMap) - } - operation.Security = append(operation.Security, securityMap) - return nil -} + 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...) -func (operation *Operation) ProcessSecurityOption(securityOption string, securityMap SecurityMap) { - // exists scope - 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)) + } else { + securityKey := strings.TrimSpace(securityOption) + securityMap := map[string][]string{} + securityMap[securityKey] = []string{} } - securityKey := securityOption[0:l] - securityMap[securityKey] = append(securityMap[securityKey], s...) - - } else { - 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 From 750be1e8f6bac2d48b0bec4eef518ba7822e7702 Mon Sep 17 00:00:00 2001 From: PIL Date: Tue, 8 Mar 2022 10:52:45 +0100 Subject: [PATCH 4/8] [securityOr] fix simpleTest --- operation.go | 5 +++-- operation_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/operation.go b/operation.go index 948d58bbd..ad3cb6a12 100644 --- a/operation.go +++ b/operation.go @@ -631,7 +631,9 @@ 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 map[string][]string = map[string][]string{} + + var securityMap = make(map[string][]string) securitySource := commentLine[strings.Index(commentLine, "@Security")+1:] for _, securityOption := range strings.Split(securitySource, "||") { securityOption = strings.TrimSpace(securityOption) @@ -648,7 +650,6 @@ func (operation *Operation) ParseSecurityComment(commentLine string) error { } else { securityKey := strings.TrimSpace(securityOption) - securityMap := map[string][]string{} securityMap[securityKey] = []string{} } } diff --git a/operation_test.go b/operation_test.go index 6068f3d3e..45598e7db 100644 --- a/operation_test.go +++ b/operation_test.go @@ -2036,6 +2036,22 @@ 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() From 0ca3a3b481a1c6179fd2a3c95bc73304f4cbc7e2 Mon Sep 17 00:00:00 2001 From: PIL Date: Tue, 8 Mar 2022 15:33:10 +0100 Subject: [PATCH 5/8] [security or] describe or option --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 06724e379..bd3e98c4f 100644 --- a/README.md +++ b/README.md @@ -813,6 +813,14 @@ Make it AND condition // @Security OAuth2Application[write, admin] ``` +Make it OR condition + +```go +// @Security ApiKeyAuth || firebase +// @Security OAuth2Application[write, admin] || APIKeyAuth +``` + + ### Add a description for enum items ```go From 19987e8a24b57d0f164eb82ee7731fd3cbf08ab5 Mon Sep 17 00:00:00 2001 From: PIL Date: Thu, 10 Mar 2022 13:53:51 +0100 Subject: [PATCH 6/8] [security or] fix add or to generalTest --- testdata/simple/api/api.go | 1 + testdata/simple/expected.json | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/testdata/simple/api/api.go b/testdata/simple/api/api.go index e766c35f9..34e843713 100644 --- a/testdata/simple/api/api.go +++ b/testdata/simple/api/api.go @@ -41,6 +41,7 @@ func GetStringByInt(w http.ResponseWriter, r *http.Request) { // @Security OAuth2Implicit[read, admin] // @Security OAuth2AccessCode[read] // @Security OAuth2Password[admin] +// @Security OAuth2Implicit[read, write] || Firebase // @Router /testapi/get-struct-array-by-string/{some_id} [get] func GetStructArrayByString(w http.ResponseWriter, r *http.Request) { //write your code diff --git a/testdata/simple/expected.json b/testdata/simple/expected.json index dbe9b6b62..159eee394 100644 --- a/testdata/simple/expected.json +++ b/testdata/simple/expected.json @@ -299,6 +299,13 @@ "OAuth2Password": [ "admin" ] + }, + { + "Firebase": [], + "OAuth2Implicit": [ + "read", + "write" + ] } ], "description": "get struct array by ID", From e163c2d14305b622d2281db580ef88905b8f214d Mon Sep 17 00:00:00 2001 From: PIL Date: Thu, 10 Mar 2022 13:55:03 +0100 Subject: [PATCH 7/8] [security or] remove dev-comment --- operation.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/operation.go b/operation.go index ad3cb6a12..cbb71d30f 100644 --- a/operation.go +++ b/operation.go @@ -631,8 +631,6 @@ 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:] for _, securityOption := range strings.Split(securitySource, "||") { From 626958c2b6d98f536038b6bd99ab589579bcd8c9 Mon Sep 17 00:00:00 2001 From: PIL Date: Thu, 10 Mar 2022 14:11:21 +0100 Subject: [PATCH 8/8] Revert "[security or] remove dev-comment" This reverts commit e163c2d14305b622d2281db580ef88905b8f214d. --- operation.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/operation.go b/operation.go index cbb71d30f..ad3cb6a12 100644 --- a/operation.go +++ b/operation.go @@ -631,6 +631,8 @@ 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:] for _, securityOption := range strings.Split(securitySource, "||") {