Skip to content

Commit c26a387

Browse files
committed
Merge pull request #1271 from laurentvdl/master
implement form parameter support using Angular $httpParamSerializer
2 parents 2a5ab78 + 61690d6 commit c26a387

File tree

7 files changed

+200
-180
lines changed

7 files changed

+200
-180
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenOperation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ public boolean getHasPathParams() {
7777
return nonempty(pathParams);
7878
}
7979

80+
/**
81+
* Check if there's at least one form parameter
82+
*
83+
* @return true if any form parameter exists, false otherwise
84+
*/
85+
public boolean getHasFormParams() {
86+
return nonempty(formParams);
87+
}
88+
8089
}

modules/swagger-codegen/src/main/resources/TypeScript-Angular/api.mustache

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ module {{package}} {
1414
export class {{classname}} {
1515
private basePath = '{{basePath}}';
1616
17-
static $inject: string[] = ['$http'];
17+
static $inject: string[] = ['$http', '$httpParamSerializer'];
1818
19-
constructor(private $http: ng.IHttpService, basePath?: string) {
19+
constructor(private $http: ng.IHttpService, basePath?: string, private $httpParamSerializer?: (any) => any) {
2020
if (basePath) {
2121
this.basePath = basePath;
2222
}
@@ -32,7 +32,10 @@ module {{package}} {
3232
{{/pathParams}}
3333
var queryParameters: any = {};
3434
var headerParams: any = {};
35+
{{#hasFormParams}}
36+
var formParams: any = {};
3537

38+
{{/hasFormParams}}
3639
{{#allParams}}
3740
{{#required}}
3841
// verify required parameter '{{paramName}}' is set
@@ -52,12 +55,22 @@ module {{package}} {
5255
headerParams['{{baseName}}'] = {{paramName}};
5356

5457
{{/headerParams}}
58+
{{#hasFormParams}}
59+
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';
60+
61+
{{/hasFormParams}}
62+
{{#formParams}}
63+
formParams['{{baseName}}'] = {{paramName}};
64+
65+
{{/formParams}}
5566
var httpRequestParams: any = {
5667
method: '{{httpMethod}}',
5768
url: path,
58-
json: true,
69+
json: {{#hasFormParams}}false{{/hasFormParams}}{{^hasFormParams}}true{{/hasFormParams}},
5970
{{#bodyParam}}data: {{paramName}},
6071
{{/bodyParam}}
72+
{{#hasFormParams}}data: this.$httpParamSerializer(formParams),
73+
{{/hasFormParams}}
6174
params: queryParameters,
6275
headers: headerParams
6376
};

samples/client/petstore/typescript-angular/API/Client/PetApi.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module API.Client {
88
export class PetApi {
99
private basePath = 'http://petstore.swagger.io/v2';
1010

11-
static $inject: string[] = ['$http'];
11+
static $inject: string[] = ['$http', '$httpParamSerializer'];
1212

13-
constructor(private $http: ng.IHttpService, basePath?: string) {
13+
constructor(private $http: ng.IHttpService, basePath?: string, private $httpParamSerializer?: (any) => any) {
1414
if (basePath) {
1515
this.basePath = basePath;
1616
}
@@ -21,13 +21,13 @@ module API.Client {
2121

2222
var queryParameters: any = {};
2323
var headerParams: any = {};
24-
2524
var httpRequestParams: any = {
2625
method: 'PUT',
2726
url: path,
2827
json: true,
2928
data: body,
3029

30+
3131
params: queryParameters,
3232
headers: headerParams
3333
};
@@ -48,13 +48,13 @@ module API.Client {
4848

4949
var queryParameters: any = {};
5050
var headerParams: any = {};
51-
5251
var httpRequestParams: any = {
5352
method: 'POST',
5453
url: path,
5554
json: true,
5655
data: body,
5756

57+
5858
params: queryParameters,
5959
headers: headerParams
6060
};
@@ -75,7 +75,6 @@ module API.Client {
7575

7676
var queryParameters: any = {};
7777
var headerParams: any = {};
78-
7978
if (status !== undefined) {
8079
queryParameters['status'] = status;
8180
}
@@ -85,6 +84,7 @@ module API.Client {
8584
url: path,
8685
json: true,
8786

87+
8888
params: queryParameters,
8989
headers: headerParams
9090
};
@@ -105,7 +105,6 @@ module API.Client {
105105

106106
var queryParameters: any = {};
107107
var headerParams: any = {};
108-
109108
if (tags !== undefined) {
110109
queryParameters['tags'] = tags;
111110
}
@@ -115,6 +114,7 @@ module API.Client {
115114
url: path,
116115
json: true,
117116

117+
118118
params: queryParameters,
119119
headers: headerParams
120120
};
@@ -137,7 +137,6 @@ module API.Client {
137137

138138
var queryParameters: any = {};
139139
var headerParams: any = {};
140-
141140
// verify required parameter 'petId' is set
142141
if (!petId) {
143142
throw new Error('Missing required parameter petId when calling getPetById');
@@ -148,6 +147,7 @@ module API.Client {
148147
url: path,
149148
json: true,
150149

150+
151151
params: queryParameters,
152152
headers: headerParams
153153
};
@@ -170,16 +170,25 @@ module API.Client {
170170

171171
var queryParameters: any = {};
172172
var headerParams: any = {};
173+
var formParams: any = {};
173174

174175
// verify required parameter 'petId' is set
175176
if (!petId) {
176177
throw new Error('Missing required parameter petId when calling updatePetWithForm');
177178
}
178179

180+
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';
181+
182+
formParams['name'] = name;
183+
184+
formParams['status'] = status;
185+
179186
var httpRequestParams: any = {
180187
method: 'POST',
181188
url: path,
182-
json: true,
189+
json: false,
190+
191+
data: this.$httpParamSerializer(formParams),
183192

184193
params: queryParameters,
185194
headers: headerParams
@@ -203,19 +212,19 @@ module API.Client {
203212

204213
var queryParameters: any = {};
205214
var headerParams: any = {};
206-
207215
// verify required parameter 'petId' is set
208216
if (!petId) {
209217
throw new Error('Missing required parameter petId when calling deletePet');
210218
}
211219

212-
headerParams['apiKey'] = apiKey;
220+
headerParams['api_key'] = apiKey;
213221

214222
var httpRequestParams: any = {
215223
method: 'DELETE',
216224
url: path,
217225
json: true,
218226

227+
219228
params: queryParameters,
220229
headers: headerParams
221230
};
@@ -238,16 +247,25 @@ module API.Client {
238247

239248
var queryParameters: any = {};
240249
var headerParams: any = {};
250+
var formParams: any = {};
241251

242252
// verify required parameter 'petId' is set
243253
if (!petId) {
244254
throw new Error('Missing required parameter petId when calling uploadFile');
245255
}
246256

257+
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';
258+
259+
formParams['additionalMetadata'] = additionalMetadata;
260+
261+
formParams['file'] = file;
262+
247263
var httpRequestParams: any = {
248264
method: 'POST',
249265
url: path,
250-
json: true,
266+
json: false,
267+
268+
data: this.$httpParamSerializer(formParams),
251269

252270
params: queryParameters,
253271
headers: headerParams

samples/client/petstore/typescript-angular/API/Client/StoreApi.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module API.Client {
88
export class StoreApi {
99
private basePath = 'http://petstore.swagger.io/v2';
1010

11-
static $inject: string[] = ['$http'];
11+
static $inject: string[] = ['$http', '$httpParamSerializer'];
1212

13-
constructor(private $http: ng.IHttpService, basePath?: string) {
13+
constructor(private $http: ng.IHttpService, basePath?: string, private $httpParamSerializer?: (any) => any) {
1414
if (basePath) {
1515
this.basePath = basePath;
1616
}
@@ -21,12 +21,12 @@ module API.Client {
2121

2222
var queryParameters: any = {};
2323
var headerParams: any = {};
24-
2524
var httpRequestParams: any = {
2625
method: 'GET',
2726
url: path,
2827
json: true,
2928

29+
3030
params: queryParameters,
3131
headers: headerParams
3232
};
@@ -47,13 +47,13 @@ module API.Client {
4747

4848
var queryParameters: any = {};
4949
var headerParams: any = {};
50-
5150
var httpRequestParams: any = {
5251
method: 'POST',
5352
url: path,
5453
json: true,
5554
data: body,
5655

56+
5757
params: queryParameters,
5858
headers: headerParams
5959
};
@@ -76,7 +76,6 @@ module API.Client {
7676

7777
var queryParameters: any = {};
7878
var headerParams: any = {};
79-
8079
// verify required parameter 'orderId' is set
8180
if (!orderId) {
8281
throw new Error('Missing required parameter orderId when calling getOrderById');
@@ -87,6 +86,7 @@ module API.Client {
8786
url: path,
8887
json: true,
8988

89+
9090
params: queryParameters,
9191
headers: headerParams
9292
};
@@ -109,7 +109,6 @@ module API.Client {
109109

110110
var queryParameters: any = {};
111111
var headerParams: any = {};
112-
113112
// verify required parameter 'orderId' is set
114113
if (!orderId) {
115114
throw new Error('Missing required parameter orderId when calling deleteOrder');
@@ -120,6 +119,7 @@ module API.Client {
120119
url: path,
121120
json: true,
122121

122+
123123
params: queryParameters,
124124
headers: headerParams
125125
};

0 commit comments

Comments
 (0)