Skip to content

Commit 6199c88

Browse files
committed
multiple set-cookies response header support
1 parent bd9ecc2 commit 6199c88

5 files changed

Lines changed: 44 additions & 26 deletions

File tree

pkg/codegen/templates/authenticator.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if gt (len .SecuritySchemeProviderNames) 0 }}
1+
{{if gt (len .SecuritySchemeProviderNames) 0}}
22
type Authenticator interface {
33
{{range $ProviderName := .SecuritySchemeProviderNames}}
44
{{- $ProviderName | sanitizeGoIdentity | ucFirst}}Auth(r *http.Request) (interface{}, error)

pkg/codegen/templates/constants.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if gt (len .SecuritySchemeProviderNames) 0 }}
1+
{{if gt (len .SecuritySchemeProviderNames) 0}}
22
const (
33
{{range $ProviderName := .SecuritySchemeProviderNames}}
44
{{- $ProviderName | sanitizeGoIdentity | ucFirst}}Scopes = "{{$ProviderName}}.Scopes"

pkg/codegen/templates/strict/strict-http.tmpl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,24 @@ const SecurityAuthContextKey = StrictHTTPContextKeyType("Secutiry-Auth")
3535
// {{$opid}} operation middleware
3636
func (sh *strictHandler) {{.OperationId}}(w http.ResponseWriter, r *http.Request{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params {{.OperationId}}Params{{end}}) {
3737
{{/* __BEGIN_CYLONIX_MOD__ */}}
38-
{{- if gt (len .SecurityDefinitions) 0 }}
38+
{{- if gt (len .SecurityDefinitions) 0 -}}
3939
var auth interface{}
40-
{{range $i, $s := .SecurityDefinitions}}
41-
{{- if gt $i 0 }}
40+
{{- range $i, $s := .SecurityDefinitions}}
41+
{{- if gt $i 0}}
4242
if auth == nil {
43-
{{end}}
43+
{{- end}}
4444
auth, _ = sh.ssi.GetAuthenticator().{{.ProviderName | sanitizeGoIdentity | ucFirst}}Auth(r)
45-
{{- if gt $i 0 }}
45+
{{- if gt $i 0}}
4646
}
47-
{{end}}
48-
{{end}}
47+
{{- end}}
48+
{{- end}}
4949
if auth == nil {
5050
http.Error(w, "Unauthorized!", http.StatusUnauthorized)
5151
return
5252
}
5353
ctx := context.WithValue(r.Context(), SecurityAuthContextKey, auth)
5454
r = r.WithContext(ctx)
55-
{{end}}
55+
{{- end}}
5656
{{/* __END_CYLONIX_MOD__ */}}
5757

5858
var request {{$opid | ucFirst}}RequestObject

pkg/codegen/templates/strict/strict-interface.tmpl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@
8383
}
8484
{{end -}}
8585
{{range $headers -}}
86-
w.Header().Set("{{.Name}}", fmt.Sprint(response.Headers.{{.GoName}}))
86+
{{/* __BEGIN_CYLONIX_MOD__*/}}
87+
{{- if and (eq .Name "Set-Cookie") (ne .Schema.ArrayType nil)}}
88+
// Special handling for https://github.com/OAI/OpenAPI-Specification/issues/1237
89+
for _, v := range response.Headers.{{.GoName}} {
90+
w.Header().Add("{{.Name}}", v)
91+
}
92+
{{else -}}
93+
w.Header().Set("{{.Name}}", fmt.Sprint(response.Headers.{{.GoName}}))
94+
{{- end -}}
95+
{{/* __END_CYLONIX_MOD__*/}}
8796
{{end -}}
8897
w.WriteHeader({{if $fixedStatusCode}}{{$statusCode}}{{else}}response.StatusCode{{end}})
8998
{{$hasBodyVar := or ($hasHeaders) (not $fixedStatusCode) (not .IsSupported)}}
@@ -128,7 +137,16 @@
128137
{{end -}}
129138
func (response {{$opid}}{{$statusCode}}Response) Visit{{$opid}}Response(w http.ResponseWriter) error {
130139
{{range $headers -}}
131-
w.Header().Set("{{.Name}}", fmt.Sprint(response.Headers.{{.GoName}}))
140+
{{/* __BEGIN_CYLONIX_MOD__*/}}
141+
{{- if and (eq .Name "Set-Cookie") (ne .Schema.ArrayType nil)}}
142+
// Special handling for https://github.com/OAI/OpenAPI-Specification/issues/1237
143+
for _, v := range response.Headers.{{.GoName}} {
144+
w.Header().Add("{{.Name}}", v)
145+
}
146+
{{else -}}
147+
w.Header().Set("{{.Name}}", fmt.Sprint(response.Headers.{{.GoName}}))
148+
{{- end -}}
149+
{{/* __END_CYLONIX_MOD__*/}}
132150
{{end -}}
133151
w.WriteHeader({{if $fixedStatusCode}}{{$statusCode}}{{else}}response.StatusCode{{end}})
134152
return nil

pkg/codegen/templates/strict/strict-server.tmpl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
// StrictServer implements StrictServerInterface with handlers to be customized.
44
type StrictServer struct {
5-
Authenticator Authenticator
6-
{{range .}}{{.SummaryAsComment }}
7-
// ({{.Method}} {{.Path}})
8-
{{$opid := .OperationId -}}
9-
{{$opid}}Handler func(ctx context.Context, request {{$opid | ucFirst}}RequestObject) ({{$opid | ucFirst}}ResponseObject, error)
10-
{{end}}{{/* range . */ -}}
5+
Authenticator Authenticator
6+
{{range .}}{{.SummaryAsComment }}
7+
// ({{.Method}} {{.Path}})
8+
{{$opid := .OperationId}}
9+
{{$opid}}Handler func(ctx context.Context, request {{$opid | ucFirst}}RequestObject) ({{$opid | ucFirst}}ResponseObject, error)
10+
{{end}}{{/* range . */}}
1111
}
1212

13-
{{range .}}{{.SummaryAsComment }}
14-
// ({{.Method}} {{.Path}})
15-
{{$opid := .OperationId -}}
16-
func (s *StrictServer) {{$opid}}(ctx context.Context, request {{$opid | ucFirst}}RequestObject) ({{$opid | ucFirst}}ResponseObject, error) {
17-
if s.{{$opid}}Handler != nil {
18-
return s.{{$opid}}Handler(ctx, request)
13+
{{range .}}{{.SummaryAsComment}}
14+
// ({{.Method}} {{.Path}})
15+
{{$opid := .OperationId}}
16+
func (s *StrictServer) {{$opid}}(ctx context.Context, request {{$opid | ucFirst}}RequestObject) ({{$opid | ucFirst}}ResponseObject, error) {
17+
if s.{{$opid}}Handler != nil {
18+
return s.{{$opid}}Handler(ctx, request)
19+
}
20+
return nil, errors.New("api {{$opid}} is not implemented")
1921
}
20-
return nil, errors.New("api {{$opid}} is not implemented")
21-
}
2222
{{end}}
2323

2424
func (s StrictServer) GetAuthenticator() Authenticator {

0 commit comments

Comments
 (0)