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 #1742 #1744

Merged
merged 3 commits into from
Jan 22, 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
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 @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
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