Skip to content

Commit

Permalink
[typescript-angular] add customized encoder to use '+' char in query …
Browse files Browse the repository at this point in the history
…parameter swagger-api#6306
  • Loading branch information
taxpon committed Aug 31, 2017
1 parent 37f4823 commit a3e7f63
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts"));
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { {{classname}} } from '../{{filename}}';

import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { CustomQueryEncoderHelper } from '../encoder';
{{#withInterfaces}}
import { {{classname}}Interface } from './{{classname}}Interface';
{{/withInterfaces}}
Expand Down Expand Up @@ -118,7 +119,7 @@ export class {{classname}} {
const path = this.basePath + '{{{path}}}'{{#pathParams}}
.replace('${' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};

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

{{#allParams}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* CustomQueryEncoderHelper
* Fix plus sign (+) not encoding, so sent as blank space
* See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
*/
import { QueryEncoder } from "@angular/http";

export class CustomQueryEncoderHelper extends QueryEncoder {
encodeKey(k: string): string {
k = super.encodeKey(k);
return k.replace(/\+/gi, '%2B');
}
encodeValue(v: string): string {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3-SNAPSHOT
2.3.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { FakeService } from './api/fake.service';
providers: [ FakeService ]
})
export class ApiModule {
public static forConfig(configuration: Configuration): ModuleWithProviders {
public static forConfig(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ {provide: Configuration, useValue: configuration}]
providers: [ {provide: Configuration, useFactory: configurationFactory}]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Do not edit the class manually.
*/

/* tslint:disable:no-unused-variable member-ordering */

import { Inject, Injectable, Optional } from '@angular/core';
import { Http, Headers, URLSearchParams } from '@angular/http';
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
Expand All @@ -21,12 +23,12 @@ import '../rxjs-operators';

import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';

/* tslint:disable:no-unused-variable member-ordering */
import { CustomQueryEncoderHelper } from '../encoder';


@Injectable()
export class FakeService {

protected basePath = 'https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r';
public defaultHeaders: Headers = new Headers();
public configuration: Configuration = new Configuration();
Expand Down Expand Up @@ -71,8 +73,8 @@ export class FakeService {
}

/**
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
*
* @summary To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param test code inject * ' " =end rn n r To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
*/
public testCodeInjectEndRnNR(test code inject * &#39; &quot; &#x3D;end rn n r?: string, extraHttpRequestParams?: any): Observable<{}> {
Expand All @@ -95,12 +97,12 @@ export class FakeService {
public testCodeInjectEndRnNRWithHttpInfo(test code inject * &#39; &quot; &#x3D;end rn n r?: string, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/fake';

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

// to determine the Content-Type header
let consumes: string[] = [
'application/json',
'application/json',
'*_/ =end -- '
];
let canConsumeForm = this.canConsumeForm(consumes);
Expand All @@ -111,7 +113,7 @@ export class FakeService {

// to determine the Accept header
let produces: string[] = [
'application/json',
'application/json',
'*_/ =end -- '
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ export interface ConfigurationParameters {
apiKeys?: {[ key: string ]: string};
username?: string;
password?: string;
accessToken?: string;
accessToken?: string | (() => string);
basePath?: string;
withCredentials?: boolean;
}

export class Configuration {
apiKeys: {[ key: string ]: string};
username: string;
password: string;
accessToken: string | (() => string);
basePath: string;
withCredentials: boolean;
apiKeys?: {[ key: string ]: string};
username?: string;
password?: string;
accessToken?: string | (() => string);
basePath?: string;
withCredentials?: boolean;

constructor(configurationParameters: ConfigurationParameters = {}) {
this.apiKeys = configurationParameters.apiKeys;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* CustomQueryEncoderHelper
* Fix plus sign (+) not encoding, so sent as blank space
* See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
*/
import { QueryEncoder } from "@angular/http";

export class CustomQueryEncoderHelper extends QueryEncoder {
encodeKey(k: string): string {
k = super.encodeKey(k);
return k.replace(/\+/gi, '%2B');
}
encodeValue(v: string): string {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { InjectionToken<string> } from '@angular/core';
import { InjectionToken } from '@angular/core';

export const BASE_PATH = new InjectionToken<string>('basePath');
export const COLLECTION_FORMATS = {
'csv': ',',
'tsv': ' ',
'ssv': ' ',
'pipes': '|'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Pet } from '../model/pet';

import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { CustomQueryEncoderHelper } from '../encoder';


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// verify required parameter 'petId' is not null or undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Order } from '../model/order';

import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { CustomQueryEncoderHelper } from '../encoder';


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

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

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

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


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

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

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

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

// verify required parameter 'body' is not null or undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { User } from '../model/user';

import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { CustomQueryEncoderHelper } from '../encoder';


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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

// verify required parameter 'username' is not null or undefined
Expand Down
Loading

0 comments on commit a3e7f63

Please sign in to comment.