diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache index 6f085fe3411..8700083b63d 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache @@ -143,7 +143,10 @@ export class {{classname}} { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } {{/isOAuth}} {{/authMethods}} diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/configuration.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/configuration.mustache index 94989933b63..dd8d4be9728 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/configuration.mustache @@ -2,5 +2,5 @@ export class Configuration { apiKey: string; username: string; password: string; - accessToken: string; + accessToken: string | () => string; } \ No newline at end of file diff --git a/samples/client/petstore-security-test/typescript-angular2/api/FakeApi.ts b/samples/client/petstore-security-test/typescript-angular2/api/FakeApi.ts index 88b8e7a6562..1cbd4d97048 100644 --- a/samples/client/petstore-security-test/typescript-angular2/api/FakeApi.ts +++ b/samples/client/petstore-security-test/typescript-angular2/api/FakeApi.ts @@ -1,72 +1,67 @@ /** - * Swagger Petstore *_/ ' \" =end \\r\\n \\n \\r - * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end + * Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end -- * - * OpenAPI spec version: 1.0.0 *_/ ' \" =end \\r\\n \\n \\r - * Contact: apiteam@swagger.io *_/ ' \" =end \\r\\n \\n \\r + * OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r + * Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r * * NOTE: This class is auto generated by the swagger code generator program. * https://github.com/swagger-api/swagger-codegen.git * Do not edit the class manually. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ -import {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from '@angular/http'; -import {Injectable, Optional} from '@angular/core'; -import {Observable} from 'rxjs/Observable'; -import * as models from '../model/models'; -import 'rxjs/Rx'; +import { Inject, Injectable, Optional } from '@angular/core'; +import { Http, Headers, URLSearchParams } from '@angular/http'; +import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http'; +import { Response, ResponseContentType } from '@angular/http'; + +import { Observable } from 'rxjs/Observable'; +import 'rxjs/add/operator/map'; + +import * as models from '../model/models'; +import { BASE_PATH } from '../variables'; +import { Configuration } from '../configuration'; /* tslint:disable:no-unused-variable member-ordering */ -'use strict'; @Injectable() export class FakeApi { - protected basePath = 'https://petstore.swagger.io *_/ ' \" =end \\r\\n \\n \\r/v2 *_/ ' \" =end \\r\\n \\n \\r'; - public defaultHeaders : Headers = new Headers(); + 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(); - constructor(protected http: Http, @Optional() basePath: string) { + constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { if (basePath) { this.basePath = basePath; } + if (configuration) { + this.configuration = configuration; + } } /** - * 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 + * Extends object by coping non-existing properties. + * @param objA object to be extended + * @param objB source object */ - public testCodeInjectEndRnNR (test code inject * ' " =end rn n r?: string, extraHttpRequestParams?: any ) : Observable<{}> { - const path = this.basePath + '/fake'; + private extendObj(objA: T1, objB: T2) { + for(let key in objB){ + if(objB.hasOwnProperty(key)){ + (objA as any)[key] = (objB as any)[key]; + } + } + return objA; + } - let queryParameters = new URLSearchParams(); - let headerParams = this.defaultHeaders; - let formParams = new URLSearchParams(); - - headerParams.set('Content-Type', 'application/x-www-form-urlencoded'); - - formParams['test code inject */ ' " =end \r\n \n \r'] = test code inject * ' " =end rn n r; - - let requestOptions: RequestOptionsArgs = { - method: 'PUT', - headers: headerParams, - search: queryParameters - }; - requestOptions.body = formParams.toString(); - - return this.http.request(path, requestOptions) + /** + * 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 * ' " =end rn n r?: string, extraHttpRequestParams?: any): Observable<{}> { + return this.testCodeInjectEndRnNRWithHttpInfo(test code inject * ' " =end rn n r, extraHttpRequestParams) .map((response: Response) => { if (response.status === 204) { return undefined; @@ -76,4 +71,54 @@ export class FakeApi { }); } + + /** + * 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 testCodeInjectEndRnNRWithHttpInfo(test code inject * ' " =end rn n r?: string, extraHttpRequestParams?: any): Observable { + const path = this.basePath + `/fake`; + + let queryParameters = new URLSearchParams(); + let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + let formParams = new URLSearchParams(); + + + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + '*_/ =end -- ' + ]; + + // to determine the Accept header + let produces: string[] = [ + 'application/json', + '*_/ =end -- ' + ]; + + + headers.set('Content-Type', 'application/x-www-form-urlencoded'); + + + if (test code inject * ' " =end rn n r !== undefined) { + formParams.set('test code inject */ ' " =end -- \r\n \n \r', test code inject * ' " =end rn n r); + } + + let requestOptions: RequestOptionsArgs = new RequestOptions({ + method: RequestMethod.Put, + headers: headers, + body: formParams.toString(), + search: queryParameters + }); + + // https://github.com/swagger-api/swagger-codegen/issues/4037 + if (extraHttpRequestParams) { + requestOptions = this.extendObj(requestOptions, extraHttpRequestParams); + } + + return this.http.request(path, requestOptions); + } + } diff --git a/samples/client/petstore-security-test/typescript-angular2/configuration.ts b/samples/client/petstore-security-test/typescript-angular2/configuration.ts new file mode 100644 index 00000000000..dd8d4be9728 --- /dev/null +++ b/samples/client/petstore-security-test/typescript-angular2/configuration.ts @@ -0,0 +1,6 @@ +export class Configuration { + apiKey: string; + username: string; + password: string; + accessToken: string | () => string; +} \ No newline at end of file diff --git a/samples/client/petstore-security-test/typescript-angular2/index.ts b/samples/client/petstore-security-test/typescript-angular2/index.ts index 557365516ad..d097c728017 100644 --- a/samples/client/petstore-security-test/typescript-angular2/index.ts +++ b/samples/client/petstore-security-test/typescript-angular2/index.ts @@ -1,2 +1,4 @@ export * from './api/api'; -export * from './model/models'; \ No newline at end of file +export * from './model/models'; +export * from './variables'; +export * from './configuration'; \ No newline at end of file diff --git a/samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts b/samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts index 23c982f6888..5e9e114be05 100644 --- a/samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts +++ b/samples/client/petstore-security-test/typescript-angular2/model/ModelReturn.ts @@ -1,38 +1,24 @@ /** - * Swagger Petstore *_/ ' \" =end \\r\\n \\n \\r - * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end + * Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end -- * - * OpenAPI spec version: 1.0.0 *_/ ' \" =end \\r\\n \\n \\r - * Contact: apiteam@swagger.io *_/ ' \" =end \\r\\n \\n \\r + * OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r + * Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r * * NOTE: This class is auto generated by the swagger code generator program. * https://github.com/swagger-api/swagger-codegen.git * Do not edit the class manually. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. */ -'use strict'; import * as models from './models'; /** - * Model for testing reserved words *_/ ' \" =end \\r\\n \\n \\r + * Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r */ export interface ModelReturn { - - /** - * property description *_/ ' \" =end \\r\\n \\n \\r + * property description *_/ ' \" =end -- \\r\\n \\n \\r */ return?: number; + } diff --git a/samples/client/petstore-security-test/typescript-angular2/variables.ts b/samples/client/petstore-security-test/typescript-angular2/variables.ts new file mode 100644 index 00000000000..27b987e9b23 --- /dev/null +++ b/samples/client/petstore-security-test/typescript-angular2/variables.ts @@ -0,0 +1,3 @@ +import { OpaqueToken } from '@angular/core'; + +export const BASE_PATH = new OpaqueToken('basePath'); \ No newline at end of file diff --git a/samples/client/petstore/typescript-angular2/default/api/PetApi.ts b/samples/client/petstore/typescript-angular2/default/api/PetApi.ts index 70ab123c5a5..5751b452369 100644 --- a/samples/client/petstore/typescript-angular2/default/api/PetApi.ts +++ b/samples/client/petstore/typescript-angular2/default/api/PetApi.ts @@ -217,7 +217,10 @@ export class PetApi { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } @@ -271,7 +274,10 @@ export class PetApi { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } @@ -320,7 +326,10 @@ export class PetApi { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } @@ -369,7 +378,10 @@ export class PetApi { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } @@ -415,16 +427,19 @@ export class PetApi { 'application/xml' ]; - // authentication (api_key) required - if (this.configuration.apiKey) - { - headers.set('api_key', this.configuration.apiKey); - } // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); + } + // authentication (api_key) required + if (this.configuration.apiKey) + { + headers.set('api_key', this.configuration.apiKey); } @@ -472,7 +487,10 @@ export class PetApi { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } @@ -529,7 +547,10 @@ export class PetApi { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } headers.set('Content-Type', 'application/x-www-form-urlencoded'); @@ -592,7 +613,10 @@ export class PetApi { // oauth required if (this.configuration.accessToken) { - headers.set('Authorization', 'Bearer ' + this.configuration.accessToken); + let accessToken = typeof this.configuration.accessToken === 'function' + ? this.configuration.accessToken() + : this.configuration.accessToken; + headers.set('Authorization', 'Bearer ' + accessToken); } headers.set('Content-Type', 'application/x-www-form-urlencoded'); diff --git a/samples/client/petstore/typescript-angular2/default/configuration.ts b/samples/client/petstore/typescript-angular2/default/configuration.ts index 94989933b63..dd8d4be9728 100644 --- a/samples/client/petstore/typescript-angular2/default/configuration.ts +++ b/samples/client/petstore/typescript-angular2/default/configuration.ts @@ -2,5 +2,5 @@ export class Configuration { apiKey: string; username: string; password: string; - accessToken: string; + accessToken: string | () => string; } \ No newline at end of file