From 50f547b6facfe9625dbc05893a89c314a70af84a Mon Sep 17 00:00:00 2001 From: sdghchj Date: Mon, 12 Dec 2022 16:59:37 +0800 Subject: [PATCH 1/2] fix issue 1414 --- parser.go | 25 +++++++++++++++++-------- parser_test.go | 14 +++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/parser.go b/parser.go index ea77311d5..3b2511eb1 100644 --- a/parser.go +++ b/parser.go @@ -621,7 +621,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 @@ -642,14 +642,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 } } @@ -659,17 +668,17 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur } if isExists { - scopes[securityAttr[len(scopeAttrPrefix):]] = v[len(securityAttr):] + scopes[securityAttr[len(scopeAttrPrefix):]] = value } 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 diff --git a/parser_test.go b/parser_test.go index c7e5bacb1..ee1624433 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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{ @@ -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{ From ceb8bc9ec6db4582024f2ab75b94f1323f3d15f2 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Mon, 12 Dec 2022 18:04:40 +0800 Subject: [PATCH 2/2] fix test --- parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.go b/parser.go index 3b2511eb1..80f9e0397 100644 --- a/parser.go +++ b/parser.go @@ -668,7 +668,7 @@ func parseSecAttributes(context string, lines []string, index *int) (*spec.Secur } if isExists { - scopes[securityAttr[len(scopeAttrPrefix):]] = value + scopes[securityAttr[len(scopeAttrPrefix):]] = v[len(securityAttr):] } if strings.HasPrefix(securityAttr, "@x-") {