Skip to content

Commit

Permalink
fix: change security field type and fix tests (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritterhoff committed Mar 31, 2022
1 parent eb08933 commit 8ad9884
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
14 changes: 9 additions & 5 deletions fizz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ type testInputModel struct {
QueryParam string `query:"q"`
}

type testInputModel1 struct {
PathParam1 string `path:"a"`
}

type testInputModel2 struct {
C string `path:"c"`
Message string `json:"message" description:"A short message"`
Expand Down Expand Up @@ -305,7 +309,7 @@ func TestSpecHandler(t *testing.T) {
WithoutSecurity(),
XInternal(),
},
tonic.Handler(func(c *gin.Context) (*T, error) {
tonic.Handler(func(c *gin.Context, in *testInputModel1) (*T, error) {
return &T{}, nil
}, 200),
)
Expand Down Expand Up @@ -350,11 +354,11 @@ func TestSpecHandler(t *testing.T) {
}
fizz.Generator().SetServers(servers)

security := openapi.SecurityRequirement{
"api_key": []string{},
"oauth2": []string{"write:pets", "read:pets"},
security := []*openapi.SecurityRequirement{
{"api_key": []string{}},
{"oauth2": []string{"write:pets", "read:pets"}},
}
fizz.Generator().SetSecurityRequirement(&security)
fizz.Generator().SetSecurityRequirement(security)

fizz.Generator().API().Components.SecuritySchemes = map[string]*openapi.SecuritySchemeOrRef{
"api_key": {
Expand Down
2 changes: 1 addition & 1 deletion openapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (g *Generator) SetServers(servers []*Server) {

// SetSecurityRequirement sets the security options for the
// current specification.
func (g *Generator) SetSecurityRequirement(security *SecurityRequirement) {
func (g *Generator) SetSecurityRequirement(security []*SecurityRequirement) {
g.api.Security = security
}

Expand Down
16 changes: 8 additions & 8 deletions openapi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import "encoding/json"
// OpenAPI represents the root document object of
// an OpenAPI document.
type OpenAPI struct {
OpenAPI string `json:"openapi" yaml:"openapi"`
Info *Info `json:"info" yaml:"info"`
Servers []*Server `json:"servers,omitempty" yaml:"servers,omitempty"`
Paths Paths `json:"paths" yaml:"paths"`
Components *Components `json:"components,omitempty" yaml:"components,omitempty"`
Tags []*Tag `json:"tags,omitempty" yaml:"tags,omitempty"`
Security *SecurityRequirement `json:"security,omitempty" yaml:"security,omitempty"`
XTagGroups []*XTagGroup `json:"x-tagGroups,omitempty" yaml:"x-tagGroups,omitempty"`
OpenAPI string `json:"openapi" yaml:"openapi"`
Info *Info `json:"info" yaml:"info"`
Servers []*Server `json:"servers,omitempty" yaml:"servers,omitempty"`
Paths Paths `json:"paths" yaml:"paths"`
Components *Components `json:"components,omitempty" yaml:"components,omitempty"`
Tags []*Tag `json:"tags,omitempty" yaml:"tags,omitempty"`
Security []*SecurityRequirement `json:"security,omitempty" yaml:"security,omitempty"`
XTagGroups []*XTagGroup `json:"x-tagGroups,omitempty" yaml:"x-tagGroups,omitempty"`
}

// Components holds a set of reusable objects for different
Expand Down
28 changes: 21 additions & 7 deletions testdata/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,34 @@
}
}
}
],
"security": [
{
"api_key": []
},
{
"oauth2": [
"write:pets",
"read:pets"
]
}
],
"security": {
"api_key": [],
"oauth2": [
"write:pets",
"read:pets"
]
},
"paths": {
"/test/{a}": {
"get": {
"summary": "Test-A",
"description": "Test routes",
"operationId": "GetTest",
"parameters": [
{
"name": "a",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
Expand Down
14 changes: 10 additions & 4 deletions testdata/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ servers:
default: v2
description: version of the API
security:
api_key: []
oauth2:
- write:pets
- read:pets
- api_key: []
- oauth2:
- write:pets
- read:pets
paths:
/test/{a}:
get:
summary: Test-A
description: Test routes
operationId: GetTest
parameters:
- name: a
in: path
required: true
schema:
type: string
responses:
'200':
description: OK
Expand Down

0 comments on commit 8ad9884

Please sign in to comment.