Skip to content

Commit 84bb749

Browse files
taxponwing328
authored andcommitted
[typescript-angular] add customized encoder to use '+' char in query parameter #6306 (#6334)
1 parent 30fb132 commit 84bb749

File tree

21 files changed

+190
-91
lines changed

21 files changed

+190
-91
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void processOpts() {
8484
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
8585
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
8686
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
87+
supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts"));
8788
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
8889
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
8990

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { {{classname}} } from '../{{filename}}';
1515

1616
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
1717
import { Configuration } from '../configuration';
18+
import { CustomQueryEncoderHelper } from '../encoder';
1819
{{#withInterfaces}}
1920
import { {{classname}}Interface } from './{{classname}}Interface';
2021
{{/withInterfaces}}
@@ -118,7 +119,7 @@ export class {{classname}} {
118119
const path = this.basePath + '{{{path}}}'{{#pathParams}}
119120
.replace('${' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
120121

121-
let queryParameters = new URLSearchParams();
122+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
122123
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
123124

124125
{{#allParams}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { QueryEncoder } from "@angular/http";
2+
3+
/**
4+
* CustomQueryEncoderHelper
5+
* Fix plus sign (+) not encoding, so sent as blank space
6+
* See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
7+
*/
8+
export class CustomQueryEncoderHelper extends QueryEncoder {
9+
encodeKey(k: string): string {
10+
k = super.encodeKey(k);
11+
return k.replace(/\+/gi, '%2B');
12+
}
13+
encodeValue(v: string): string {
14+
v = super.encodeValue(v);
15+
return v.replace(/\+/gi, '%2B');
16+
}
17+
}

samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Pet } from '../model/pet';
2525

2626
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2727
import { Configuration } from '../configuration';
28+
import { CustomQueryEncoderHelper } from '../encoder';
2829

2930

3031
@Injectable()
@@ -215,7 +216,7 @@ export class PetService {
215216
public addPetWithHttpInfo(body: Pet, extraHttpRequestParams?: any): Observable<Response> {
216217
const path = this.basePath + '/pet';
217218

218-
let queryParameters = new URLSearchParams();
219+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
219220
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
220221

221222
// verify required parameter 'body' is not null or undefined
@@ -266,7 +267,7 @@ export class PetService {
266267
const path = this.basePath + '/pet/${petId}'
267268
.replace('${' + 'petId' + '}', String(petId));
268269

269-
let queryParameters = new URLSearchParams();
270+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
270271
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
271272

272273
// verify required parameter 'petId' is not null or undefined
@@ -316,7 +317,7 @@ export class PetService {
316317
public findPetsByStatusWithHttpInfo(status: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
317318
const path = this.basePath + '/pet/findByStatus';
318319

319-
let queryParameters = new URLSearchParams();
320+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
320321
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
321322

322323
// verify required parameter 'status' is not null or undefined
@@ -366,7 +367,7 @@ export class PetService {
366367
public findPetsByTagsWithHttpInfo(tags: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
367368
const path = this.basePath + '/pet/findByTags';
368369

369-
let queryParameters = new URLSearchParams();
370+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
370371
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
371372

372373
// verify required parameter 'tags' is not null or undefined
@@ -417,7 +418,7 @@ export class PetService {
417418
const path = this.basePath + '/pet/${petId}'
418419
.replace('${' + 'petId' + '}', String(petId));
419420

420-
let queryParameters = new URLSearchParams();
421+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
421422
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
422423

423424
// verify required parameter 'petId' is not null or undefined
@@ -459,7 +460,7 @@ export class PetService {
459460
public updatePetWithHttpInfo(body: Pet, extraHttpRequestParams?: any): Observable<Response> {
460461
const path = this.basePath + '/pet';
461462

462-
let queryParameters = new URLSearchParams();
463+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
463464
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
464465

465466
// verify required parameter 'body' is not null or undefined
@@ -511,7 +512,7 @@ export class PetService {
511512
const path = this.basePath + '/pet/${petId}'
512513
.replace('${' + 'petId' + '}', String(petId));
513514

514-
let queryParameters = new URLSearchParams();
515+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
515516
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
516517

517518
// verify required parameter 'petId' is not null or undefined
@@ -578,7 +579,7 @@ export class PetService {
578579
const path = this.basePath + '/pet/${petId}/uploadImage'
579580
.replace('${' + 'petId' + '}', String(petId));
580581

581-
let queryParameters = new URLSearchParams();
582+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
582583
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
583584

584585
// verify required parameter 'petId' is not null or undefined

samples/client/petstore/typescript-angular-v2/default/api/store.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Order } from '../model/order';
2424

2525
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2626
import { Configuration } from '../configuration';
27+
import { CustomQueryEncoderHelper } from '../encoder';
2728

2829

2930
@Injectable()
@@ -145,7 +146,7 @@ export class StoreService {
145146
const path = this.basePath + '/store/order/${orderId}'
146147
.replace('${' + 'orderId' + '}', String(orderId));
147148

148-
let queryParameters = new URLSearchParams();
149+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
149150
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
150151

151152
// verify required parameter 'orderId' is not null or undefined
@@ -181,7 +182,7 @@ export class StoreService {
181182
public getInventoryWithHttpInfo(extraHttpRequestParams?: any): Observable<Response> {
182183
const path = this.basePath + '/store/inventory';
183184

184-
let queryParameters = new URLSearchParams();
185+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
185186
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
186187

187188

@@ -219,7 +220,7 @@ export class StoreService {
219220
const path = this.basePath + '/store/order/${orderId}'
220221
.replace('${' + 'orderId' + '}', String(orderId));
221222

222-
let queryParameters = new URLSearchParams();
223+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
223224
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
224225

225226
// verify required parameter 'orderId' is not null or undefined
@@ -256,7 +257,7 @@ export class StoreService {
256257
public placeOrderWithHttpInfo(body: Order, extraHttpRequestParams?: any): Observable<Response> {
257258
const path = this.basePath + '/store/order';
258259

259-
let queryParameters = new URLSearchParams();
260+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
260261
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
261262

262263
// verify required parameter 'body' is not null or undefined

samples/client/petstore/typescript-angular-v2/default/api/user.service.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { User } from '../model/user';
2424

2525
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2626
import { Configuration } from '../configuration';
27+
import { CustomQueryEncoderHelper } from '../encoder';
2728

2829

2930
@Injectable()
@@ -210,7 +211,7 @@ export class UserService {
210211
public createUserWithHttpInfo(body: User, extraHttpRequestParams?: any): Observable<Response> {
211212
const path = this.basePath + '/user';
212213

213-
let queryParameters = new URLSearchParams();
214+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
214215
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
215216

216217
// verify required parameter 'body' is not null or undefined
@@ -250,7 +251,7 @@ export class UserService {
250251
public createUsersWithArrayInputWithHttpInfo(body: Array<User>, extraHttpRequestParams?: any): Observable<Response> {
251252
const path = this.basePath + '/user/createWithArray';
252253

253-
let queryParameters = new URLSearchParams();
254+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
254255
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
255256

256257
// verify required parameter 'body' is not null or undefined
@@ -290,7 +291,7 @@ export class UserService {
290291
public createUsersWithListInputWithHttpInfo(body: Array<User>, extraHttpRequestParams?: any): Observable<Response> {
291292
const path = this.basePath + '/user/createWithList';
292293

293-
let queryParameters = new URLSearchParams();
294+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
294295
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
295296

296297
// verify required parameter 'body' is not null or undefined
@@ -331,7 +332,7 @@ export class UserService {
331332
const path = this.basePath + '/user/${username}'
332333
.replace('${' + 'username' + '}', String(username));
333334

334-
let queryParameters = new URLSearchParams();
335+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
335336
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
336337

337338
// verify required parameter 'username' is not null or undefined
@@ -369,7 +370,7 @@ export class UserService {
369370
const path = this.basePath + '/user/${username}'
370371
.replace('${' + 'username' + '}', String(username));
371372

372-
let queryParameters = new URLSearchParams();
373+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
373374
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
374375

375376
// verify required parameter 'username' is not null or undefined
@@ -407,7 +408,7 @@ export class UserService {
407408
public loginUserWithHttpInfo(username: string, password: string, extraHttpRequestParams?: any): Observable<Response> {
408409
const path = this.basePath + '/user/login';
409410

410-
let queryParameters = new URLSearchParams();
411+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
411412
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
412413

413414
// verify required parameter 'username' is not null or undefined
@@ -455,7 +456,7 @@ export class UserService {
455456
public logoutUserWithHttpInfo(extraHttpRequestParams?: any): Observable<Response> {
456457
const path = this.basePath + '/user/logout';
457458

458-
let queryParameters = new URLSearchParams();
459+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
459460
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
460461

461462

@@ -490,7 +491,7 @@ export class UserService {
490491
const path = this.basePath + '/user/${username}'
491492
.replace('${' + 'username' + '}', String(username));
492493

493-
let queryParameters = new URLSearchParams();
494+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
494495
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
495496

496497
// verify required parameter 'username' is not null or undefined
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { QueryEncoder } from "@angular/http";
2+
3+
/**
4+
* CustomQueryEncoderHelper
5+
* Fix plus sign (+) not encoding, so sent as blank space
6+
* See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
7+
*/
8+
export class CustomQueryEncoderHelper extends QueryEncoder {
9+
encodeKey(k: string): string {
10+
k = super.encodeKey(k);
11+
return k.replace(/\+/gi, '%2B');
12+
}
13+
encodeValue(v: string): string {
14+
v = super.encodeValue(v);
15+
return v.replace(/\+/gi, '%2B');
16+
}
17+
}

samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Pet } from '../model/pet';
2525

2626
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2727
import { Configuration } from '../configuration';
28+
import { CustomQueryEncoderHelper } from '../encoder';
2829

2930

3031
@Injectable()
@@ -215,7 +216,7 @@ export class PetService {
215216
public addPetWithHttpInfo(body: Pet, extraHttpRequestParams?: any): Observable<Response> {
216217
const path = this.basePath + '/pet';
217218

218-
let queryParameters = new URLSearchParams();
219+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
219220
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
220221

221222
// verify required parameter 'body' is not null or undefined
@@ -266,7 +267,7 @@ export class PetService {
266267
const path = this.basePath + '/pet/${petId}'
267268
.replace('${' + 'petId' + '}', String(petId));
268269

269-
let queryParameters = new URLSearchParams();
270+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
270271
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
271272

272273
// verify required parameter 'petId' is not null or undefined
@@ -316,7 +317,7 @@ export class PetService {
316317
public findPetsByStatusWithHttpInfo(status: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
317318
const path = this.basePath + '/pet/findByStatus';
318319

319-
let queryParameters = new URLSearchParams();
320+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
320321
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
321322

322323
// verify required parameter 'status' is not null or undefined
@@ -366,7 +367,7 @@ export class PetService {
366367
public findPetsByTagsWithHttpInfo(tags: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
367368
const path = this.basePath + '/pet/findByTags';
368369

369-
let queryParameters = new URLSearchParams();
370+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
370371
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
371372

372373
// verify required parameter 'tags' is not null or undefined
@@ -417,7 +418,7 @@ export class PetService {
417418
const path = this.basePath + '/pet/${petId}'
418419
.replace('${' + 'petId' + '}', String(petId));
419420

420-
let queryParameters = new URLSearchParams();
421+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
421422
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
422423

423424
// verify required parameter 'petId' is not null or undefined
@@ -459,7 +460,7 @@ export class PetService {
459460
public updatePetWithHttpInfo(body: Pet, extraHttpRequestParams?: any): Observable<Response> {
460461
const path = this.basePath + '/pet';
461462

462-
let queryParameters = new URLSearchParams();
463+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
463464
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
464465

465466
// verify required parameter 'body' is not null or undefined
@@ -511,7 +512,7 @@ export class PetService {
511512
const path = this.basePath + '/pet/${petId}'
512513
.replace('${' + 'petId' + '}', String(petId));
513514

514-
let queryParameters = new URLSearchParams();
515+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
515516
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
516517

517518
// verify required parameter 'petId' is not null or undefined
@@ -578,7 +579,7 @@ export class PetService {
578579
const path = this.basePath + '/pet/${petId}/uploadImage'
579580
.replace('${' + 'petId' + '}', String(petId));
580581

581-
let queryParameters = new URLSearchParams();
582+
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
582583
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
583584

584585
// verify required parameter 'petId' is not null or undefined

0 commit comments

Comments
 (0)