Skip to content

Commit 1cd8141

Browse files
committed
implement form parameter support using Angular $httpParamSerializer
1 parent 98ff2d2 commit 1cd8141

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
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, private $httpParamSerializer: (any) => any, basePath?: string) {
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
};

0 commit comments

Comments
 (0)