Skip to content

Commit

Permalink
Allow explicit empty security definition to overwrite existing defini…
Browse files Browse the repository at this point in the history
…tions.

It enables to clear default security configuration in operationObject.Security (to make public api).
protoc-gen-swagger doesn't allow empty array (and it causes segfault) in operationObject.Security but it is really needed
resetting security requirement configuration by Swagger.
  • Loading branch information
co3k authored and johanbrandhorst committed Jun 23, 2018
1 parent 11bef10 commit 41568b5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,21 +653,26 @@ func renderServices(services []*descriptor.Service, paths swaggerPathsObject, re
}
if opts.Security != nil {
newSecurity := []swaggerSecurityRequirementObject{}
if operationObject.Security == nil {
newSecurity = []swaggerSecurityRequirementObject{}
} else {
newSecurity = operationObject.Security
if operationObject.Security != nil {
newSecurity = *operationObject.Security
}
for _, secReq := range opts.Security {
newSecReq := swaggerSecurityRequirementObject{}
for secReqKey, secReqValue := range secReq.SecurityRequirement {
if secReqValue == nil {
continue
}

newSecReqValue := make([]string, len(secReqValue.Scope))
copy(newSecReqValue, secReqValue.Scope)
newSecReq[secReqKey] = newSecReqValue
}
newSecurity = append(newSecurity, newSecReq)

if len(newSecReq) > 0 {
newSecurity = append(newSecurity, newSecReq)
}
}
operationObject.Security = newSecurity
operationObject.Security = &newSecurity
}
if opts.Responses != nil {
for name, resp := range opts.Responses {
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-swagger/genswagger/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type swaggerOperationObject struct {
Tags []string `json:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`

Security []swaggerSecurityRequirementObject `json:"security,omitempty"`
Security *[]swaggerSecurityRequirementObject `json:"security,omitempty"`
ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
}

Expand Down

0 comments on commit 41568b5

Please sign in to comment.