From 67f89d11fb8cc71edf6fda3292a8a4ae370105bb Mon Sep 17 00:00:00 2001 From: sdghchj Date: Wed, 17 Jan 2024 11:26:22 +0800 Subject: [PATCH 1/3] fix #1742 Signed-off-by: sdghchj --- parser.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/parser.go b/parser.go index a97da1d59..7393242f5 100644 --- a/parser.go +++ b/parser.go @@ -735,6 +735,7 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur attrMap, scopes := make(map[string]string), make(map[string]string) extensions, description := make(map[string]interface{}), "" +loopline: for ; *index < len(lines); *index++ { v := strings.TrimSpace(lines[*index]) if len(v) == 0 { @@ -751,23 +752,21 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur for _, findterm := range search { if securityAttr == findterm { attrMap[securityAttr] = value - - break + continue loopline } } - isExists, err := isExistsScope(securityAttr) - if err != nil { + if isExists, err := isExistsScope(securityAttr); err != nil { return nil, err - } - - if isExists { - scopes[securityAttr[len(scopeAttrPrefix):]] = v[len(securityAttr):] + } else if isExists { + scopes[securityAttr[len(scopeAttrPrefix):]] = value + continue } if strings.HasPrefix(securityAttr, "@x-") { // Add the custom attribute without the @ extensions[securityAttr[1:]] = value + continue } // Not mandatory field @@ -914,14 +913,14 @@ func getMarkdownForTag(tagName string, dirPath string) ([]byte, error) { func isExistsScope(scope string) (bool, error) { s := strings.Fields(scope) for _, v := range s { - if strings.Contains(v, scopeAttrPrefix) { + if strings.HasPrefix(v, scopeAttrPrefix) { if strings.Contains(v, ",") { return false, fmt.Errorf("@scope can't use comma(,) get=" + v) } } } - return strings.Contains(scope, scopeAttrPrefix), nil + return strings.HasPrefix(scope, scopeAttrPrefix), nil } func getTagsFromComment(comment string) (tags []string) { From d6f1f9020069bdb45b2ae4b2150a3308aba9b036 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Wed, 17 Jan 2024 11:36:57 +0800 Subject: [PATCH 2/3] fix tests Signed-off-by: sdghchj --- example/celler/docs/docs.go | 16 +++---- packages.go | 4 +- parser_test.go | 58 +++++++++++++------------- testdata/global_security/expected.json | 4 +- testdata/simple/expected.json | 14 +++---- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/example/celler/docs/docs.go b/example/celler/docs/docs.go index 31cc65906..e5c794d07 100644 --- a/example/celler/docs/docs.go +++ b/example/celler/docs/docs.go @@ -997,7 +997,7 @@ const docTemplate = `{ "authorizationUrl": "https://example.com/oauth/authorize", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information" + "admin": "Grants read and write access to administrative information" } }, "OAuth2Application": { @@ -1005,8 +1005,8 @@ const docTemplate = `{ "flow": "application", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Implicit": { @@ -1014,8 +1014,8 @@ const docTemplate = `{ "flow": "implicit", "authorizationUrl": "https://example.com/oauth/authorize", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Password": { @@ -1023,9 +1023,9 @@ const docTemplate = `{ "flow": "password", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "read": " Grants read access", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "read": "Grants read access", + "write": "Grants write access" } } } diff --git a/packages.go b/packages.go index 35d9a782a..69a1b0529 100644 --- a/packages.go +++ b/packages.go @@ -354,7 +354,7 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe } //delete it from parsed schemas, and will parse it again - if _, ok := parsedSchemas[typeDef]; ok { + if _, ok = parsedSchemas[typeDef]; ok { delete(parsedSchemas, typeDef) } @@ -363,7 +363,7 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe } name := constVar.Name.Name - if _, ok := constVar.Value.(ast.Expr); ok { + if _, ok = constVar.Value.(ast.Expr); ok { continue } diff --git a/parser_test.go b/parser_test.go index d818f705c..d4d061d14 100644 --- a/parser_test.go +++ b/parser_test.go @@ -219,7 +219,7 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) { "authorizationUrl": "https://example.com/oauth/authorize", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information" + "admin": "Grants read and write access to administrative information" }, "x-tokenname": "id_token" }, @@ -228,8 +228,8 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) { "flow": "application", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Implicit": { @@ -237,8 +237,8 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) { "flow": "implicit", "authorizationUrl": "https://example.com/oauth/authorize", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" }, "x-google-audiences": "some_audience.google.com" }, @@ -247,9 +247,9 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) { "flow": "password", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", + "admin": "Grants read and write access to administrative information", "read": " Grants read access", - "write": " Grants write access" + "write": "Grants write access" } } }, @@ -310,7 +310,7 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) { "authorizationUrl": "https://example.com/oauth/authorize", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information" + "admin": "Grants read and write access to administrative information" } }, "OAuth2Application": { @@ -318,8 +318,8 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) { "flow": "application", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Implicit": { @@ -327,8 +327,8 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) { "flow": "implicit", "authorizationUrl": "https://example.com/oauth/authorize", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Password": { @@ -336,9 +336,9 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) { "flow": "password", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", + "admin": "Grants read and write access to administrative information", "read": " Grants read access", - "write": " Grants write access" + "write": "Grants write access" } } }, @@ -635,7 +635,7 @@ func TestParser_ParseGeneralAPISecurity(t *testing.T) { "authorizationUrl": "https://example.com/oauth/authorize", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " foo" + "admin": "foo" } } }` @@ -1336,7 +1336,7 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) { "authorizationUrl": "https://example.com/oauth/authorize", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information" + "admin": "Grants read and write access to administrative information" } }, "OAuth2Application": { @@ -1344,8 +1344,8 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) { "flow": "application", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Implicit": { @@ -1353,8 +1353,8 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) { "flow": "implicit", "authorizationUrl": "https://example.com/oauth/authorize", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Password": { @@ -1362,9 +1362,9 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) { "flow": "password", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", + "admin": "Grants read and write access to administrative information", "read": " Grants read access", - "write": " Grants write access" + "write": "Grants write access" } } } @@ -1792,7 +1792,7 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) { "authorizationUrl": "https://example.com/oauth/authorize", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information" + "admin": "Grants read and write access to administrative information" } }, "OAuth2Application": { @@ -1800,8 +1800,8 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) { "flow": "application", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Implicit": { @@ -1809,8 +1809,8 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) { "flow": "implicit", "authorizationUrl": "https://example.com/oauth/authorize", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Password": { @@ -1818,9 +1818,9 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) { "flow": "password", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", + "admin": "Grants read and write access to administrative information", "read": " Grants read access", - "write": " Grants write access" + "write": "Grants write access" } } } diff --git a/testdata/global_security/expected.json b/testdata/global_security/expected.json index 7505df733..c79eb5a99 100644 --- a/testdata/global_security/expected.json +++ b/testdata/global_security/expected.json @@ -91,8 +91,8 @@ "flow": "application", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } } }, diff --git a/testdata/simple/expected.json b/testdata/simple/expected.json index 24ce9f696..66646c996 100644 --- a/testdata/simple/expected.json +++ b/testdata/simple/expected.json @@ -781,7 +781,7 @@ "authorizationUrl": "https://example.com/oauth/authorize", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information" + "admin": "Grants read and write access to administrative information" } }, "OAuth2Application": { @@ -789,8 +789,8 @@ "flow": "application", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Implicit": { @@ -798,8 +798,8 @@ "flow": "implicit", "authorizationUrl": "https://example.com/oauth/authorize", "scopes": { - "admin": " Grants read and write access to administrative information", - "write": " Grants write access" + "admin": "Grants read and write access to administrative information", + "write": "Grants write access" } }, "OAuth2Password": { @@ -807,9 +807,9 @@ "flow": "password", "tokenUrl": "https://example.com/oauth/token", "scopes": { - "admin": " Grants read and write access to administrative information", + "admin": "Grants read and write access to administrative information", "read": " Grants read access", - "write": " Grants write access" + "write": "Grants write access" } } } From efb30fe1032245f701a20b28328d10daeae24711 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Wed, 17 Jan 2024 11:39:22 +0800 Subject: [PATCH 3/3] fix tests Signed-off-by: sdghchj --- parser_test.go | 8 ++++---- testdata/simple/expected.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/parser_test.go b/parser_test.go index d4d061d14..770e64bc0 100644 --- a/parser_test.go +++ b/parser_test.go @@ -248,7 +248,7 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) { "tokenUrl": "https://example.com/oauth/token", "scopes": { "admin": "Grants read and write access to administrative information", - "read": " Grants read access", + "read": "Grants read access", "write": "Grants write access" } } @@ -337,7 +337,7 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) { "tokenUrl": "https://example.com/oauth/token", "scopes": { "admin": "Grants read and write access to administrative information", - "read": " Grants read access", + "read": "Grants read access", "write": "Grants write access" } } @@ -1363,7 +1363,7 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) { "tokenUrl": "https://example.com/oauth/token", "scopes": { "admin": "Grants read and write access to administrative information", - "read": " Grants read access", + "read": "Grants read access", "write": "Grants write access" } } @@ -1819,7 +1819,7 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) { "tokenUrl": "https://example.com/oauth/token", "scopes": { "admin": "Grants read and write access to administrative information", - "read": " Grants read access", + "read": "Grants read access", "write": "Grants write access" } } diff --git a/testdata/simple/expected.json b/testdata/simple/expected.json index 66646c996..b3f642629 100644 --- a/testdata/simple/expected.json +++ b/testdata/simple/expected.json @@ -808,7 +808,7 @@ "tokenUrl": "https://example.com/oauth/token", "scopes": { "admin": "Grants read and write access to administrative information", - "read": " Grants read access", + "read": "Grants read access", "write": "Grants write access" } }