Skip to content

Commit ae7e404

Browse files
authoredJan 22, 2024
fix #1742 (#1744)
* fix #1742 Signed-off-by: sdghchj <sdghchj@qq.com>
1 parent d4218f2 commit ae7e404

File tree

6 files changed

+62
-63
lines changed

6 files changed

+62
-63
lines changed
 

‎example/celler/docs/docs.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -997,35 +997,35 @@ const docTemplate = `{
997997
"authorizationUrl": "https://example.com/oauth/authorize",
998998
"tokenUrl": "https://example.com/oauth/token",
999999
"scopes": {
1000-
"admin": " Grants read and write access to administrative information"
1000+
"admin": "Grants read and write access to administrative information"
10011001
}
10021002
},
10031003
"OAuth2Application": {
10041004
"type": "oauth2",
10051005
"flow": "application",
10061006
"tokenUrl": "https://example.com/oauth/token",
10071007
"scopes": {
1008-
"admin": " Grants read and write access to administrative information",
1009-
"write": " Grants write access"
1008+
"admin": "Grants read and write access to administrative information",
1009+
"write": "Grants write access"
10101010
}
10111011
},
10121012
"OAuth2Implicit": {
10131013
"type": "oauth2",
10141014
"flow": "implicit",
10151015
"authorizationUrl": "https://example.com/oauth/authorize",
10161016
"scopes": {
1017-
"admin": " Grants read and write access to administrative information",
1018-
"write": " Grants write access"
1017+
"admin": "Grants read and write access to administrative information",
1018+
"write": "Grants write access"
10191019
}
10201020
},
10211021
"OAuth2Password": {
10221022
"type": "oauth2",
10231023
"flow": "password",
10241024
"tokenUrl": "https://example.com/oauth/token",
10251025
"scopes": {
1026-
"admin": " Grants read and write access to administrative information",
1027-
"read": " Grants read access",
1028-
"write": " Grants write access"
1026+
"admin": "Grants read and write access to administrative information",
1027+
"read": "Grants read access",
1028+
"write": "Grants write access"
10291029
}
10301030
}
10311031
}

‎packages.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe
354354
}
355355

356356
//delete it from parsed schemas, and will parse it again
357-
if _, ok := parsedSchemas[typeDef]; ok {
357+
if _, ok = parsedSchemas[typeDef]; ok {
358358
delete(parsedSchemas, typeDef)
359359
}
360360

@@ -363,7 +363,7 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe
363363
}
364364

365365
name := constVar.Name.Name
366-
if _, ok := constVar.Value.(ast.Expr); ok {
366+
if _, ok = constVar.Value.(ast.Expr); ok {
367367
continue
368368
}
369369

‎parser.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur
737737
attrMap, scopes := make(map[string]string), make(map[string]string)
738738
extensions, description := make(map[string]interface{}), ""
739739

740+
loopline:
740741
for ; *index < len(lines); *index++ {
741742
v := strings.TrimSpace(lines[*index])
742743
if len(v) == 0 {
@@ -753,23 +754,21 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur
753754
for _, findterm := range search {
754755
if securityAttr == findterm {
755756
attrMap[securityAttr] = value
756-
757-
break
757+
continue loopline
758758
}
759759
}
760760

761-
isExists, err := isExistsScope(securityAttr)
762-
if err != nil {
761+
if isExists, err := isExistsScope(securityAttr); err != nil {
763762
return nil, err
764-
}
765-
766-
if isExists {
767-
scopes[securityAttr[len(scopeAttrPrefix):]] = v[len(securityAttr):]
763+
} else if isExists {
764+
scopes[securityAttr[len(scopeAttrPrefix):]] = value
765+
continue
768766
}
769767

770768
if strings.HasPrefix(securityAttr, "@x-") {
771769
// Add the custom attribute without the @
772770
extensions[securityAttr[1:]] = value
771+
continue
773772
}
774773

775774
// Not mandatory field
@@ -916,14 +915,14 @@ func getMarkdownForTag(tagName string, dirPath string) ([]byte, error) {
916915
func isExistsScope(scope string) (bool, error) {
917916
s := strings.Fields(scope)
918917
for _, v := range s {
919-
if strings.Contains(v, scopeAttrPrefix) {
918+
if strings.HasPrefix(v, scopeAttrPrefix) {
920919
if strings.Contains(v, ",") {
921920
return false, fmt.Errorf("@scope can't use comma(,) get=" + v)
922921
}
923922
}
924923
}
925924

926-
return strings.Contains(scope, scopeAttrPrefix), nil
925+
return strings.HasPrefix(scope, scopeAttrPrefix), nil
927926
}
928927

929928
func getTagsFromComment(comment string) (tags []string) {

‎parser_test.go

+33-33
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) {
219219
"authorizationUrl": "https://example.com/oauth/authorize",
220220
"tokenUrl": "https://example.com/oauth/token",
221221
"scopes": {
222-
"admin": " Grants read and write access to administrative information"
222+
"admin": "Grants read and write access to administrative information"
223223
},
224224
"x-tokenname": "id_token"
225225
},
@@ -228,17 +228,17 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) {
228228
"flow": "application",
229229
"tokenUrl": "https://example.com/oauth/token",
230230
"scopes": {
231-
"admin": " Grants read and write access to administrative information",
232-
"write": " Grants write access"
231+
"admin": "Grants read and write access to administrative information",
232+
"write": "Grants write access"
233233
}
234234
},
235235
"OAuth2Implicit": {
236236
"type": "oauth2",
237237
"flow": "implicit",
238238
"authorizationUrl": "https://example.com/oauth/authorize",
239239
"scopes": {
240-
"admin": " Grants read and write access to administrative information",
241-
"write": " Grants write access"
240+
"admin": "Grants read and write access to administrative information",
241+
"write": "Grants write access"
242242
},
243243
"x-google-audiences": "some_audience.google.com"
244244
},
@@ -247,9 +247,9 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) {
247247
"flow": "password",
248248
"tokenUrl": "https://example.com/oauth/token",
249249
"scopes": {
250-
"admin": " Grants read and write access to administrative information",
251-
"read": " Grants read access",
252-
"write": " Grants write access"
250+
"admin": "Grants read and write access to administrative information",
251+
"read": "Grants read access",
252+
"write": "Grants write access"
253253
}
254254
}
255255
},
@@ -310,35 +310,35 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) {
310310
"authorizationUrl": "https://example.com/oauth/authorize",
311311
"tokenUrl": "https://example.com/oauth/token",
312312
"scopes": {
313-
"admin": " Grants read and write access to administrative information"
313+
"admin": "Grants read and write access to administrative information"
314314
}
315315
},
316316
"OAuth2Application": {
317317
"type": "oauth2",
318318
"flow": "application",
319319
"tokenUrl": "https://example.com/oauth/token",
320320
"scopes": {
321-
"admin": " Grants read and write access to administrative information",
322-
"write": " Grants write access"
321+
"admin": "Grants read and write access to administrative information",
322+
"write": "Grants write access"
323323
}
324324
},
325325
"OAuth2Implicit": {
326326
"type": "oauth2",
327327
"flow": "implicit",
328328
"authorizationUrl": "https://example.com/oauth/authorize",
329329
"scopes": {
330-
"admin": " Grants read and write access to administrative information",
331-
"write": " Grants write access"
330+
"admin": "Grants read and write access to administrative information",
331+
"write": "Grants write access"
332332
}
333333
},
334334
"OAuth2Password": {
335335
"type": "oauth2",
336336
"flow": "password",
337337
"tokenUrl": "https://example.com/oauth/token",
338338
"scopes": {
339-
"admin": " Grants read and write access to administrative information",
340-
"read": " Grants read access",
341-
"write": " Grants write access"
339+
"admin": "Grants read and write access to administrative information",
340+
"read": "Grants read access",
341+
"write": "Grants write access"
342342
}
343343
}
344344
},
@@ -635,7 +635,7 @@ func TestParser_ParseGeneralAPISecurity(t *testing.T) {
635635
"authorizationUrl": "https://example.com/oauth/authorize",
636636
"tokenUrl": "https://example.com/oauth/token",
637637
"scopes": {
638-
"admin": " foo"
638+
"admin": "foo"
639639
}
640640
}
641641
}`
@@ -1336,35 +1336,35 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
13361336
"authorizationUrl": "https://example.com/oauth/authorize",
13371337
"tokenUrl": "https://example.com/oauth/token",
13381338
"scopes": {
1339-
"admin": " Grants read and write access to administrative information"
1339+
"admin": "Grants read and write access to administrative information"
13401340
}
13411341
},
13421342
"OAuth2Application": {
13431343
"type": "oauth2",
13441344
"flow": "application",
13451345
"tokenUrl": "https://example.com/oauth/token",
13461346
"scopes": {
1347-
"admin": " Grants read and write access to administrative information",
1348-
"write": " Grants write access"
1347+
"admin": "Grants read and write access to administrative information",
1348+
"write": "Grants write access"
13491349
}
13501350
},
13511351
"OAuth2Implicit": {
13521352
"type": "oauth2",
13531353
"flow": "implicit",
13541354
"authorizationUrl": "https://example.com/oauth/authorize",
13551355
"scopes": {
1356-
"admin": " Grants read and write access to administrative information",
1357-
"write": " Grants write access"
1356+
"admin": "Grants read and write access to administrative information",
1357+
"write": "Grants write access"
13581358
}
13591359
},
13601360
"OAuth2Password": {
13611361
"type": "oauth2",
13621362
"flow": "password",
13631363
"tokenUrl": "https://example.com/oauth/token",
13641364
"scopes": {
1365-
"admin": " Grants read and write access to administrative information",
1366-
"read": " Grants read access",
1367-
"write": " Grants write access"
1365+
"admin": "Grants read and write access to administrative information",
1366+
"read": "Grants read access",
1367+
"write": "Grants write access"
13681368
}
13691369
}
13701370
}
@@ -1792,35 +1792,35 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) {
17921792
"authorizationUrl": "https://example.com/oauth/authorize",
17931793
"tokenUrl": "https://example.com/oauth/token",
17941794
"scopes": {
1795-
"admin": " Grants read and write access to administrative information"
1795+
"admin": "Grants read and write access to administrative information"
17961796
}
17971797
},
17981798
"OAuth2Application": {
17991799
"type": "oauth2",
18001800
"flow": "application",
18011801
"tokenUrl": "https://example.com/oauth/token",
18021802
"scopes": {
1803-
"admin": " Grants read and write access to administrative information",
1804-
"write": " Grants write access"
1803+
"admin": "Grants read and write access to administrative information",
1804+
"write": "Grants write access"
18051805
}
18061806
},
18071807
"OAuth2Implicit": {
18081808
"type": "oauth2",
18091809
"flow": "implicit",
18101810
"authorizationUrl": "https://example.com/oauth/authorize",
18111811
"scopes": {
1812-
"admin": " Grants read and write access to administrative information",
1813-
"write": " Grants write access"
1812+
"admin": "Grants read and write access to administrative information",
1813+
"write": "Grants write access"
18141814
}
18151815
},
18161816
"OAuth2Password": {
18171817
"type": "oauth2",
18181818
"flow": "password",
18191819
"tokenUrl": "https://example.com/oauth/token",
18201820
"scopes": {
1821-
"admin": " Grants read and write access to administrative information",
1822-
"read": " Grants read access",
1823-
"write": " Grants write access"
1821+
"admin": "Grants read and write access to administrative information",
1822+
"read": "Grants read access",
1823+
"write": "Grants write access"
18241824
}
18251825
}
18261826
}

‎testdata/global_security/expected.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
"flow": "application",
9292
"tokenUrl": "https://example.com/oauth/token",
9393
"scopes": {
94-
"admin": " Grants read and write access to administrative information",
95-
"write": " Grants write access"
94+
"admin": "Grants read and write access to administrative information",
95+
"write": "Grants write access"
9696
}
9797
}
9898
},

‎testdata/simple/expected.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -781,35 +781,35 @@
781781
"authorizationUrl": "https://example.com/oauth/authorize",
782782
"tokenUrl": "https://example.com/oauth/token",
783783
"scopes": {
784-
"admin": " Grants read and write access to administrative information"
784+
"admin": "Grants read and write access to administrative information"
785785
}
786786
},
787787
"OAuth2Application": {
788788
"type": "oauth2",
789789
"flow": "application",
790790
"tokenUrl": "https://example.com/oauth/token",
791791
"scopes": {
792-
"admin": " Grants read and write access to administrative information",
793-
"write": " Grants write access"
792+
"admin": "Grants read and write access to administrative information",
793+
"write": "Grants write access"
794794
}
795795
},
796796
"OAuth2Implicit": {
797797
"type": "oauth2",
798798
"flow": "implicit",
799799
"authorizationUrl": "https://example.com/oauth/authorize",
800800
"scopes": {
801-
"admin": " Grants read and write access to administrative information",
802-
"write": " Grants write access"
801+
"admin": "Grants read and write access to administrative information",
802+
"write": "Grants write access"
803803
}
804804
},
805805
"OAuth2Password": {
806806
"type": "oauth2",
807807
"flow": "password",
808808
"tokenUrl": "https://example.com/oauth/token",
809809
"scopes": {
810-
"admin": " Grants read and write access to administrative information",
811-
"read": " Grants read access",
812-
"write": " Grants write access"
810+
"admin": "Grants read and write access to administrative information",
811+
"read": "Grants read access",
812+
"write": "Grants write access"
813813
}
814814
}
815815
}

0 commit comments

Comments
 (0)
Please sign in to comment.