Skip to content

Commit

Permalink
fix #1742 (#1744)
Browse files Browse the repository at this point in the history
* fix #1742

Signed-off-by: sdghchj <[email protected]>
  • Loading branch information
sdghchj committed Jan 22, 2024
1 parent d4218f2 commit ae7e404
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 63 deletions.
16 changes: 8 additions & 8 deletions example/celler/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,35 +997,35 @@ 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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}

Expand Down
19 changes: 9 additions & 10 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
66 changes: 33 additions & 33 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -228,17 +228,17 @@ 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": {
"type": "oauth2",
"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"
},
Expand All @@ -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"
}
}
},
Expand Down Expand Up @@ -310,35 +310,35 @@ 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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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"
}
}
},
Expand Down Expand Up @@ -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"
}
}
}`
Expand Down Expand Up @@ -1336,35 +1336,35 @@ 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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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"
}
}
}
Expand Down Expand Up @@ -1792,35 +1792,35 @@ 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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/global_security/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down
16 changes: 8 additions & 8 deletions testdata/simple/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -781,35 +781,35 @@
"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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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": {
"type": "oauth2",
"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"
}
}
}
Expand Down

0 comments on commit ae7e404

Please sign in to comment.