Skip to content
Closed
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
14 changes: 12 additions & 2 deletions modules/swagger-codegen/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (c *APIClient) prepareRequest(
}

// add form parameters and file if available.
if len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
if body != nil {
return nil, errors.New("Cannot specify postBody and multipart form at the same time.")
}
Expand Down Expand Up @@ -224,6 +224,16 @@ func (c *APIClient) prepareRequest(
w.Close()
}

if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
if body != nil {
return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.")
}
body = &bytes.Buffer{}
body.WriteString(formParams.Encode())
// Set Content-Length
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
}

// Setup path and query parameters
url, err := url.Parse(path)
if err != nil {
Expand Down Expand Up @@ -465,4 +475,4 @@ func (e GenericSwaggerError) Body() []byte {
// Model returns the unpacked model of the error
func (e GenericSwaggerError) Model() interface{} {
return e.model
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0-SNAPSHOT
2.4.0-SNAPSHOT
34 changes: 17 additions & 17 deletions samples/client/petstore-security-test/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This API client was generated by the [swagger-codegen](https://github.com/swagge

## Installation
Put the package under your project folder and add the following in import:
```
"./swagger"
```golang
import "./swagger"
```

## Documentation for API Endpoints
Expand All @@ -35,12 +35,12 @@ Class | Method | HTTP request | Description
- **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)
```golang
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
```
## petstore_auth
- **Type**: OAuth
Expand All @@ -51,20 +51,20 @@ Example
- **read:pets**: read your pets *_/ ' \" =end -- \\r\\n \\n \\r

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

Or via OAuth2 module to automatically refresh tokens and perform user authentication.
```
import "golang.org/x/oauth2"
```golang
import "golang.org/x/oauth2"

/ .. Perform OAuth2 round trip request and obtain a token .. //
/* Perform OAuth2 round trip request and obtain a token */

tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
r, err := client.Service.Operation(auth, args)
tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
r, err := client.Service.Operation(auth, args)
```

## Author
Expand Down
72 changes: 72 additions & 0 deletions samples/client/petstore-security-test/go/api/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
swagger: "2.0"
info:
description: "This spec is mainly for testing Petstore server and contains fake\
\ endpoints, models. Please do not use this for any other purpose. Special characters:\
\ \" \\ */ ' \" =end -- \r\n \n \r"
version: "1.0.0 */ ' \" =end -- \\r\\n \\n \\r"
title: "Swagger Petstore */ ' \" =end -- \\r\\n \\n \\r"
termsOfService: "http://swagger.io/terms/ */ ' \" =end -- \\r\\n \\n \\r"
contact:
email: "[email protected] */ ' \" =end -- \\r\\n \\n \\r"
license:
name: "Apache-2.0 */ ' \" =end -- \\r\\n \\n \\r"
url: "http://www.apache.org/licenses/LICENSE-2.0.html */ ' \" =end -- \\r\\n \\\
n \\r"
host: "petstore.swagger.io */ ' \" =end -- \\r\\n \\n \\r"
basePath: "/v2 */ ' \" =end -- \\r\\n \\n \\r"
tags:
- name: "fake"
description: "Everything about your Pets */ ' \" =end -- \\r\\n \\n \\r"
externalDocs:
description: "Find out more */ ' \" =end -- \\r\\n \\n \\r"
url: "http://swagger.io"
paths:
/fake:
put:
tags:
- "fake"
summary: "To test code injection */ ' \" =end -- \\r\\n \\n \\r"
operationId: "testCodeInject */ ' \" =end -- \\r\\n \\n \\r"
consumes:
- "application/json"
- "*/ ' \" =end -- \r\n \n \r"
produces:
- "application/json"
- "*/ ' \" =end -- \r\n \n \r"
parameters:
- name: "test code inject */ ' \" =end -- \\r\\n \\n \\r"
in: "formData"
description: "To test code injection */ ' \" =end -- \\r\\n \\n \\r"
required: false
type: "string"
x-exportParamName: "TestCodeInjectEndRnNR"
x-optionalDataType: "String"
responses:
400:
description: "To test code injection */ ' \" =end -- \\r\\n \\n \\r"
securityDefinitions:
petstore_auth:
type: "oauth2"
authorizationUrl: "http://petstore.swagger.io/api/oauth/dialog"
flow: "implicit"
scopes:
write:pets: "modify pets in your account */ ' \" =end -- \\r\\n \\n \\r"
read:pets: "read your pets */ ' \" =end -- \\r\\n \\n \\r"
api_key:
type: "apiKey"
name: "api_key */ ' \" =end -- \\r\\n \\n \\r"
in: "header"
definitions:
Return:
properties:
return:
type: "integer"
format: "int32"
description: "property description */ ' \" =end -- \\r\\n \\n \\r"
xml:
name: "Return"
description: "Model for testing reserved words */ ' \" =end -- \\r\\n \\n \\r"
externalDocs:
description: "Find out more about Swagger */ ' \" =end -- \\r\\n \\n \\r"
url: "http://swagger.io"
105 changes: 105 additions & 0 deletions samples/client/petstore-security-test/go/api_fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* API version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/

package swagger

import (
"context"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/antihax/optional"
)

// Linger please
var (
_ context.Context
)

type FakeApiService service

/*
FakeApiService To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param optional nil or *TestCodeInjectEndRnNROpts - Optional Parameters:
* @param "TestCodeInjectEndRnNR" (optional.String) - To test code injection *_/ ' \" =end -- \\r\\n \\n \\r


*/

type TestCodeInjectEndRnNROpts struct {
TestCodeInjectEndRnNR optional.String
}

func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOptionals *TestCodeInjectEndRnNROpts) (*http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Put")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte

)

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

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

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

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

// to determine the Accept header
localVarHttpHeaderAccepts := []string{"application/json", "*_/ ' =end -- "}

// set Accept header
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
if localVarOptionals != nil && localVarOptionals.TestCodeInjectEndRnNR.IsSet() {
localVarFormParams.Add("test code inject */ ' " =end -- \r\n \n \r", parameterToString(localVarOptionals.TestCodeInjectEndRnNR.Value(), ""))
}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}

localVarHttpResponse, err := a.client.callAPI(r)
if err != nil || localVarHttpResponse == nil {
return localVarHttpResponse, err
}

localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
localVarHttpResponse.Body.Close()
if err != nil {
return localVarHttpResponse, err
}


if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericSwaggerError{
body: localVarBody,
error: localVarHttpResponse.Status,
}

return localVarHttpResponse, newErr
}

return localVarHttpResponse, nil
}
Loading