Generate types from components/securitySchemes to be used as a key in context.WithValue#1187
Conversation
| } | ||
|
|
||
| // openIdContextKey is the context key for OpenId security scheme | ||
| type openIdContextKey string |
There was a problem hiding this comment.
would we get any benefits from
| type openIdContextKey string | |
| type openIdContextKey = string |
I feel this would reduce the limited impact of a breaking change, but not sure if it's worth it for any other reason.
There was a problem hiding this comment.
Yes, that approach would avoid a breaking change.
However, by using a type alias, it ends up being the same as using the string type, so it doesn't change the situation.
package main
import (
"context"
"fmt"
)
type keyType1 = string // type alias
type keyType2 = string
const key1 keyType1 = "KEY"
const key2 keyType2 = "KEY"
func main() {
ctx := context.Background()
ctx = context.WithValue(ctx, key1, "VALUE")
// key1 conflicts with key2
fmt.Println(ctx.Value(key1)) // => VALUE
fmt.Println(ctx.Value(key2)) // => VALUE (want: nil)
}$ staticcheck ./example.go
example.go:16:31: should not use built-in type string as key for value; define your own type to avoid collisions (SA1029)|
@jamietanna any updates on this? I would really love seeing this merged. Not like I'm the one who makes decision here, yet I think it's good to go. |
|
You're right, this is an oversight in my original code and we should fix it. I'm pulling this branch and fixing it up for the current state of the code before approving and merging. I think the small risk of breakage (that is easy to fix) is worth having correctness. |
Greptile SummaryThis PR implements a valuable improvement to address the The core implementation is sound and consistent: both the Go codegen path ( Three minor style issues were found: a doc comment references the wrong function name, an unused template parameter, and no-op parentheses that look like a failed type cast. Confidence Score: 4/5
Last reviewed commit: 2791e30 |
Additional Comments (2)
Add the
This echo v5 template was not updated alongside echo v1. Apply the same fix as echo v1: |
The constants template used `sanitizeGoIdentity | lcFirst` to derive the context key type name, while the type definition in codegen.go used `LowercaseFirstCharacter(SchemaNameToTypeName(...))`. For security scheme names containing underscores (e.g. `api_key`, `petstore_auth`), these produced different results (`api_keyContextKey` vs `apiKeyContextKey`), causing compilation errors. Fix by exposing `SchemaNameToTypeName` as a template function and using it in the constants template so both paths produce the same type name. All affected generated files have been regenerated. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Additional Comments (2)
The echo v4 template was correctly updated with
Iris's |
Gin, Iris, and Echo v5 use framework-level string-keyed maps for their Set() methods, which require a string argument. With the new typed context key constants, these calls fail to compile without an explicit string() conversion. Echo v4 already had this cast. Chi, gorilla, stdhttp, and fiber use context.WithValue (which accepts any) and are unaffected. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
Generate types from
components/securitySchemesto be used as a key incontext.WithValue.Background
For example, prepare the following API schema and configuration.
schemas.yaml
config.yaml
If you run oapi-codegen, the following code is generated.
The constant
BearerAuthScopesis generated as a key forcontext.WithValue, but its type is astringand there is a risk of collision between packages.https://pkg.go.dev/context#WithValue
If you use staticcheck, it will also warn this.
https://staticcheck.dev/docs/checks/#SA1029
This can be avoided by defining a type as shown below.
In this PR, I have implemented to generate types from
components/securitySchemesto be used as keys forcontext.WithValue, as shown above.Considerations
While the implementation has been done to avoid breaking the existing code as much as possible, if users are directly using the underlying string (in this case,
"BearerAuth.Scopes), they will no longer be able to extract values from thecontext.However, this is considered to be a rare case.