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.go b/parser.go index 6832d5886..e52e9b07d 100644 --- a/parser.go +++ b/parser.go @@ -737,6 +737,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 { @@ -753,23 +754,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 @@ -916,14 +915,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) { diff --git a/parser_test.go b/parser_test.go index d818f705c..770e64bc0 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", - "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" } } }, @@ -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", - "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" } } }, @@ -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", - "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" } } } @@ -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", - "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/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..b3f642629 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", - "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" } } }