Skip to content

Commit

Permalink
Correctly encode plus sign in parameters
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
Luis Fernando Planella Gonzalez committed Dec 20, 2017
1 parent 592ef46 commit 5b0a9e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 33 additions & 1 deletion templates/baseService.mustache
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
/* tslint:disable */
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpParameterCodec, HttpParams } from '@angular/common/http';
import { ApiConfiguration } from './api-configuration';

/**
* Custom parameter codec to correctly handle the plus sign in parameter
* values. See https://github.com/angular/angular/issues/18261
*/
class ParameterCodec implements HttpParameterCodec {
encodeKey(key: string): string {
return encodeURIComponent(key);
}

encodeValue(value: string): string {
return encodeURIComponent(value);
}

decodeKey(key: string): string {
return decodeURIComponent(key);
}

decodeValue(value: string): string {
return decodeURIComponent(value);
}
}
const PARAMETER_CODEC = new ParameterCodec();

/**
* Base class for API services
*/
Expand All @@ -28,4 +51,13 @@ export class BaseService {
set rootUrl(rootUrl: string) {
this._rootUrl = rootUrl;
}

/**
* Creates a new `HttpParams` with the correct codec
*/
protected newParams(): HttpParams {
return new HttpParams({
encoder: PARAMETER_CODEC
});
}
}
2 changes: 1 addition & 1 deletion templates/operationResponse.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}}{{^paramIsLast}},
{{/paramIsLast}}{{/operationParameters}}{{/operationParamsClass
}}): Observable<HttpResponse<{{operationResultType}}>> {
let __params = new HttpParams();
let __params = this.newParams();
let __headers = new HttpHeaders();
let __body: any = null;
{{#operationParameters}}{{>parameter}}
Expand Down

0 comments on commit 5b0a9e0

Please sign in to comment.