Skip to content

Commit b2a361a

Browse files
committed
Merge branch 'go-with-formurlencoded' of https://github.com/beardeddragon5/swagger-codegen into beardeddragon5-go-with-formurlencoded
# Conflicts: # samples/client/petstore-security-test/go/.swagger-codegen/VERSION # samples/client/petstore-security-test/go/api/swagger.yaml # samples/client/petstore-security-test/go/docs/FakeApi.md
2 parents a35ca36 + be8a5d4 commit b2a361a

File tree

8 files changed

+656
-5
lines changed

8 files changed

+656
-5
lines changed

modules/swagger-codegen/src/main/resources/go/client.mustache

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (c *APIClient) prepareRequest(
185185
}
186186

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

227+
if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
228+
if body != nil {
229+
return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.")
230+
}
231+
body = &bytes.Buffer{}
232+
body.WriteString(formParams.Encode())
233+
// Set Content-Length
234+
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
235+
}
236+
227237
// Setup path and query parameters
228238
url, err := url.Parse(path)
229239
if err != nil {
@@ -465,4 +475,4 @@ func (e GenericSwaggerError) Body() []byte {
465475
// Model returns the unpacked model of the error
466476
func (e GenericSwaggerError) Model() interface{} {
467477
return e.model
468-
}
478+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
<<<<<<< HEAD
12
2.3.1

samples/client/petstore-security-test/go/api/swagger.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ definitions:
6363
type: "integer"
6464
format: "int32"
6565
description: "property description */ ' \" =end -- \\r\\n \\n \\r"
66+
<<<<<<< HEAD
6667
description: "Model for testing reserved words */ ' \" =end -- \\r\\n \\n \\r"
6768
xml:
6869
name: "Return"
70+
=======
71+
xml:
72+
name: "Return"
73+
description: "Model for testing reserved words */ ' \" =end -- \\r\\n \\n \\r"
74+
>>>>>>> be8a5d44b898ae043444f6a78792e96dbde6fe8b
6975
externalDocs:
7076
description: "Find out more about Swagger */ ' \" =end -- \\r\\n \\n \\r"
7177
url: "http://swagger.io"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
3+
*
4+
* 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 --
5+
*
6+
* API version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
7+
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
8+
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
9+
*/
10+
11+
package swagger
12+
13+
import (
14+
"context"
15+
"io/ioutil"
16+
"net/http"
17+
"net/url"
18+
"strings"
19+
"github.com/antihax/optional"
20+
)
21+
22+
// Linger please
23+
var (
24+
_ context.Context
25+
)
26+
27+
type FakeApiService service
28+
29+
/*
30+
FakeApiService To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
31+
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
32+
* @param optional nil or *TestCodeInjectEndRnNROpts - Optional Parameters:
33+
* @param "TestCodeInjectEndRnNR" (optional.String) - To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
34+
35+
36+
*/
37+
38+
type TestCodeInjectEndRnNROpts struct {
39+
TestCodeInjectEndRnNR optional.String
40+
}
41+
42+
func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOptionals *TestCodeInjectEndRnNROpts) (*http.Response, error) {
43+
var (
44+
localVarHttpMethod = strings.ToUpper("Put")
45+
localVarPostBody interface{}
46+
localVarFileName string
47+
localVarFileBytes []byte
48+
49+
)
50+
51+
// create path and map variables
52+
localVarPath := a.client.cfg.BasePath + "/fake"
53+
54+
localVarHeaderParams := make(map[string]string)
55+
localVarQueryParams := url.Values{}
56+
localVarFormParams := url.Values{}
57+
58+
// to determine the Content-Type header
59+
localVarHttpContentTypes := []string{"application/json", "*_/ ' =end -- "}
60+
61+
// set Content-Type header
62+
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
63+
if localVarHttpContentType != "" {
64+
localVarHeaderParams["Content-Type"] = localVarHttpContentType
65+
}
66+
67+
// to determine the Accept header
68+
localVarHttpHeaderAccepts := []string{"application/json", "*_/ ' =end -- "}
69+
70+
// set Accept header
71+
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
72+
if localVarHttpHeaderAccept != "" {
73+
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
74+
}
75+
if localVarOptionals != nil && localVarOptionals.TestCodeInjectEndRnNR.IsSet() {
76+
localVarFormParams.Add("test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r", parameterToString(localVarOptionals.TestCodeInjectEndRnNR.Value(), ""))
77+
}
78+
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
localVarHttpResponse, err := a.client.callAPI(r)
84+
if err != nil || localVarHttpResponse == nil {
85+
return localVarHttpResponse, err
86+
}
87+
88+
localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
89+
localVarHttpResponse.Body.Close()
90+
if err != nil {
91+
return localVarHttpResponse, err
92+
}
93+
94+
95+
if localVarHttpResponse.StatusCode >= 300 {
96+
newErr := GenericSwaggerError{
97+
body: localVarBody,
98+
error: localVarHttpResponse.Status,
99+
}
100+
101+
return localVarHttpResponse, newErr
102+
}
103+
104+
return localVarHttpResponse, nil
105+
}

0 commit comments

Comments
 (0)