diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java index ad2c318f770..00c98ed43ff 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java @@ -41,7 +41,9 @@ public TypeScriptAngular2ClientCodegen() { embeddedTemplateDir = templateDir = "typescript-angular2"; modelTemplateFiles.put("model.mustache", ".ts"); apiTemplateFiles.put("api.service.mustache", ".ts"); + languageSpecificPrimitives.add("Blob"); typeMapping.put("Date","Date"); + typeMapping.put("file","Blob"); apiPackage = "api"; modelPackage = "model"; @@ -116,6 +118,11 @@ private String getIndexDirectory() { return indexPackage.replace('.', File.separatorChar); } + @Override + public boolean isDataTypeFile(final String dataType) { + return dataType != null && dataType.equals("Blob"); + } + @Override public String getTypeDeclaration(Property p) { Property inner; @@ -127,7 +134,9 @@ public String getTypeDeclaration(Property p) { MapProperty mp = (MapProperty)p; inner = mp.getAdditionalProperties(); return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }"; - } else if(p instanceof FileProperty || p instanceof ObjectProperty) { + } else if(p instanceof FileProperty) { + return "Blob"; + } else if(p instanceof ObjectProperty) { return "any"; } else { return super.getTypeDeclaration(p); diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.service.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.service.mustache index 15c2ef0707a..2b3ce3713b5 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.service.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.service.mustache @@ -54,6 +54,20 @@ export class {{classname}} { return objA; } + /** + * @param consumes string[] mime-types + * @return true: consumes contains 'multipart/form-data', false: otherwise + */ + private canConsumeForm(consumes: string[]): boolean { + const form = 'multipart/form-data'; + for (let consume of consumes) { + if (form === consume) { + return true; + } + } + return false; + } + {{#operation}} /** * {{summary}} @@ -66,7 +80,12 @@ export class {{classname}} { if (response.status === 204) { return undefined; } else { +{{^isResponseFile}} return response.json(); +{{/isResponseFile}} +{{#isResponseFile}} + return response.blob(); +{{/isResponseFile}} } }); } @@ -84,10 +103,7 @@ export class {{classname}} { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 -{{#hasFormParams}} - let formParams = new URLSearchParams(); -{{/hasFormParams}} {{#allParams}} {{#required}} // verify required parameter '{{paramName}}' is not null or undefined @@ -106,12 +122,24 @@ export class {{classname}} { headers.set('{{baseName}}', String({{paramName}})); {{/headerParams}} +{{#hasFormParams}} // to determine the Content-Type header let consumes: string[] = [ {{#consumes}} '{{{mediaType}}}'{{#hasMore}}, {{/hasMore}} {{/consumes}} ]; + let canConsumeForm = this.canConsumeForm(consumes); + let useForm = false; +{{#formParams}} +{{#isFile}} + useForm = canConsumeForm; +{{/isFile}} +{{/formParams}} + let formParams = new (useForm ? FormData : URLSearchParams as any)() as { + set(param: string, value: any): void; + }; +{{/hasFormParams}} // to determine the Accept header let produces: string[] = [ @@ -155,17 +183,13 @@ export class {{classname}} { {{/isOAuth}} {{/authMethods}} -{{#hasFormParams}} - headers.set('Content-Type', 'application/x-www-form-urlencoded'); -{{/hasFormParams}} - {{#bodyParam}} headers.set('Content-Type', 'application/json'); {{/bodyParam}} {{#formParams}} if ({{paramName}} !== undefined) { - formParams.set('{{baseName}}', {{paramName}}); + formParams.set('{{baseName}}', {{paramName}}); } {{/formParams}} @@ -176,8 +200,11 @@ export class {{classname}} { body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612 {{/bodyParam}} {{#hasFormParams}} - body: formParams.toString(), + body: formParams, {{/hasFormParams}} +{{#isResponseFile}} + responseType: ResponseContentType.Blob, +{{/isResponseFile}} search: queryParameters }); diff --git a/samples/client/petstore/typescript-angular2/default/api/pet.service.ts b/samples/client/petstore/typescript-angular2/default/api/pet.service.ts index 4487d08f6ec..14eb4b9e794 100644 --- a/samples/client/petstore/typescript-angular2/default/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular2/default/api/pet.service.ts @@ -57,6 +57,20 @@ export class PetService { return objA; } + /** + * @param consumes string[] mime-types + * @return true: consumes contains 'multipart/form-data', false: otherwise + */ + private canConsumeForm(consumes: string[]): boolean { + const form = 'multipart/form-data'; + for (let consume of consumes) { + if (form === consume) { + return true; + } + } + return false; + } + /** * Add a new pet to the store * @@ -179,7 +193,7 @@ export class PetService { * @param additionalMetadata Additional data to pass to server * @param file file to upload */ - public uploadFile(petId: number, additionalMetadata?: string, file?: any, extraHttpRequestParams?: any): Observable<{}> { + public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, extraHttpRequestParams?: any): Observable<{}> { return this.uploadFileWithHttpInfo(petId, additionalMetadata, file, extraHttpRequestParams) .map((response: Response) => { if (response.status === 204) { @@ -203,11 +217,7 @@ export class PetService { 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/xml' - ]; + // to determine the Accept header let produces: string[] = [ @@ -225,7 +235,6 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); @@ -255,6 +264,7 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { throw new Error('Required parameter petId was null or undefined when calling deletePet.'); @@ -262,9 +272,6 @@ export class PetService { headers.set('api_key', String(apiKey)); - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -284,7 +291,6 @@ export class PetService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -309,14 +315,12 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + if (status !== undefined) { queryParameters.set('status', status); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -336,7 +340,6 @@ export class PetService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -361,14 +364,12 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + if (tags !== undefined) { queryParameters.set('tags', tags); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -388,7 +389,6 @@ export class PetService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -413,15 +413,13 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { throw new Error('Required parameter petId was null or undefined when calling getPetById.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -443,10 +441,14 @@ export class PetService { { headers.set('api_key', this.configuration.apiKey); } + // authentication (api_key) required + if (this.configuration.apiKey) + { + headers.set('api_key', this.configuration.apiKey); + } - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -473,11 +475,7 @@ export class PetService { 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/xml' - ]; + // to determine the Accept header let produces: string[] = [ @@ -495,7 +493,6 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); @@ -526,7 +523,6 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - let formParams = new URLSearchParams(); // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { @@ -538,6 +534,11 @@ export class PetService { let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + let canConsumeForm = this.canConsumeForm(consumes); + let useForm = false; + let formParams = new (useForm ? FormData : URLSearchParams as any)() as { + set(param: string, value: any): void; + }; // to determine the Accept header let produces: string[] = [ @@ -555,20 +556,18 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/x-www-form-urlencoded'); - if (name !== undefined) { - formParams.set('name', name); + formParams.set('name', name); } if (status !== undefined) { - formParams.set('status', status); + formParams.set('status', status); } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, headers: headers, - body: formParams.toString(), + body: formParams, search: queryParameters }); @@ -587,12 +586,11 @@ export class PetService { * @param additionalMetadata Additional data to pass to server * @param file file to upload */ - public uploadFileWithHttpInfo(petId: number, additionalMetadata?: string, file?: any, extraHttpRequestParams?: any): Observable { + public uploadFileWithHttpInfo(petId: number, additionalMetadata?: string, file?: Blob, extraHttpRequestParams?: any): Observable { const path = this.basePath + `/pet/${petId}/uploadImage`; let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - let formParams = new URLSearchParams(); // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { @@ -604,6 +602,12 @@ export class PetService { let consumes: string[] = [ 'multipart/form-data' ]; + let canConsumeForm = this.canConsumeForm(consumes); + let useForm = false; + useForm = canConsumeForm; + let formParams = new (useForm ? FormData : URLSearchParams as any)() as { + set(param: string, value: any): void; + }; // to determine the Accept header let produces: string[] = [ @@ -621,20 +625,18 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/x-www-form-urlencoded'); - if (additionalMetadata !== undefined) { - formParams.set('additionalMetadata', additionalMetadata); + formParams.set('additionalMetadata', additionalMetadata); } if (file !== undefined) { - formParams.set('file', file); + formParams.set('file', file); } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, headers: headers, - body: formParams.toString(), + body: formParams, search: queryParameters }); diff --git a/samples/client/petstore/typescript-angular2/default/api/store.service.ts b/samples/client/petstore/typescript-angular2/default/api/store.service.ts index 6f275fee1fc..912ec0517df 100644 --- a/samples/client/petstore/typescript-angular2/default/api/store.service.ts +++ b/samples/client/petstore/typescript-angular2/default/api/store.service.ts @@ -57,6 +57,20 @@ export class StoreService { return objA; } + /** + * @param consumes string[] mime-types + * @return true: consumes contains 'multipart/form-data', false: otherwise + */ + private canConsumeForm(consumes: string[]): boolean { + const form = 'multipart/form-data'; + for (let consume of consumes) { + if (form === consume) { + return true; + } + } + return false; + } + /** * Delete purchase order by ID * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -131,15 +145,13 @@ export class StoreService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'orderId' is not null or undefined if (orderId === null || orderId === undefined) { throw new Error('Required parameter orderId was null or undefined when calling deleteOrder.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -150,7 +162,6 @@ export class StoreService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -176,9 +187,7 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -194,7 +203,6 @@ export class StoreService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -219,15 +227,13 @@ export class StoreService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'orderId' is not null or undefined if (orderId === null || orderId === undefined) { throw new Error('Required parameter orderId was null or undefined when calling getOrderById.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -238,7 +244,6 @@ export class StoreService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -265,9 +270,7 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -276,7 +279,6 @@ export class StoreService { ]; - headers.set('Content-Type', 'application/json'); diff --git a/samples/client/petstore/typescript-angular2/default/api/user.service.ts b/samples/client/petstore/typescript-angular2/default/api/user.service.ts index fbbc5a724c1..e95be165e12 100644 --- a/samples/client/petstore/typescript-angular2/default/api/user.service.ts +++ b/samples/client/petstore/typescript-angular2/default/api/user.service.ts @@ -57,6 +57,20 @@ export class UserService { return objA; } + /** + * @param consumes string[] mime-types + * @return true: consumes contains 'multipart/form-data', false: otherwise + */ + private canConsumeForm(consumes: string[]): boolean { + const form = 'multipart/form-data'; + for (let consume of consumes) { + if (form === consume) { + return true; + } + } + return false; + } + /** * Create user * This can only be done by the logged in user. @@ -199,9 +213,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -210,7 +222,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json'); @@ -241,9 +252,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -252,7 +261,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json'); @@ -283,9 +291,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -294,7 +300,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json'); @@ -323,15 +328,13 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'username' is not null or undefined if (username === null || username === undefined) { throw new Error('Required parameter username was null or undefined when calling deleteUser.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -342,7 +345,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -367,15 +369,13 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'username' is not null or undefined if (username === null || username === undefined) { throw new Error('Required parameter username was null or undefined when calling getUserByName.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -386,7 +386,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -412,6 +411,7 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + if (username !== undefined) { queryParameters.set('username', username); } @@ -420,9 +420,6 @@ export class UserService { } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -433,7 +430,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -459,9 +455,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -472,7 +466,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -498,15 +491,13 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'username' is not null or undefined if (username === null || username === undefined) { throw new Error('Required parameter username was null or undefined when calling updateUser.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -515,7 +506,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json'); diff --git a/samples/client/petstore/typescript-angular2/npm/README.md b/samples/client/petstore/typescript-angular2/npm/README.md index 8d39b84f704..8a9b25d452a 100644 --- a/samples/client/petstore/typescript-angular2/npm/README.md +++ b/samples/client/petstore/typescript-angular2/npm/README.md @@ -41,4 +41,4 @@ import { BASE_PATH } from './path-to-swagger-gen-service/index'; bootstrap(AppComponent, [ { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, ]); -``` \ No newline at end of file +``` diff --git a/samples/client/petstore/typescript-angular2/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular2/npm/api/pet.service.ts index 4487d08f6ec..14eb4b9e794 100644 --- a/samples/client/petstore/typescript-angular2/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular2/npm/api/pet.service.ts @@ -57,6 +57,20 @@ export class PetService { return objA; } + /** + * @param consumes string[] mime-types + * @return true: consumes contains 'multipart/form-data', false: otherwise + */ + private canConsumeForm(consumes: string[]): boolean { + const form = 'multipart/form-data'; + for (let consume of consumes) { + if (form === consume) { + return true; + } + } + return false; + } + /** * Add a new pet to the store * @@ -179,7 +193,7 @@ export class PetService { * @param additionalMetadata Additional data to pass to server * @param file file to upload */ - public uploadFile(petId: number, additionalMetadata?: string, file?: any, extraHttpRequestParams?: any): Observable<{}> { + public uploadFile(petId: number, additionalMetadata?: string, file?: Blob, extraHttpRequestParams?: any): Observable<{}> { return this.uploadFileWithHttpInfo(petId, additionalMetadata, file, extraHttpRequestParams) .map((response: Response) => { if (response.status === 204) { @@ -203,11 +217,7 @@ export class PetService { 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/xml' - ]; + // to determine the Accept header let produces: string[] = [ @@ -225,7 +235,6 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); @@ -255,6 +264,7 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { throw new Error('Required parameter petId was null or undefined when calling deletePet.'); @@ -262,9 +272,6 @@ export class PetService { headers.set('api_key', String(apiKey)); - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -284,7 +291,6 @@ export class PetService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -309,14 +315,12 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + if (status !== undefined) { queryParameters.set('status', status); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -336,7 +340,6 @@ export class PetService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -361,14 +364,12 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + if (tags !== undefined) { queryParameters.set('tags', tags); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -388,7 +389,6 @@ export class PetService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -413,15 +413,13 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { throw new Error('Required parameter petId was null or undefined when calling getPetById.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -443,10 +441,14 @@ export class PetService { { headers.set('api_key', this.configuration.apiKey); } + // authentication (api_key) required + if (this.configuration.apiKey) + { + headers.set('api_key', this.configuration.apiKey); + } - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -473,11 +475,7 @@ export class PetService { 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/xml' - ]; + // to determine the Accept header let produces: string[] = [ @@ -495,7 +493,6 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); @@ -526,7 +523,6 @@ export class PetService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - let formParams = new URLSearchParams(); // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { @@ -538,6 +534,11 @@ export class PetService { let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + let canConsumeForm = this.canConsumeForm(consumes); + let useForm = false; + let formParams = new (useForm ? FormData : URLSearchParams as any)() as { + set(param: string, value: any): void; + }; // to determine the Accept header let produces: string[] = [ @@ -555,20 +556,18 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/x-www-form-urlencoded'); - if (name !== undefined) { - formParams.set('name', name); + formParams.set('name', name); } if (status !== undefined) { - formParams.set('status', status); + formParams.set('status', status); } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, headers: headers, - body: formParams.toString(), + body: formParams, search: queryParameters }); @@ -587,12 +586,11 @@ export class PetService { * @param additionalMetadata Additional data to pass to server * @param file file to upload */ - public uploadFileWithHttpInfo(petId: number, additionalMetadata?: string, file?: any, extraHttpRequestParams?: any): Observable { + public uploadFileWithHttpInfo(petId: number, additionalMetadata?: string, file?: Blob, extraHttpRequestParams?: any): Observable { const path = this.basePath + `/pet/${petId}/uploadImage`; let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - let formParams = new URLSearchParams(); // verify required parameter 'petId' is not null or undefined if (petId === null || petId === undefined) { @@ -604,6 +602,12 @@ export class PetService { let consumes: string[] = [ 'multipart/form-data' ]; + let canConsumeForm = this.canConsumeForm(consumes); + let useForm = false; + useForm = canConsumeForm; + let formParams = new (useForm ? FormData : URLSearchParams as any)() as { + set(param: string, value: any): void; + }; // to determine the Accept header let produces: string[] = [ @@ -621,20 +625,18 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/x-www-form-urlencoded'); - if (additionalMetadata !== undefined) { - formParams.set('additionalMetadata', additionalMetadata); + formParams.set('additionalMetadata', additionalMetadata); } if (file !== undefined) { - formParams.set('file', file); + formParams.set('file', file); } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, headers: headers, - body: formParams.toString(), + body: formParams, search: queryParameters }); diff --git a/samples/client/petstore/typescript-angular2/npm/api/store.service.ts b/samples/client/petstore/typescript-angular2/npm/api/store.service.ts index 6f275fee1fc..912ec0517df 100644 --- a/samples/client/petstore/typescript-angular2/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular2/npm/api/store.service.ts @@ -57,6 +57,20 @@ export class StoreService { return objA; } + /** + * @param consumes string[] mime-types + * @return true: consumes contains 'multipart/form-data', false: otherwise + */ + private canConsumeForm(consumes: string[]): boolean { + const form = 'multipart/form-data'; + for (let consume of consumes) { + if (form === consume) { + return true; + } + } + return false; + } + /** * Delete purchase order by ID * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -131,15 +145,13 @@ export class StoreService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'orderId' is not null or undefined if (orderId === null || orderId === undefined) { throw new Error('Required parameter orderId was null or undefined when calling deleteOrder.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -150,7 +162,6 @@ export class StoreService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -176,9 +187,7 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -194,7 +203,6 @@ export class StoreService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -219,15 +227,13 @@ export class StoreService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'orderId' is not null or undefined if (orderId === null || orderId === undefined) { throw new Error('Required parameter orderId was null or undefined when calling getOrderById.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -238,7 +244,6 @@ export class StoreService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -265,9 +270,7 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -276,7 +279,6 @@ export class StoreService { ]; - headers.set('Content-Type', 'application/json'); diff --git a/samples/client/petstore/typescript-angular2/npm/api/user.service.ts b/samples/client/petstore/typescript-angular2/npm/api/user.service.ts index fbbc5a724c1..e95be165e12 100644 --- a/samples/client/petstore/typescript-angular2/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular2/npm/api/user.service.ts @@ -57,6 +57,20 @@ export class UserService { return objA; } + /** + * @param consumes string[] mime-types + * @return true: consumes contains 'multipart/form-data', false: otherwise + */ + private canConsumeForm(consumes: string[]): boolean { + const form = 'multipart/form-data'; + for (let consume of consumes) { + if (form === consume) { + return true; + } + } + return false; + } + /** * Create user * This can only be done by the logged in user. @@ -199,9 +213,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -210,7 +222,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json'); @@ -241,9 +252,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -252,7 +261,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json'); @@ -283,9 +291,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -294,7 +300,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json'); @@ -323,15 +328,13 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'username' is not null or undefined if (username === null || username === undefined) { throw new Error('Required parameter username was null or undefined when calling deleteUser.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -342,7 +345,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -367,15 +369,13 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'username' is not null or undefined if (username === null || username === undefined) { throw new Error('Required parameter username was null or undefined when calling getUserByName.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -386,7 +386,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -412,6 +411,7 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + if (username !== undefined) { queryParameters.set('username', username); } @@ -420,9 +420,6 @@ export class UserService { } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -433,7 +430,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -459,9 +455,7 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - // to determine the Content-Type header - let consumes: string[] = [ - ]; + // to determine the Accept header let produces: string[] = [ @@ -472,7 +466,6 @@ export class UserService { - let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -498,15 +491,13 @@ export class UserService { let queryParameters = new URLSearchParams(); let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // verify required parameter 'username' is not null or undefined if (username === null || username === undefined) { throw new Error('Required parameter username was null or undefined when calling updateUser.'); } - // to determine the Content-Type header - let consumes: string[] = [ - ]; // to determine the Accept header let produces: string[] = [ @@ -515,7 +506,6 @@ export class UserService { ]; - headers.set('Content-Type', 'application/json');