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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ If you want to join the committee, please kindly apply by sending an email to wi
| Dart | @ircecho (2017/07) |
| Eiffel | @jvelilla (2017/09) |
| Elixir | |
| Erlang | |
| Erlang | @tsloughter (2017/11) |
| Groovy | |
| Go | |
| Haskell | |
Expand Down
6 changes: 3 additions & 3 deletions modules/swagger-codegen/src/main/resources/go/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Class | Method | HTTP request | Description

Example
```
auth := context.WithValue(context.TODO(), sw.ContextAPIKey, sw.APIKey{
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
Expand All @@ -57,7 +57,7 @@ Example

Example
```
auth := context.WithValue(context.TODO(), sw.ContextBasicAuth, sw.BasicAuth{
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
UserName: "username",
Password: "password",
})
Expand All @@ -73,7 +73,7 @@ Example

Example
```
auth := context.WithValue(context.TODO(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
r, err := client.Service.Operation(auth, args)
```

Expand Down
6 changes: 3 additions & 3 deletions modules/swagger-codegen/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type {{classname}}Service service

/* {{{classname}}}Service {{summary}}{{#notes}}
{{notes}}{{/notes}}
{{#hasAuthMethods}} * @param ctx context.Context Authentication Context {{/hasAuthMethods}}
* @param ctx context.Context for authentication, logging, tracing, etc.
{{#allParams}}{{#required}} @param {{paramName}} {{description}}
{{/required}}{{/allParams}}{{#hasOptionalParams}} @param optional (nil or map[string]interface{}) with one or more of:
{{#allParams}}{{^required}} @param "{{paramName}}" ({{dataType}}) {{description}}
{{/required}}{{/allParams}}{{/hasOptionalParams}} @return {{#returnType}}{{{returnType}}}{{/returnType}}*/
func (a *{{{classname}}}Service) {{{nickname}}}({{#hasAuthMethods}}ctx context.Context, {{/hasAuthMethods}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals map[string]interface{}{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}} *http.Response, error) {
func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals map[string]interface{}{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}} *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("{{httpMethod}}")
localVarPostBody interface{}
Expand Down Expand Up @@ -201,7 +201,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}({{#hasAuthMethods}}ctx context.C
}
{{/isApiKey}}
{{/authMethods}}
r, err := a.client.prepareRequest({{#hasAuthMethods}}ctx, {{/hasAuthMethods}}{{^hasAuthMethods}}nil, {{/hasAuthMethods}}localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return {{#returnType}}successPayload, {{/returnType}}nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,13 @@ func (c *APIClient) prepareRequest (
// Add the user agent to the request.
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)

// Walk through any authentication.

if ctx != nil {
// add context to the request
localVarRequest = localVarRequest.WithContext(ctx)

// Walk through any authentication.

// OAuth2 authentication
if tok, ok := ctx.Value(ContextOAuth2).(oauth2.TokenSource); ok {
// We were able to grab an oauth2 token from the context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ Method | HTTP request | Description
{{#operations}}
{{#operation}}
# **{{{operationId}}}**
> {{#returnType}}{{{returnType}}} {{/returnType}}{{{operationId}}}({{#authMethods}}ctx, {{/authMethods}}{{#allParams}}{{#required}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}optional{{/hasOptionalParams}})
> {{#returnType}}{{{returnType}}} {{/returnType}}{{{operationId}}}(ctx, {{#allParams}}{{#required}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}optional{{/hasOptionalParams}})
{{{summary}}}{{#notes}}

{{{notes}}}{{/notes}}

### Required Parameters
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}}
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------{{#authMethods}}
**ctx** | **context.Context** | context containing the authentication | nil if no authentication{{/authMethods}}{{/-last}}{{/allParams}}{{#allParams}}{{#required}}
------------- | ------------- | ------------- | -------------
**ctx** | **context.Context** | context for logging, tracing, authentication, etc.{{/-last}}{{/allParams}}{{#allParams}}{{#required}}
**{{paramName}}** | {{#isFile}}**{{dataType}}**{{/isFile}}{{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/required}}{{/allParams}}{{#hasOptionalParams}}
**optional** | **map[string]interface{}** | optional parameters | nil if no parameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,37 @@ import (
"net/http"
)

const ContextOAuth2 int = 1
const ContextBasicAuth int = 2
const ContextAccessToken int = 3
const ContextAPIKey int = 4
// contextKeys are used to identify the type of value in the context.
// Since these are string, it is possible to get a short description of the
// context key for logging and debugging using key.String().

type contextKey string

func (c contextKey) String() string {
return "auth " + string(c)
}

var (
// ContextOAuth2 takes a oauth2.TokenSource as authentication for the request.
ContextOAuth2 = contextKey("token")

// ContextBasicAuth takes BasicAuth as authentication for the request.
ContextBasicAuth = contextKey("basic")

// ContextAccessToken takes a string oauth2 access token as authentication for the request.
ContextAccessToken = contextKey("accesstoken")

// ContextAPIKey takes an APIKey as authentication for the request
ContextAPIKey = contextKey("apikey")
)

// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
type BasicAuth struct {
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
}

// APIKey provides API key based authentication to a request passed via context using ContextAPIKey
type APIKey struct {
Key string
Prefix string
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore-security-test/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Class | Method | HTTP request | Description

Example
```
auth := context.WithValue(context.TODO(), sw.ContextAPIKey, sw.APIKey{
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
Expand All @@ -52,7 +52,7 @@ Example

Example
```
auth := context.WithValue(context.TODO(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
r, err := client.Service.Operation(auth, args)
```

Expand Down
7 changes: 6 additions & 1 deletion samples/client/petstore-security-test/go/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,13 @@ func (c *APIClient) prepareRequest (
// Add the user agent to the request.
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)

// Walk through any authentication.

if ctx != nil {
// add context to the request
localVarRequest = localVarRequest.WithContext(ctx)

// Walk through any authentication.

// OAuth2 authentication
if tok, ok := ctx.Value(ContextOAuth2).(oauth2.TokenSource); ok {
// We were able to grab an oauth2 token from the context
Expand Down
29 changes: 25 additions & 4 deletions samples/client/petstore-security-test/go/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,37 @@ import (
"net/http"
)

const ContextOAuth2 int = 1
const ContextBasicAuth int = 2
const ContextAccessToken int = 3
const ContextAPIKey int = 4
// contextKeys are used to identify the type of value in the context.
// Since these are string, it is possible to get a short description of the
// context key for logging and debugging using key.String().

type contextKey string

func (c contextKey) String() string {
return "auth " + string(c)
}

var (
// ContextOAuth2 takes a oauth2.TokenSource as authentication for the request.
ContextOAuth2 = contextKey("token")

// ContextBasicAuth takes BasicAuth as authentication for the request.
ContextBasicAuth = contextKey("basic")

// ContextAccessToken takes a string oauth2 access token as authentication for the request.
ContextAccessToken = contextKey("accesstoken")

// ContextAPIKey takes an APIKey as authentication for the request
ContextAPIKey = contextKey("apikey")
)

// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
type BasicAuth struct {
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
}

// APIKey provides API key based authentication to a request passed via context using ContextAPIKey
type APIKey struct {
Key string
Prefix string
Expand Down
3 changes: 2 additions & 1 deletion samples/client/petstore-security-test/go/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ Method | HTTP request | Description


# **TestCodeInjectEndRnNR**
> TestCodeInjectEndRnNR(optional)
> TestCodeInjectEndRnNR(ctx, optional)
To test code injection *_/ ' \" =end -- \\r\\n \\n \\r

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**ctx** | **context.Context** | context for logging, tracing, authentication, etc.
**optional** | **map[string]interface{}** | optional parameters | nil if no parameters

### Optional Parameters
Expand Down
6 changes: 3 additions & 3 deletions samples/client/petstore-security-test/go/fake_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type FakeApiService service


/* FakeApiService To test code injection *_/ ' \" =end -- \\r\\n \\n \\r

* @param ctx context.Context for authentication, logging, tracing, etc.
@param optional (nil or map[string]interface{}) with one or more of:
@param "testCodeInjectEndRnNR" (string) To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
@return */
func (a *FakeApiService) TestCodeInjectEndRnNR(localVarOptionals map[string]interface{}) ( *http.Response, error) {
func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOptionals map[string]interface{}) ( *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Put")
localVarPostBody interface{}
Expand Down Expand Up @@ -72,7 +72,7 @@ func (a *FakeApiService) TestCodeInjectEndRnNR(localVarOptionals map[string]inte
if localVarTempParam, localVarOk := localVarOptionals["testCodeInjectEndRnNR"].(string); localVarOk {
localVarFormParams.Add("test code inject */ ' " =end -- \r\n \n \r", parameterToString(localVarTempParam, ""))
}
r, err := a.client.prepareRequest(nil, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore-security-test/go/git_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ git_remote=`git remote`
if [ "$git_remote" = "" ]; then # git remote not defined

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment."
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
Expand Down
32 changes: 16 additions & 16 deletions samples/client/petstore/go/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestOAuth2(t *testing.T) {

// then a fake tokenSource
tokenSource := cfg.TokenSource(createContext(nil), &tok)
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
auth := context.WithValue(context.Background(), sw.ContextOAuth2, tokenSource)

newPet := (sw.Pet{Id: 12992, Name: "gopher",
PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending", Tags: []sw.Tag{sw.Tag{Id: 1, Name: "tag2"}}})

r, err := client.PetApi.AddPet(nil, newPet)
r, err := client.PetApi.AddPet(context.Background(), newPet)

if err != nil {
t.Errorf("Error while adding pet")
Expand All @@ -60,7 +60,7 @@ func TestOAuth2(t *testing.T) {
if r.StatusCode != 200 {
t.Log(r)
}
reqb, err := httputil.DumpRequest(r.Request, true)
reqb, _ := httputil.DumpRequest(r.Request, true)

if !strings.Contains((string)(reqb), "Authorization: Bearer FAKE") {
t.Errorf("OAuth2 Authentication is missing")
Expand All @@ -69,7 +69,7 @@ func TestOAuth2(t *testing.T) {

func TestBasicAuth(t *testing.T) {

auth := context.WithValue(context.TODO(), sw.ContextBasicAuth, sw.BasicAuth{
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
UserName: "fakeUser",
Password: "f4k3p455",
})
Expand All @@ -96,14 +96,14 @@ func TestBasicAuth(t *testing.T) {
if r.StatusCode != 200 {
t.Log(r)
}
reqb, err := httputil.DumpRequest(r.Request, true)
reqb, _ := httputil.DumpRequest(r.Request, true)
if !strings.Contains((string)(reqb), "Authorization: Basic ZmFrZVVzZXI6ZjRrM3A0NTU") {
t.Errorf("Basic Authentication is missing")
}
}

func TestAccessToken(t *testing.T) {
auth := context.WithValue(context.TODO(), sw.ContextAccessToken, "TESTFAKEACCESSTOKENISFAKE")
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "TESTFAKEACCESSTOKENISFAKE")

newPet := (sw.Pet{Id: 12992, Name: "gopher",
PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending", Tags: []sw.Tag{sw.Tag{Id: 1, Name: "tag2"}}})
Expand All @@ -127,19 +127,19 @@ func TestAccessToken(t *testing.T) {
if r.StatusCode != 200 {
t.Log(r)
}
reqb, err := httputil.DumpRequest(r.Request, true)
reqb, _ := httputil.DumpRequest(r.Request, true)
if !strings.Contains((string)(reqb), "Authorization: Bearer TESTFAKEACCESSTOKENISFAKE") {
t.Errorf("AccessToken Authentication is missing")
}
}

func TestAPIKeyNoPrefix(t *testing.T) {
auth := context.WithValue(context.TODO(), sw.ContextAPIKey, sw.APIKey{Key: "TEST123"})
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{Key: "TEST123"})

newPet := (sw.Pet{Id: 12992, Name: "gopher",
PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending", Tags: []sw.Tag{sw.Tag{Id: 1, Name: "tag2"}}})

r, err := client.PetApi.AddPet(nil, newPet)
r, err := client.PetApi.AddPet(context.Background(), newPet)

if err != nil {
t.Errorf("Error while adding pet")
Expand All @@ -155,7 +155,7 @@ func TestAPIKeyNoPrefix(t *testing.T) {
t.Log(err)
}

reqb, err := httputil.DumpRequest(r.Request, true)
reqb, _ := httputil.DumpRequest(r.Request, true)
if !strings.Contains((string)(reqb), "Api_key: TEST123") {
t.Errorf("APIKey Authentication is missing")
}
Expand All @@ -171,7 +171,7 @@ func TestAPIKeyNoPrefix(t *testing.T) {
}

func TestAPIKeyWithPrefix(t *testing.T) {
auth := context.WithValue(context.TODO(), sw.ContextAPIKey, sw.APIKey{Key: "TEST123", Prefix: "Bearer"})
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{Key: "TEST123", Prefix: "Bearer"})

newPet := (sw.Pet{Id: 12992, Name: "gopher",
PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending", Tags: []sw.Tag{sw.Tag{Id: 1, Name: "tag2"}}})
Expand All @@ -192,7 +192,7 @@ func TestAPIKeyWithPrefix(t *testing.T) {
t.Log(err)
}

reqb, err := httputil.DumpRequest(r.Request, true)
reqb, _ := httputil.DumpRequest(r.Request, true)
if !strings.Contains((string)(reqb), "Api_key: Bearer TEST123") {
t.Errorf("APIKey Authentication is missing")
}
Expand All @@ -212,7 +212,7 @@ func TestDefaultHeader(t *testing.T) {
newPet := (sw.Pet{Id: 12992, Name: "gopher",
PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending", Tags: []sw.Tag{sw.Tag{Id: 1, Name: "tag2"}}})

r, err := client.PetApi.AddPet(nil, newPet)
r, err := client.PetApi.AddPet(context.Background(), newPet)

if err != nil {
t.Errorf("Error while adding pet")
Expand All @@ -222,7 +222,7 @@ func TestDefaultHeader(t *testing.T) {
t.Log(r)
}

r, err = client.PetApi.DeletePet(nil, 12992, nil)
r, err = client.PetApi.DeletePet(context.Background(), 12992, nil)

if err != nil {
t.Errorf("Error while deleting pet by id")
Expand All @@ -231,14 +231,14 @@ func TestDefaultHeader(t *testing.T) {
if r.StatusCode != 200 {
t.Log(r)
}
reqb, err := httputil.DumpRequest(r.Request, true)
reqb, _ := httputil.DumpRequest(r.Request, true)
if !strings.Contains((string)(reqb), "Testheader: testvalue") {
t.Errorf("Default Header is missing")
}
}

func TestHostOverride(t *testing.T) {
_, r, err := client.PetApi.FindPetsByStatus(nil, nil)
_, r, err := client.PetApi.FindPetsByStatus(context.Background(), nil)

if err != nil {
t.Errorf("Error while finding pets by status")
Expand Down
Loading