diff --git a/parser.go b/parser.go index d1a2bba4b..e92a30168 100644 --- a/parser.go +++ b/parser.go @@ -387,15 +387,28 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error { return err } securityMap[value] = securitySchemeOAuth2AccessToken(attrMap["@authorizationurl"], attrMap["@tokenurl"], scopes, extensions) - case "@x-tokenname": - // ignore this - break case "@query.collection.format": parser.collectionFormatInQuery = value default: prefixExtension := "@x-" if len(attribute) > 5 { // Prefix extension + 1 char + 1 space + 1 char if attribute[:len(prefixExtension)] == prefixExtension { + + extExistsInSecurityDef := false + // for each security definition + for _, v := range securityMap { + // check if extension exists + _, extExistsInSecurityDef = v.VendorExtensible.Extensions.GetString(attribute[1:]) + // if it exists in at least one, then we stop iterating + if extExistsInSecurityDef { + break + } + } + // if it is present on security def, don't add it again + if extExistsInSecurityDef { + break + } + var valueJSON interface{} split := strings.SplitAfter(commentLine, attribute+" ") if len(split) < 2 { @@ -462,8 +475,9 @@ func extractSecurityAttribute(context string, search []string, lines []string) ( } scopes[scopScheme] = v[len(securityAttr):] } - if securityAttr == "@x-tokenname" { - extensions["x-tokenName"] = strings.TrimSpace(v[len(securityAttr):]) + if strings.HasPrefix(securityAttr, "@x-") { + // Add the custom attribute without the @ + extensions[securityAttr[1:]] = strings.TrimSpace(v[len(securityAttr):]) } // next securityDefinitions if strings.Index(securityAttr, "@securitydefinitions.") == 0 { diff --git a/parser_test.go b/parser_test.go index c541cd2e2..44ddbbe15 100644 --- a/parser_test.go +++ b/parser_test.go @@ -90,7 +90,7 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) { "scopes": { "admin": " Grants read and write access to administrative information" }, - "x-tokenName": "id_token" + "x-tokenname": "id_token" }, "OAuth2Application": { "type": "oauth2", @@ -108,7 +108,8 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) { "scopes": { "admin": " Grants read and write access to administrative information", "write": " Grants write access" - } + }, + "x-google-audiences": "some_audience.google.com" }, "OAuth2Password": { "type": "oauth2", diff --git a/testdata/main.go b/testdata/main.go index 9ee7c9339..2cadfe4fb 100644 --- a/testdata/main.go +++ b/testdata/main.go @@ -31,6 +31,7 @@ package main // @authorizationurl https://example.com/oauth/authorize // @scope.write Grants write access // @scope.admin Grants read and write access to administrative information +// @x-google-audiences some_audience.google.com // @securitydefinitions.oauth2.password OAuth2Password // @tokenUrl https://example.com/oauth/token