You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
staticcheck failes: define your own type to avoid collisions (SA1029) #1200
PR #1187 (merged as commit a13ef41) added GenerateTypesForSecuritySchemes which creates a custom unexported type per security scheme (e.g. 'type keyContextKey string'). The constants template was updated to use that type for the scope constants, satisfying staticcheck SA1029. Multiple generated test files confirm the typed constants are produced correctly.
Closing as fixed. If you can still reproduce on a recent release, please reopen with the exact spec and version used.
Great to see this improved on the project. I have personally also added for my own project something like the following to the constants.tmpl file to get some helper for converting strings to correct enums:
var (
// Err{{$Enum.TypeName}} defines an error if an invalid value gets mapped.
Err{{$Enum.TypeName}} = fmt.Errorf("invalid type for {{$Enum.TypeName}}")
stringTo{{$Enum.TypeName}} = map[string]{{$Enum.TypeName}}{
{{range $name, $value := $Enum.GetValues -}}
{{$Enum.ValueWrapper}}{{$value}}{{$Enum.ValueWrapper -}}: {{$name}},
{{end -}}
}
)
// To{{$Enum.TypeName}} acts as a helper to map a string to the defined enum.
func To{{$Enum.TypeName}}(val string) ({{$Enum.TypeName}}, error) {
if res, ok := stringTo{{$Enum.TypeName}}[val]; ok {
return res, nil
}
return {{$Enum.TypeName}}(""), Err{{$Enum.TypeName}}
}
Activity
[-]staticcheck [/-][+]staticcheck failes: define your own type to avoid collisions (SA1029)[/+]asger-noer commentedon Sep 21, 2023
I'm also having this issue. As far as I can see this is the section in need of modification.
https://github.com/deepmap/oapi-codegen/blob/46d269400f4bd1f4da2f5e6b84cdf7f3f2d753dd/pkg/codegen/templates/constants.tmpl#L1-L6
fix: security scopes have its own type
asger-noer commentedon Sep 21, 2023
But @tempcke, if you want to fix this before a fix is made in
oapi-codegenthere is an option to do the following:Create a local
templates/constants.tmpl:{{- if gt (len .SecuritySchemeProviderNames) 0 }} // contextKey provides a type for use with context.WithValue. type contextKey string const ( {{range $ProviderName := .SecuritySchemeProviderNames}} {{- $ProviderName | sanitizeGoIdentity | ucFirst}}Scopes contextKey = "{{$ProviderName}}.Scopes" {{end}} ) {{end}} {{range $Enum := .EnumDefinitions}} // Defines values for {{$Enum.TypeName}}. const ( {{range $name, $value := $Enum.GetValues}} {{$name}} {{$Enum.TypeName}} = {{$Enum.ValueWrapper}}{{$value}}{{$Enum.ValueWrapper -}} {{end}} ) {{end}}Then add this to your config file:
fix: security scopes have its own type
tboerger commentedon May 13, 2024
The snippet from @asger-noer works like a charm, would be great to get this fix included.
mromaszewicz commentedon May 1, 2026
PR #1187 (merged as commit a13ef41) added GenerateTypesForSecuritySchemes which creates a custom unexported type per security scheme (e.g. 'type keyContextKey string'). The constants template was updated to use that type for the scope constants, satisfying staticcheck SA1029. Multiple generated test files confirm the typed constants are produced correctly.
Closing as fixed. If you can still reproduce on a recent release, please reopen with the exact spec and version used.
tboerger commentedon May 7, 2026
Great to see this improved on the project. I have personally also added for my own project something like the following to the constants.tmpl file to get some helper for converting strings to correct enums: