Skip to content

Commit

Permalink
fix issue 1414 (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdghchj committed Dec 12, 2022
1 parent 7867c24 commit 9a4fa5d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
23 changes: 16 additions & 7 deletions parser.go
Expand Up @@ -631,7 +631,7 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur

var search []string

attribute := strings.ToLower(strings.Split(lines[*index], " ")[0])
attribute := strings.ToLower(FieldsByAnySpace(lines[*index], 2)[0])
switch attribute {
case secBasicAttr:
return spec.BasicAuth(), nil
Expand All @@ -652,14 +652,23 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur
extensions, description := make(map[string]interface{}), ""

for ; *index < len(lines); *index++ {
v := lines[*index]
v := strings.TrimSpace(lines[*index])
if len(v) == 0 {
continue
}

fields := FieldsByAnySpace(v, 2)
securityAttr := strings.ToLower(fields[0])
var value string
if len(fields) > 1 {
value = fields[1]
}

securityAttr := strings.ToLower(strings.Split(v, " ")[0])
for _, findterm := range search {
if securityAttr == findterm {
attrMap[securityAttr] = strings.TrimSpace(v[len(securityAttr):])
attrMap[securityAttr] = value

continue
break
}
}

Expand All @@ -674,12 +683,12 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur

if strings.HasPrefix(securityAttr, "@x-") {
// Add the custom attribute without the @
extensions[securityAttr[1:]] = strings.TrimSpace(v[len(securityAttr):])
extensions[securityAttr[1:]] = value
}

// Not mandatory field
if securityAttr == descriptionAttr {
description = strings.TrimSpace(v[len(securityAttr):])
description = value
}

// next securityDefinitions
Expand Down
14 changes: 7 additions & 7 deletions parser_test.go
Expand Up @@ -3732,10 +3732,10 @@ func TestTryAddDescription(t *testing.T) {
{
name: "added description",
lines: []string{
"@securitydefinitions.apikey test",
"@in header",
"@name x-api-key",
"@description some description",
"\t@securitydefinitions.apikey test",
"\t@in header",
"\t@name x-api-key",
"\t@description some description",
},
want: &spec.SecurityScheme{
SecuritySchemeProps: spec.SecuritySchemeProps{
Expand All @@ -3749,9 +3749,9 @@ func TestTryAddDescription(t *testing.T) {
{
name: "no description",
lines: []string{
"@securitydefinitions.oauth2.application swagger",
"@tokenurl https://example.com/oauth/token",
"@not-description some description",
" @securitydefinitions.oauth2.application swagger",
" @tokenurl https://example.com/oauth/token",
" @not-description some description",
},
want: &spec.SecurityScheme{
SecuritySchemeProps: spec.SecuritySchemeProps{
Expand Down

0 comments on commit 9a4fa5d

Please sign in to comment.