Skip to content

Implemented optional bearerformat #1986

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

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
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
27 changes: 17 additions & 10 deletions parserv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
descriptionAttr = "@description"
tokenURL = "@tokenurl"
authorizationURL = "@authorizationurl"
bearerFormat = "@bearerformat"
)

var search []string
Expand All @@ -351,20 +352,16 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
search = []string{authorizationURL, in}
case secAccessCodeAttr:
search = []string{tokenURL, authorizationURL, in}
case secBearerAuthAttr:
scheme := spec.SecurityScheme{
Type: "http",
Scheme: "bearer",
BearerFormat: "JWT",
}
return "bearerauth", &scheme, nil
}

// For the first line we get the attributes in the context parameter, so we skip to the next one
*index++

attrMap, scopes := make(map[string]string), make(map[string]string)
extensions, description := make(map[string]interface{}), ""
attrMap := make(map[string]string)
scopes := make(map[string]string)
optMap := make(map[string]string)
extensions := make(map[string]interface{})
description := ""

for ; *index < len(lines); *index++ {
v := strings.TrimSpace(lines[*index])
Expand All @@ -379,6 +376,8 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
value = fields[1]
}

optMap[securityAttr] = value

for _, findTerm := range search {
if securityAttr == findTerm {
attrMap[securityAttr] = value
Expand Down Expand Up @@ -467,6 +466,14 @@ func parseSecAttributesV3(context string, lines []string, index *int) (string, *
scheme.Flows.Spec.AuthorizationCode = spec.NewOAuthFlow()
scheme.Flows.Spec.AuthorizationCode.Spec.AuthorizationURL = attrMap[authorizationURL]
scheme.Flows.Spec.AuthorizationCode.Spec.TokenURL = attrMap[tokenURL]

case secBearerAuthAttr:
scheme.Type = "http"
scheme.Scheme = "bearer"
scheme.BearerFormat = optMap[bearerFormat]
if len(scheme.BearerFormat) == 0 {
scheme.BearerFormat = "JWT"
}
}

scheme.Description = description
Expand Down Expand Up @@ -727,7 +734,7 @@ func (p *Parser) ParseDefinitionV3(typeSpecDef *TypeSpecDef) (*SchemaV3, error)

if len(typeSpecDef.Enums) > 0 {
var varNames []string
var enumComments = make(map[string]string)
enumComments := make(map[string]string)
for _, value := range typeSpecDef.Enums {
definition.Spec.Enum = append(definition.Spec.Enum, value.Value)
varNames = append(varNames, value.key)
Expand Down
23 changes: 13 additions & 10 deletions parserv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestParserParseGeneralApiInfoV3(t *testing.T) {
assert.Equal(t, "OpenAPI", p.openAPI.ExternalDocs.Spec.Description)
assert.Equal(t, "https://swagger.io/resources/open-api", p.openAPI.ExternalDocs.Spec.URL)

assert.Equal(t, 7, len(p.openAPI.Components.Spec.SecuritySchemes))
assert.Equal(t, 8, len(p.openAPI.Components.Spec.SecuritySchemes))

security := p.openAPI.Components.Spec.SecuritySchemes
assert.Equal(t, "basic", security["basic"].Spec.Spec.Scheme)
Expand Down Expand Up @@ -166,9 +166,13 @@ func TestParserParseGeneralApiInfoV3(t *testing.T) {
assert.Equal(t, "header", security["OAuth2AccessCode"].Spec.Spec.In)
assert.Equal(t, "https://example.com/oauth/token", security["OAuth2AccessCode"].Spec.Spec.Flows.Spec.AuthorizationCode.Spec.TokenURL)

assert.Equal(t, "bearer", security["bearerauth"].Spec.Spec.Scheme)
assert.Equal(t, "http", security["bearerauth"].Spec.Spec.Type)
assert.Equal(t, "JWT", security["bearerauth"].Spec.Spec.BearerFormat)
assert.Equal(t, "bearer", security["BearerAuthDefault"].Spec.Spec.Scheme)
assert.Equal(t, "http", security["BearerAuthDefault"].Spec.Spec.Type)
assert.Equal(t, "JWT", security["BearerAuthDefault"].Spec.Spec.BearerFormat)

assert.Equal(t, "bearer", security["BearerAuthFormat"].Spec.Spec.Scheme)
assert.Equal(t, "http", security["BearerAuthFormat"].Spec.Spec.Type)
assert.Equal(t, "Bearer <token>", security["BearerAuthFormat"].Spec.Spec.BearerFormat)
}

func TestParser_ParseGeneralApiInfoExtensionsV3(t *testing.T) {
Expand Down Expand Up @@ -280,14 +284,16 @@ func TestParserParseGeneralAPITagDocsV3(t *testing.T) {
parser := New(GenerateOpenAPI3Doc(true))
assert.Error(t, parser.parseGeneralAPIInfoV3([]string{
"@tag.name Test",
"@tag.docs.description Best example documentation"}))
"@tag.docs.description Best example documentation",
}))

parser = New(GenerateOpenAPI3Doc(true))
err := parser.parseGeneralAPIInfoV3([]string{
"@tag.name test",
"@tag.description A test Tag",
"@tag.docs.url https://example.com",
"@tag.docs.description Best example documentation"})
"@tag.docs.description Best example documentation",
})
assert.NoError(t, err)

assert.Equal(t, "test", parser.openAPI.Tags[0].Spec.Name)
Expand Down Expand Up @@ -349,7 +355,6 @@ func TestParsePet(t *testing.T) {
assert.Equal(t, 8, len(petSchema.Properties))
assert.Equal(t, typeInteger, petSchema.Properties["id"].Spec.Type)
assert.Equal(t, typeString, petSchema.Properties["name"].Spec.Type)

}

func TestParseSimpleApiV3(t *testing.T) {
Expand All @@ -375,7 +380,7 @@ func TestParseSimpleApiV3(t *testing.T) {
path = paths["/FormData"].Spec.Spec.Post.Spec
assert.NotNil(t, path)
assert.NotNil(t, path.RequestBody)
//TODO add asserts
// TODO add asserts

t.Run("Test parse struct oneOf", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -466,7 +471,6 @@ func TestParseSimpleApiV3(t *testing.T) {
{Ref: &spec.Ref{Ref: "#/components/schemas/web.Cat"}},
{Ref: &spec.Ref{Ref: "#/components/schemas/web.Dog"}},
}, rootSchema.OneOf)

})
}

Expand Down Expand Up @@ -494,7 +498,6 @@ func TestParserParseServers(t *testing.T) {

assert.Equal(t, "https://petstore.com/v3", servers[1].Spec.URL)
assert.Equal(t, "Production Petstore server.", servers[1].Spec.Description)

}

func TestParseTypeAlias(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion testdata/v3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ package main
// @in header
// @name name

// @securitydefinitions.bearerauth BearerAuth
// @securitydefinitions.bearerauth BearerAuthDefault

// @securitydefinitions.bearerauth BearerAuthFormat
// @bearerformat Bearer <token>

// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api
Expand Down