Skip to content

Commit d36e3cd

Browse files
authored
Merge pull request #9786 from michalzubkowicz/fix/9785
#9785
2 parents f1a021e + fbb8c24 commit d36e3cd

File tree

5 files changed

+116
-128
lines changed

5 files changed

+116
-128
lines changed

modules/swagger-codegen/src/main/resources/typescript-inversify/api.service.mustache

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{{>licenseInfo}}
22
/* tslint:disable:no-unused-variable member-ordering */
33

4-
import { Observable } from "rxjs/Observable";
4+
import { Observable } from 'rxjs/Observable';
55
import 'rxjs/add/operator/map';
66
import 'rxjs/add/operator/toPromise';
7-
import IHttpClient from "../IHttpClient";
8-
import { inject, injectable } from "inversify";
9-
import { IAPIConfiguration } from "../IAPIConfiguration";
10-
import { Headers } from "../Headers";
11-
import HttpResponse from "../HttpResponse";
7+
import IHttpClient from '../IHttpClient';
8+
import { inject, injectable } from 'inversify';
9+
import { IAPIConfiguration } from '../IAPIConfiguration';
10+
import { Headers } from '../Headers';
11+
import HttpResponse from '../HttpResponse';
1212

1313
{{#imports}}
1414
import { {{classname}} } from '../{{filename}}';
@@ -34,13 +34,10 @@ export class {{classname}} implements {{classname}}Interface {
3434
{{^withInterfaces}}
3535
export class {{classname}} {
3636
{{/withInterfaces}}
37-
private basePath: string = '{{{basePath}}}';
37+
@inject('IAPIConfiguration') private APIConfiguration: IAPIConfiguration;
38+
@inject('IApiHttpClient') private httpClient: IHttpClient;
39+
3840

39-
constructor(@inject("IApiHttpClient") private httpClient: IHttpClient,
40-
@inject("IAPIConfiguration") private APIConfiguration: IAPIConfiguration ) {
41-
if(this.APIConfiguration.basePath)
42-
this.basePath = this.APIConfiguration.basePath;
43-
}
4441
{{#operation}}
4542

4643
/**
@@ -68,21 +65,21 @@ export class {{classname}} {
6865
if ({{paramName}}) {
6966
{{#isCollectionFormatMulti}}
7067
{{paramName}}.forEach((element) => {
71-
queryParameters.push("{{paramName}}="+encodeURIComponent(String({{paramName}})));
68+
queryParameters.push('{{paramName}}='+encodeURIComponent(String({{paramName}})));
7269
})
7370
{{/isCollectionFormatMulti}}
7471
{{^isCollectionFormatMulti}}
75-
queryParameters.push("{{paramName}}="+encodeURIComponent({{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])));
72+
queryParameters.push('{{paramName}}='+encodeURIComponent({{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])));
7673
{{/isCollectionFormatMulti}}
7774
}
7875
{{/isListContainer}}
7976
{{^isListContainer}}
8077
if ({{paramName}} !== undefined) {
8178
{{#isDateTime}}
82-
queryParameters.push("{{paramName}}="+encodeURIComponent(<any>{{paramName}}.toISOString()));
79+
queryParameters.push('{{paramName}}='+encodeURIComponent(<any>{{paramName}}.toISOString()));
8380
{{/isDateTime}}
8481
{{^isDateTime}}
85-
queryParameters.push("{{paramName}}="+encodeURIComponent(String({{paramName}})));
82+
queryParameters.push('{{paramName}}='+encodeURIComponent(String({{paramName}})));
8683
{{/isDateTime}}
8784
}
8885
{{/isListContainer}}
@@ -106,13 +103,13 @@ export class {{classname}} {
106103
// authentication ({{name}}) required
107104
{{#isApiKey}}
108105
{{#isKeyInHeader}}
109-
if (this.APIConfiguration.apiKeys["{{keyParamName}}"]) {
110-
headers['{{keyParamName}}'] = this.APIConfiguration.apiKeys["{{keyParamName}}"];
106+
if (this.APIConfiguration.apiKeys['{{keyParamName}}']) {
107+
headers['{{keyParamName}}'] = this.APIConfiguration.apiKeys['{{keyParamName}}'];
111108
}
112109
{{/isKeyInHeader}}
113110
{{#isKeyInQuery}}
114-
if (this.APIConfiguration.apiKeys["{{keyParamName}}"]) {
115-
queryParameters.push("{{paramName}}="+encodeURIComponent(String(this.APIConfiguration.apiKeys["{{keyParamName}}"])));
111+
if (this.APIConfiguration.apiKeys['{{keyParamName}}']) {
112+
queryParameters.push('{{paramName}}='+encodeURIComponent(String(this.APIConfiguration.apiKeys['{{keyParamName}}'])));
116113
}
117114
{{/isKeyInQuery}}
118115
{{/isApiKey}}
@@ -169,9 +166,9 @@ export class {{classname}} {
169166
{{/formParams}}
170167

171168
{{/hasFormParams}}
172-
const response: Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>> = this.httpClient.{{httpMethod}}(`${this.basePath}{{{path}}}{{#hasQueryParams}}?${queryParameters.join('&')}{{/hasQueryParams}}`{{#bodyParam}}, {{paramName}} {{/bodyParam}}{{#hasFormParams}}, body{{/hasFormParams}}, headers);
173-
if (observe == 'body') {
174-
return response.map(httpResponse => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response)){{#usePromise}}.toPromise(){{/usePromise}};
169+
const response: Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>> = this.httpClient.{{httpMethod}}(`${this.APIConfiguration.basePath}{{{path}}}{{#hasQueryParams}}?${queryParameters.join('&')}{{/hasQueryParams}}`{{#bodyParam}}, {{paramName}}{{/bodyParam}} as any{{#hasFormParams}}, body{{/hasFormParams}}, headers);
170+
if (observe === 'body') {
171+
return response.map(httpResponse => httpResponse.response){{#usePromise}}.toPromise(){{/usePromise}};
175172
}
176173
return response{{#usePromise}}.toPromise(){{/usePromise}};
177174
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.0-SNAPSHOT
1+
2.4.10-SNAPSHOT

samples/client/petstore/typescript-inversify/api/pet.service.ts

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*/
1212
/* tslint:disable:no-unused-variable member-ordering */
1313

14-
import { Observable } from "rxjs/Observable";
14+
import { Observable } from 'rxjs/Observable';
1515
import 'rxjs/add/operator/map';
1616
import 'rxjs/add/operator/toPromise';
17-
import IHttpClient from "../IHttpClient";
18-
import { inject, injectable } from "inversify";
19-
import { IAPIConfiguration } from "../IAPIConfiguration";
20-
import { Headers } from "../Headers";
21-
import HttpResponse from "../HttpResponse";
17+
import IHttpClient from '../IHttpClient';
18+
import { inject, injectable } from 'inversify';
19+
import { IAPIConfiguration } from '../IAPIConfiguration';
20+
import { Headers } from '../Headers';
21+
import HttpResponse from '../HttpResponse';
2222

2323
import { ApiResponse } from '../model/apiResponse';
2424
import { Pet } from '../model/pet';
@@ -29,13 +29,10 @@ import { COLLECTION_FORMATS } from '../variables';
2929

3030
@injectable()
3131
export class PetService {
32-
private basePath: string = 'http://petstore.swagger.io/v2';
32+
@inject('IAPIConfiguration') private APIConfiguration: IAPIConfiguration;
33+
@inject('IApiHttpClient') private httpClient: IHttpClient;
34+
3335

34-
constructor(@inject("IApiHttpClient") private httpClient: IHttpClient,
35-
@inject("IAPIConfiguration") private APIConfiguration: IAPIConfiguration ) {
36-
if(this.APIConfiguration.basePath)
37-
this.basePath = this.APIConfiguration.basePath;
38-
}
3936

4037
/**
4138
* Add a new pet to the store
@@ -60,9 +57,9 @@ export class PetService {
6057
headers['Accept'] = 'application/xml';
6158
headers['Content-Type'] = 'application/json';
6259

63-
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/pet`, body , headers);
64-
if (observe == 'body') {
65-
return response.map(httpResponse => <any>(httpResponse.response));
60+
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.APIConfiguration.basePath}/pet`, body as any, headers);
61+
if (observe === 'body') {
62+
return response.map(httpResponse => httpResponse.response);
6663
}
6764
return response;
6865
}
@@ -95,9 +92,9 @@ export class PetService {
9592
}
9693
headers['Accept'] = 'application/xml';
9794

98-
const response: Observable<HttpResponse<any>> = this.httpClient.delete(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, headers);
99-
if (observe == 'body') {
100-
return response.map(httpResponse => <any>(httpResponse.response));
95+
const response: Observable<HttpResponse<any>> = this.httpClient.delete(`${this.APIConfiguration.basePath}/pet/${encodeURIComponent(String(petId))}` as any, headers);
96+
if (observe === 'body') {
97+
return response.map(httpResponse => httpResponse.response);
10198
}
10299
return response;
103100
}
@@ -118,7 +115,7 @@ export class PetService {
118115

119116
let queryParameters: string[] = [];
120117
if (status) {
121-
queryParameters.push("status="+encodeURIComponent(status.join(COLLECTION_FORMATS['csv'])));
118+
queryParameters.push('status='+encodeURIComponent(status.join(COLLECTION_FORMATS['csv'])));
122119
}
123120

124121
// authentication (petstore_auth) required
@@ -130,9 +127,9 @@ export class PetService {
130127
}
131128
headers['Accept'] = 'application/xml';
132129

133-
const response: Observable<HttpResponse<Array<Pet>>> = this.httpClient.get(`${this.basePath}/pet/findByStatus?${queryParameters.join('&')}`, headers);
134-
if (observe == 'body') {
135-
return response.map(httpResponse => <Array<Pet>>(httpResponse.response));
130+
const response: Observable<HttpResponse<Array<Pet>>> = this.httpClient.get(`${this.APIConfiguration.basePath}/pet/findByStatus?${queryParameters.join('&')}` as any, headers);
131+
if (observe === 'body') {
132+
return response.map(httpResponse => httpResponse.response);
136133
}
137134
return response;
138135
}
@@ -153,7 +150,7 @@ export class PetService {
153150

154151
let queryParameters: string[] = [];
155152
if (tags) {
156-
queryParameters.push("tags="+encodeURIComponent(tags.join(COLLECTION_FORMATS['csv'])));
153+
queryParameters.push('tags='+encodeURIComponent(tags.join(COLLECTION_FORMATS['csv'])));
157154
}
158155

159156
// authentication (petstore_auth) required
@@ -165,9 +162,9 @@ export class PetService {
165162
}
166163
headers['Accept'] = 'application/xml';
167164

168-
const response: Observable<HttpResponse<Array<Pet>>> = this.httpClient.get(`${this.basePath}/pet/findByTags?${queryParameters.join('&')}`, headers);
169-
if (observe == 'body') {
170-
return response.map(httpResponse => <Array<Pet>>(httpResponse.response));
165+
const response: Observable<HttpResponse<Array<Pet>>> = this.httpClient.get(`${this.APIConfiguration.basePath}/pet/findByTags?${queryParameters.join('&')}` as any, headers);
166+
if (observe === 'body') {
167+
return response.map(httpResponse => httpResponse.response);
171168
}
172169
return response;
173170
}
@@ -187,14 +184,14 @@ export class PetService {
187184
}
188185

189186
// authentication (api_key) required
190-
if (this.APIConfiguration.apiKeys["api_key"]) {
191-
headers['api_key'] = this.APIConfiguration.apiKeys["api_key"];
187+
if (this.APIConfiguration.apiKeys['api_key']) {
188+
headers['api_key'] = this.APIConfiguration.apiKeys['api_key'];
192189
}
193190
headers['Accept'] = 'application/xml';
194191

195-
const response: Observable<HttpResponse<Pet>> = this.httpClient.get(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, headers);
196-
if (observe == 'body') {
197-
return response.map(httpResponse => <Pet>(httpResponse.response));
192+
const response: Observable<HttpResponse<Pet>> = this.httpClient.get(`${this.APIConfiguration.basePath}/pet/${encodeURIComponent(String(petId))}` as any, headers);
193+
if (observe === 'body') {
194+
return response.map(httpResponse => httpResponse.response);
198195
}
199196
return response;
200197
}
@@ -223,9 +220,9 @@ export class PetService {
223220
headers['Accept'] = 'application/xml';
224221
headers['Content-Type'] = 'application/json';
225222

226-
const response: Observable<HttpResponse<any>> = this.httpClient.put(`${this.basePath}/pet`, body , headers);
227-
if (observe == 'body') {
228-
return response.map(httpResponse => <any>(httpResponse.response));
223+
const response: Observable<HttpResponse<any>> = this.httpClient.put(`${this.APIConfiguration.basePath}/pet`, body as any, headers);
224+
if (observe === 'body') {
225+
return response.map(httpResponse => httpResponse.response);
229226
}
230227
return response;
231228
}
@@ -264,9 +261,9 @@ export class PetService {
264261
formData.append('status', <any>status);
265262
}
266263

267-
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, body, headers);
268-
if (observe == 'body') {
269-
return response.map(httpResponse => <any>(httpResponse.response));
264+
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.APIConfiguration.basePath}/pet/${encodeURIComponent(String(petId))}` as any, body, headers);
265+
if (observe === 'body') {
266+
return response.map(httpResponse => httpResponse.response);
270267
}
271268
return response;
272269
}
@@ -305,9 +302,9 @@ export class PetService {
305302
formData.append('file', <any>file);
306303
}
307304

308-
const response: Observable<HttpResponse<ApiResponse>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}/uploadImage`, body, headers);
309-
if (observe == 'body') {
310-
return response.map(httpResponse => <ApiResponse>(httpResponse.response));
305+
const response: Observable<HttpResponse<ApiResponse>> = this.httpClient.post(`${this.APIConfiguration.basePath}/pet/${encodeURIComponent(String(petId))}/uploadImage` as any, body, headers);
306+
if (observe === 'body') {
307+
return response.map(httpResponse => httpResponse.response);
311308
}
312309
return response;
313310
}

samples/client/petstore/typescript-inversify/api/store.service.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*/
1212
/* tslint:disable:no-unused-variable member-ordering */
1313

14-
import { Observable } from "rxjs/Observable";
14+
import { Observable } from 'rxjs/Observable';
1515
import 'rxjs/add/operator/map';
1616
import 'rxjs/add/operator/toPromise';
17-
import IHttpClient from "../IHttpClient";
18-
import { inject, injectable } from "inversify";
19-
import { IAPIConfiguration } from "../IAPIConfiguration";
20-
import { Headers } from "../Headers";
21-
import HttpResponse from "../HttpResponse";
17+
import IHttpClient from '../IHttpClient';
18+
import { inject, injectable } from 'inversify';
19+
import { IAPIConfiguration } from '../IAPIConfiguration';
20+
import { Headers } from '../Headers';
21+
import HttpResponse from '../HttpResponse';
2222

2323
import { Order } from '../model/order';
2424

@@ -28,13 +28,10 @@ import { COLLECTION_FORMATS } from '../variables';
2828

2929
@injectable()
3030
export class StoreService {
31-
private basePath: string = 'http://petstore.swagger.io/v2';
31+
@inject('IAPIConfiguration') private APIConfiguration: IAPIConfiguration;
32+
@inject('IApiHttpClient') private httpClient: IHttpClient;
33+
3234

33-
constructor(@inject("IApiHttpClient") private httpClient: IHttpClient,
34-
@inject("IAPIConfiguration") private APIConfiguration: IAPIConfiguration ) {
35-
if(this.APIConfiguration.basePath)
36-
this.basePath = this.APIConfiguration.basePath;
37-
}
3835

3936
/**
4037
* Delete purchase order by ID
@@ -51,9 +48,9 @@ export class StoreService {
5148

5249
headers['Accept'] = 'application/xml';
5350

54-
const response: Observable<HttpResponse<any>> = this.httpClient.delete(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, headers);
55-
if (observe == 'body') {
56-
return response.map(httpResponse => <any>(httpResponse.response));
51+
const response: Observable<HttpResponse<any>> = this.httpClient.delete(`${this.APIConfiguration.basePath}/store/order/${encodeURIComponent(String(orderId))}` as any, headers);
52+
if (observe === 'body') {
53+
return response.map(httpResponse => httpResponse.response);
5754
}
5855
return response;
5956
}
@@ -68,14 +65,14 @@ export class StoreService {
6865
public getInventory(observe?: 'response', headers?: Headers): Observable<HttpResponse<{ [key: string]: number; }>>;
6966
public getInventory(observe: any = 'body', headers: Headers = {}): Observable<any> {
7067
// authentication (api_key) required
71-
if (this.APIConfiguration.apiKeys["api_key"]) {
72-
headers['api_key'] = this.APIConfiguration.apiKeys["api_key"];
68+
if (this.APIConfiguration.apiKeys['api_key']) {
69+
headers['api_key'] = this.APIConfiguration.apiKeys['api_key'];
7370
}
7471
headers['Accept'] = 'application/json';
7572

76-
const response: Observable<HttpResponse<{ [key: string]: number; }>> = this.httpClient.get(`${this.basePath}/store/inventory`, headers);
77-
if (observe == 'body') {
78-
return response.map(httpResponse => <{ [key: string]: number; }>(httpResponse.response));
73+
const response: Observable<HttpResponse<{ [key: string]: number; }>> = this.httpClient.get(`${this.APIConfiguration.basePath}/store/inventory` as any, headers);
74+
if (observe === 'body') {
75+
return response.map(httpResponse => httpResponse.response);
7976
}
8077
return response;
8178
}
@@ -96,9 +93,9 @@ export class StoreService {
9693

9794
headers['Accept'] = 'application/xml';
9895

99-
const response: Observable<HttpResponse<Order>> = this.httpClient.get(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, headers);
100-
if (observe == 'body') {
101-
return response.map(httpResponse => <Order>(httpResponse.response));
96+
const response: Observable<HttpResponse<Order>> = this.httpClient.get(`${this.APIConfiguration.basePath}/store/order/${encodeURIComponent(String(orderId))}` as any, headers);
97+
if (observe === 'body') {
98+
return response.map(httpResponse => httpResponse.response);
10299
}
103100
return response;
104101
}
@@ -120,9 +117,9 @@ export class StoreService {
120117
headers['Accept'] = 'application/xml';
121118
headers['Content-Type'] = 'application/json';
122119

123-
const response: Observable<HttpResponse<Order>> = this.httpClient.post(`${this.basePath}/store/order`, body , headers);
124-
if (observe == 'body') {
125-
return response.map(httpResponse => <Order>(httpResponse.response));
120+
const response: Observable<HttpResponse<Order>> = this.httpClient.post(`${this.APIConfiguration.basePath}/store/order`, body as any, headers);
121+
if (observe === 'body') {
122+
return response.map(httpResponse => httpResponse.response);
126123
}
127124
return response;
128125
}

0 commit comments

Comments
 (0)