Skip to content
Merged
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
9 changes: 6 additions & 3 deletions examples/authenticated-api/echo/api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/authenticated-api/stdhttp/api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions internal/test/any_of/codegen/inline/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions internal/test/any_of/codegen/ref_schema/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion internal/test/client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/test/client/client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ paths:
schema:
type: object
components:
securitySchemes:
OpenId:
type: openIdConnect
openIdConnectUrl: https://example.com/.well-known/openid-configuration
schemas:
SchemaObject:
properties:
Expand Down
10 changes: 8 additions & 2 deletions internal/test/externalref/petstore/externalref.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/test/issues/issue-1087/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 14 additions & 11 deletions internal/test/schemas/schemas.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ func GenerateTypeDefinitions(t *template.Template, swagger *openapi3.T, ops []Op
return "", fmt.Errorf("error generating Go types for component request bodies: %w", err)
}
allTypes = append(allTypes, bodyTypes...)

securitySchemeTypes, err := GenerateTypesForSecuritySchemes(t, swagger.Components.SecuritySchemes)
if err != nil {
return "", fmt.Errorf("error generating Go types for component security schemes: %w", err)
}
allTypes = append(allTypes, securitySchemeTypes...)
}

// Go through all operations, and add their types to allTypes, so that we can
Expand Down Expand Up @@ -847,6 +853,29 @@ func GenerateTypesForRequestBodies(t *template.Template, bodies map[string]*open
return types, nil
}

// GenerateTypesForSecuritySchemes generates type definitions for any custom types defined in the
// components/securitySchemes section of the Swagger spec.
func GenerateTypesForSecuritySchemes(t *template.Template, schemes map[string]*openapi3.SecuritySchemeRef) ([]TypeDefinition, error) {
var types []TypeDefinition

for _, schemeName := range SortedSecuritySchemeKeys(schemes) {
// Generate a type to be used as a key in context.WithValue
goTypeName := LowercaseFirstCharacter(SchemaNameToTypeName(schemeName)) + "ContextKey"
Comment thread
mromaszewicz marked this conversation as resolved.
goType := Schema{
GoType: "string",
Description: fmt.Sprintf("is the context key for %s security scheme", schemeName),
}

types = append(types, TypeDefinition{
JsonName: schemeName,
TypeName: goTypeName,
Schema: goType,
})
}

return types, nil
}

// GenerateTypes passes a bunch of types to the template engine, and buffers
// its output into a string.
func GenerateTypes(t *template.Template, types []TypeDefinition) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ var TemplateFunctions = template.FuncMap{
"title": titleCaser.String,
"stripNewLines": stripNewLines,
"sanitizeGoIdentity": SanitizeGoIdentity,
"schemaNameToTypeName": SchemaNameToTypeName,
"toGoString": StringToGoString,
"toGoComment": StringWithTypeNameToGoComment,

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/constants.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- if gt (len .SecuritySchemeProviderNames) 0 }}
const (
{{range $ProviderName := .SecuritySchemeProviderNames}}
{{- $ProviderName | sanitizeGoIdentity | ucFirst}}Scopes = "{{$ProviderName}}.Scopes"
{{- $ProviderName | sanitizeGoIdentity | ucFirst}}Scopes {{$ProviderName | schemaNameToTypeName | lcFirst}}ContextKey = "{{$ProviderName}}.Scopes"
{{end}}
)
{{end}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/echo/echo-wrappers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx echo.Context) error {
{{end}}

{{range .SecurityDefinitions}}
ctx.Set({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes, {{toStringArray .Scopes}})
ctx.Set(string({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes), {{toStringArray .Scopes}})
{{end}}

{{if .RequiresParamObject}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/echo/v5/echo-wrappers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx *echo.Context) error {
{{end}}

{{range .SecurityDefinitions}}
ctx.Set({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes, {{toStringArray .Scopes}})
ctx.Set(string({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes), {{toStringArray .Scopes}})
{{end}}

{{if .RequiresParamObject}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/fiber/fiber-middleware.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
{{end}}

{{range .SecurityDefinitions}}
c.Context().SetUserValue({{.ProviderName | ucFirst}}Scopes, {{toStringArray .Scopes}})
c.Context().SetUserValue(({{.ProviderName | ucFirst}}Scopes), {{toStringArray .Scopes}})
{{end}}

{{if .RequiresParamObject}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/gin/gin-wrappers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) {
{{end}}

{{range .SecurityDefinitions}}
c.Set({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes, {{toStringArray .Scopes}})
c.Set(string({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes), {{toStringArray .Scopes}})
{{end}}

{{if .RequiresParamObject}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/iris/iris-middleware.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx iris.Context) {
{{end}}

{{range .SecurityDefinitions}}
ctx.Set({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes, {{toStringArray .Scopes}})
ctx.Set(string({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes), {{toStringArray .Scopes}})
{{end}}

{{if .RequiresParamObject}}
Expand Down
Loading