Skip to content

Commit

Permalink
[GO CLIENT] Fix issue with generating code for API key (swagger-api#6630
Browse files Browse the repository at this point in the history
)

* [GO CLIENT] Fix Go security query parameter generation by using correct string literal and using the Add method.

* Add generated files.
  • Loading branch information
garetht authored and fvarose committed Oct 12, 2017
1 parent 0e1217b commit 31039ec
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 10 deletions.
2 changes: 1 addition & 1 deletion modules/swagger-codegen/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}({{#hasAuthMethods}}ctx context.C
} else {
key = auth.Key
}
{{#isKeyInHeader}}localVarHeaderParams["{{keyParamName}}"] = key{{/isKeyInHeader}}{{#isKeyInQuery}}localVarQueryParams['{{keyParamName}}'] = key{{/isKeyInQuery}}
{{#isKeyInHeader}}localVarHeaderParams["{{keyParamName}}"] = key{{/isKeyInHeader}}{{#isKeyInQuery}}localVarQueryParams.Add("{{keyParamName}}", key){{/isKeyInQuery}}
}
}
{{/isApiKey}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0-SNAPSHOT
5 changes: 5 additions & 0 deletions samples/client/petstore-security-test/go/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ func (c *APIClient) prepareRequest (
}
localVarRequest.Header = headers
}

// Override request host, if applicable
if c.cfg.Host != "" {
localVarRequest.Host = c.cfg.Host
}

// Add the user agent to the request.
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
Expand Down
14 changes: 13 additions & 1 deletion samples/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*AnotherFakeApi* | [**TestSpecialTags**](docs/AnotherFakeApi.md#testspecialtags) | **Patch** /another-fake/dummy | To test special tags
*FakeApi* | [**FakeOuterBooleanSerialize**](docs/FakeApi.md#fakeouterbooleanserialize) | **Post** /fake/outer/boolean |
*FakeApi* | [**FakeOuterCompositeSerialize**](docs/FakeApi.md#fakeoutercompositeserialize) | **Post** /fake/outer/composite |
*FakeApi* | [**FakeOuterNumberSerialize**](docs/FakeApi.md#fakeouternumberserialize) | **Post** /fake/outer/number |
Expand All @@ -29,7 +30,7 @@ Class | Method | HTTP request | Description
*FakeApi* | [**TestEndpointParameters**](docs/FakeApi.md#testendpointparameters) | **Post** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
*FakeApi* | [**TestEnumParameters**](docs/FakeApi.md#testenumparameters) | **Get** /fake | To test enum parameters
*FakeApi* | [**TestJsonFormData**](docs/FakeApi.md#testjsonformdata) | **Get** /fake/jsonFormData | test json serialization of form data
*Fake_classname_tags123Api* | [**TestClassname**](docs/Fake_classname_tags123Api.md#testclassname) | **Patch** /fake_classname_test | To test class name in snake case
*FakeClassnameTags123Api* | [**TestClassname**](docs/FakeClassnameTags123Api.md#testclassname) | **Patch** /fake_classname_test | To test class name in snake case
*PetApi* | [**AddPet**](docs/PetApi.md#addpet) | **Post** /pet | Add a new pet to the store
*PetApi* | [**DeletePet**](docs/PetApi.md#deletepet) | **Delete** /pet/{petId} | Deletes a pet
*PetApi* | [**FindPetsByStatus**](docs/PetApi.md#findpetsbystatus) | **Get** /pet/findByStatus | Finds Pets by status
Expand Down Expand Up @@ -97,6 +98,17 @@ Class | Method | HTTP request | Description
## api_key
- **Type**: API key

Example
```
auth := context.WithValue(context.TODO(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
```
## api_key_query
- **Type**: API key

Example
```
auth := context.WithValue(context.TODO(), sw.ContextAPIKey, sw.APIKey{
Expand Down
93 changes: 93 additions & 0 deletions samples/client/petstore/go/go-petstore/another_fake_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Swagger Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/

package petstore

import (
"net/url"
"net/http"
"strings"
"golang.org/x/net/context"
"encoding/json"
)

// Linger please
var (
_ context.Context
)

type AnotherFakeApiService service


/* AnotherFakeApiService To test special tags
To test special tags
@param body client model
@return Client*/
func (a *AnotherFakeApiService) TestSpecialTags(body Client) (Client, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Patch")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
successPayload Client
)

// create path and map variables
localVarPath := a.client.cfg.BasePath + "/another-fake/dummy"

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}


// to determine the Content-Type header
localVarHttpContentTypes := []string{ "application/json", }

// set Content-Type header
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}

// to determine the Accept header
localVarHttpHeaderAccepts := []string{
"application/json",
}

// set Accept header
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
// body params
localVarPostBody = &body
r, err := a.client.prepareRequest(nil, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return successPayload, nil, err
}

localVarHttpResponse, err := a.client.callAPI(r)
if err != nil || localVarHttpResponse == nil {
return successPayload, localVarHttpResponse, err
}
defer localVarHttpResponse.Body.Close()
if localVarHttpResponse.StatusCode >= 300 {
return successPayload, localVarHttpResponse, reportError(localVarHttpResponse.Status)
}

if err = json.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
return successPayload, localVarHttpResponse, err
}


return successPayload, localVarHttpResponse, err
}

6 changes: 4 additions & 2 deletions samples/client/petstore/go/go-petstore/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ type APIClient struct {
common service // Reuse a single struct instead of allocating one for each service on the heap.

// API Services
AnotherFakeApi *AnotherFakeApiService
FakeApi *FakeApiService
Fake_classname_tags123Api *Fake_classname_tags123ApiService
FakeClassnameTags123Api *FakeClassnameTags123ApiService
PetApi *PetApiService
StoreApi *StoreApiService
UserApi *UserApiService
Expand All @@ -67,8 +68,9 @@ func NewAPIClient(cfg *Configuration) *APIClient {
c.common.client = c

// API Services
c.AnotherFakeApi = (*AnotherFakeApiService)(&c.common)
c.FakeApi = (*FakeApiService)(&c.common)
c.Fake_classname_tags123Api = (*Fake_classname_tags123ApiService)(&c.common)
c.FakeClassnameTags123Api = (*FakeClassnameTags123ApiService)(&c.common)
c.PetApi = (*PetApiService)(&c.common)
c.StoreApi = (*StoreApiService)(&c.common)
c.UserApi = (*UserApiService)(&c.common)
Expand Down
36 changes: 36 additions & 0 deletions samples/client/petstore/go/go-petstore/docs/AnotherFakeApi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# \AnotherFakeApi

All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
[**TestSpecialTags**](AnotherFakeApi.md#TestSpecialTags) | **Patch** /another-fake/dummy | To test special tags


# **TestSpecialTags**
> Client TestSpecialTags(body)
To test special tags

To test special tags

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**Client**](Client.md)| client model |

### Return type

[**Client**](Client.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: application/json
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# \FakeClassnameTags123Api

All URIs are relative to *http://petstore.swagger.io:80/v2*

Method | HTTP request | Description
------------- | ------------- | -------------
[**TestClassname**](FakeClassnameTags123Api.md#TestClassname) | **Patch** /fake_classname_test | To test class name in snake case


# **TestClassname**
> Client TestClassname(ctx, body)
To test class name in snake case

### Required Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**ctx** | **context.Context** | context containing the authentication | nil if no authentication
**body** | [**Client**](Client.md)| client model |

### Return type

[**Client**](Client.md)

### Authorization

[api_key_query](../README.md#api_key_query)

### HTTP request headers

- **Content-Type**: application/json
- **Accept**: application/json

[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ This endpoint does not need any parameter.

### Return type

[**map[string]int32**](map.md)
**map[string]int32**

### Authorization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ var (
_ context.Context
)

type Fake_classname_tags123ApiService service
type FakeClassnameTags123ApiService service


/* Fake_classname_tags123ApiService To test class name in snake case
/* FakeClassnameTags123ApiService To test class name in snake case
* @param ctx context.Context Authentication Context
@param body client model
@return Client*/
func (a *Fake_classname_tags123ApiService) TestClassname(body Client) (Client, *http.Response, error) {
func (a *FakeClassnameTags123ApiService) TestClassname(ctx context.Context, body Client) (Client, *http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Patch")
localVarPostBody interface{}
Expand Down Expand Up @@ -68,7 +68,19 @@ func (a *Fake_classname_tags123ApiService) TestClassname(body Client) (Client,
}
// body params
localVarPostBody = &body
r, err := a.client.prepareRequest(nil, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
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
}
localVarQueryParams.Add("api_key_query", key)
}
}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return successPayload, nil, err
}
Expand Down

0 comments on commit 31039ec

Please sign in to comment.