swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
When using a spec file with multiple API keys defined, authentication can't be done in the generated Go code. The README.md file will state that you should set the same variable twice and that is exactly how the go code is set up as well. The authentication code found within every function in files like server_api.go and it looks like this:
// body params
localVarPostBody = &payload
if ctx != nil {
// API Key Authentication
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
var key string
if auth.Prefix != "" {
key = auth.Prefix + " " + auth.Key
} else {
key = auth.Key
}
localVarHeaderParams["X-Auth-Token"] = key
}
}
if ctx != nil {
// API Key Authentication
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
var key string
if auth.Prefix != "" {
key = auth.Prefix + " " + auth.Key
} else {
key = auth.Key
}
localVarHeaderParams["X-Auth-UserId"] = key
}
}
As expected, this doesn't work. This is what the readme looks like:
ApiKeyAuth
Type: API key
Example
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
ApiUserID
Type: API key
Example
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
Description
When using a spec file with multiple API keys defined, authentication can't be done in the generated Go code. The README.md file will state that you should set the same variable twice and that is exactly how the go code is set up as well. The authentication code found within every function in files like server_api.go and it looks like this:
As expected, this doesn't work. This is what the readme looks like:
ApiKeyAuth
Example
ApiUserID
Example
Swagger-codegen version
2.3.1.
I haven't tried other versions.
Swagger declaration file content or url
This is the relevant part:
Command line used for generation
swagger-codegen generate -l go -i spec.yaml -o swagger
Steps to reproduce
Add more than one API key to a a swagger file and generate a go client.
Related issues/PRs
None that I know of.
Suggest a fix/enhancement
I think changes are required to the mustache template for go to solve this, but I don't have the skill yet to do this.