From a5586044cd9d6a5bc8595b49af02abf31664587d Mon Sep 17 00:00:00 2001 From: Meindert Deen Date: Sat, 4 Nov 2017 17:26:42 +0100 Subject: [PATCH] Implemented fix for #6006. Mime-type support (#6751) * Implemented fix for #6006. Mime-type support for accepts and content-type. * Review comments of @macjohnny incorporated * merge with latest and retest * merge with latest and retest. Now correctly generated with new Maven build --- .../typescript-angular/api.service.mustache | 35 ++- .../typescript-angular/configuration.mustache | 53 +++++ .../default/api/pet.service.ts | 122 +++++++++- .../default/api/store.service.ts | 60 ++++- .../default/api/user.service.ts | 129 +++++++++- .../default/configuration.ts | 53 +++++ .../npm/api/pet.service.ts | 122 +++++++++- .../npm/api/store.service.ts | 60 ++++- .../npm/api/user.service.ts | 129 +++++++++- .../npm/configuration.ts | 53 +++++ .../with-interfaces/api/pet.service.ts | 122 +++++++++- .../with-interfaces/api/store.service.ts | 60 ++++- .../with-interfaces/api/user.service.ts | 129 +++++++++- .../with-interfaces/configuration.ts | 53 +++++ .../npm/api/pet.service.ts | 194 +++++++++++---- .../npm/api/store.service.ts | 105 +++++++-- .../npm/api/user.service.ts | 223 +++++++++++++----- .../npm/configuration.ts | 53 +++++ .../npm/api/pet.service.ts | 122 +++++++++- .../npm/api/store.service.ts | 60 ++++- .../npm/api/user.service.ts | 129 +++++++++- .../npm/configuration.ts | 53 +++++ 22 files changed, 1930 insertions(+), 189 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache index 20be74cfa61e..ab1cb91677f0 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache @@ -73,7 +73,6 @@ export class {{classname}} { return false; } - {{^useHttpClient}} {{! not sure why we used to generate a second method here rather than inlining those few lines of code, but let's keep it for now for the sake of backwards compatiblity. }} @@ -199,19 +198,41 @@ export class {{classname}} { {{/isOAuth}} {{/authMethods}} -{{#bodyParam}} + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + {{#produces}} + '{{{mediaType}}}'{{#hasMore}},{{/hasMore}} + {{/produces}} + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { {{^useHttpClient}} - headers.set('Content-Type', 'application/json'); - + headers.set("Accept", httpHeaderAcceptSelected); {{/useHttpClient}} -{{/bodyParam}} -{{#hasFormParams}} +{{#useHttpClient}} + headers = headers.set("Accept", httpHeaderAcceptSelected); +{{/useHttpClient}} + } + // to determine the Content-Type header let consumes: string[] = [ {{#consumes}} '{{{mediaType}}}'{{#hasMore}},{{/hasMore}} {{/consumes}} ]; +{{#bodyParam}} + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { +{{^useHttpClient}} + headers.set('Content-Type', httpContentTypeSelected); +{{/useHttpClient}} +{{#useHttpClient}} + headers = headers.set("Content-Type", httpContentTypeSelected); +{{/useHttpClient}} + } +{{/bodyParam}} + +{{#hasFormParams}} const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -239,8 +260,6 @@ export class {{classname}} { {{/useHttpClient}} } - - {{#formParams}} {{#isListContainer}} if ({{paramName}}) { diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/configuration.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/configuration.mustache index 005c3a26df33..67b2b9903b37 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/configuration.mustache @@ -23,4 +23,57 @@ export class Configuration { this.basePath = configurationParameters.basePath; this.withCredentials = configurationParameters.withCredentials; } + + /** + * Select the correct content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} contentTypes - the array of content types that are available for selection + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderContentType (contentTypes: string[]): string { + if (contentTypes.length == 0) { + return undefined; + } + + let type = contentTypes.find(x => this.isJsonMime(x)); + if (type === undefined) { + return contentTypes[0]; + } + return type; + } + + /** + * Select the correct accept content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} accepts - the array of content types that are available for selection. + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderAccept(accepts: string[]): string { + if (accepts.length == 0) { + return undefined; + } + + let type = accepts.find(x => this.isJsonMime(x)); + if (type === undefined) { + return accepts[0]; + } + return type; + } + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + * application/vnd.company+json + * @param {string} mime - MIME (Multipurpose Internet Mail Extensions) + * @return {boolean} True if the given MIME is JSON, false otherwise. + */ + public isJsonMime(mime: string): boolean { + const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); + return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); + } } diff --git a/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts index c2899427ea6e..4435de468a24 100644 --- a/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts @@ -58,7 +58,6 @@ export class PetService { return false; } - /** * * @summary Add a new pet to the store @@ -213,7 +212,25 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -253,6 +270,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -291,6 +322,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -330,6 +375,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -361,6 +420,20 @@ export class PetService { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -394,7 +467,25 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, @@ -432,10 +523,21 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -451,8 +553,6 @@ export class PetService { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (name !== undefined) { formParams.append('name', name); } @@ -496,10 +596,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'multipart/form-data' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -518,8 +628,6 @@ export class PetService { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (additionalMetadata !== undefined) { formParams.append('additionalMetadata', additionalMetadata); } diff --git a/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts b/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts index 24cf96b4136b..3eb043313ff5 100644 --- a/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts @@ -57,7 +57,6 @@ export class StoreService { return false; } - /** * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @summary Delete purchase order by ID @@ -134,6 +133,20 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -160,6 +173,19 @@ export class StoreService { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -185,6 +211,20 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -210,7 +250,23 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, diff --git a/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts b/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts index 5bd7cdbac655..7fd15126b811 100644 --- a/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts @@ -57,7 +57,6 @@ export class UserService { return false; } - /** * This can only be done by the logged in user. * @summary Create user @@ -200,7 +199,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -228,7 +243,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -256,7 +287,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -284,6 +331,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -309,6 +370,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -346,6 +421,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -368,6 +457,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -397,7 +500,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, diff --git a/samples/client/petstore/typescript-angular-v2/default/configuration.ts b/samples/client/petstore/typescript-angular-v2/default/configuration.ts index 005c3a26df33..67b2b9903b37 100644 --- a/samples/client/petstore/typescript-angular-v2/default/configuration.ts +++ b/samples/client/petstore/typescript-angular-v2/default/configuration.ts @@ -23,4 +23,57 @@ export class Configuration { this.basePath = configurationParameters.basePath; this.withCredentials = configurationParameters.withCredentials; } + + /** + * Select the correct content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} contentTypes - the array of content types that are available for selection + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderContentType (contentTypes: string[]): string { + if (contentTypes.length == 0) { + return undefined; + } + + let type = contentTypes.find(x => this.isJsonMime(x)); + if (type === undefined) { + return contentTypes[0]; + } + return type; + } + + /** + * Select the correct accept content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} accepts - the array of content types that are available for selection. + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderAccept(accepts: string[]): string { + if (accepts.length == 0) { + return undefined; + } + + let type = accepts.find(x => this.isJsonMime(x)); + if (type === undefined) { + return accepts[0]; + } + return type; + } + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + * application/vnd.company+json + * @param {string} mime - MIME (Multipurpose Internet Mail Extensions) + * @return {boolean} True if the given MIME is JSON, false otherwise. + */ + public isJsonMime(mime: string): boolean { + const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); + return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); + } } diff --git a/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts index c2899427ea6e..4435de468a24 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts @@ -58,7 +58,6 @@ export class PetService { return false; } - /** * * @summary Add a new pet to the store @@ -213,7 +212,25 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -253,6 +270,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -291,6 +322,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -330,6 +375,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -361,6 +420,20 @@ export class PetService { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -394,7 +467,25 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, @@ -432,10 +523,21 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -451,8 +553,6 @@ export class PetService { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (name !== undefined) { formParams.append('name', name); } @@ -496,10 +596,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'multipart/form-data' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -518,8 +628,6 @@ export class PetService { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (additionalMetadata !== undefined) { formParams.append('additionalMetadata', additionalMetadata); } diff --git a/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts index 24cf96b4136b..3eb043313ff5 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts @@ -57,7 +57,6 @@ export class StoreService { return false; } - /** * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @summary Delete purchase order by ID @@ -134,6 +133,20 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -160,6 +173,19 @@ export class StoreService { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -185,6 +211,20 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -210,7 +250,23 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, diff --git a/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts index 5bd7cdbac655..7fd15126b811 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts @@ -57,7 +57,6 @@ export class UserService { return false; } - /** * This can only be done by the logged in user. * @summary Create user @@ -200,7 +199,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -228,7 +243,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -256,7 +287,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -284,6 +331,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -309,6 +370,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -346,6 +421,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -368,6 +457,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -397,7 +500,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, diff --git a/samples/client/petstore/typescript-angular-v2/npm/configuration.ts b/samples/client/petstore/typescript-angular-v2/npm/configuration.ts index 005c3a26df33..67b2b9903b37 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/configuration.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/configuration.ts @@ -23,4 +23,57 @@ export class Configuration { this.basePath = configurationParameters.basePath; this.withCredentials = configurationParameters.withCredentials; } + + /** + * Select the correct content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} contentTypes - the array of content types that are available for selection + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderContentType (contentTypes: string[]): string { + if (contentTypes.length == 0) { + return undefined; + } + + let type = contentTypes.find(x => this.isJsonMime(x)); + if (type === undefined) { + return contentTypes[0]; + } + return type; + } + + /** + * Select the correct accept content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} accepts - the array of content types that are available for selection. + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderAccept(accepts: string[]): string { + if (accepts.length == 0) { + return undefined; + } + + let type = accepts.find(x => this.isJsonMime(x)); + if (type === undefined) { + return accepts[0]; + } + return type; + } + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + * application/vnd.company+json + * @param {string} mime - MIME (Multipurpose Internet Mail Extensions) + * @return {boolean} True if the given MIME is JSON, false otherwise. + */ + public isJsonMime(mime: string): boolean { + const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); + return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); + } } diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts index 8d6285d29e19..8f606901d006 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts @@ -59,7 +59,6 @@ export class PetService implements PetServiceInterface { return false; } - /** * * @summary Add a new pet to the store @@ -214,7 +213,25 @@ export class PetService implements PetServiceInterface { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -254,6 +271,20 @@ export class PetService implements PetServiceInterface { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -292,6 +323,20 @@ export class PetService implements PetServiceInterface { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -331,6 +376,20 @@ export class PetService implements PetServiceInterface { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -362,6 +421,20 @@ export class PetService implements PetServiceInterface { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -395,7 +468,25 @@ export class PetService implements PetServiceInterface { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, @@ -433,10 +524,21 @@ export class PetService implements PetServiceInterface { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -452,8 +554,6 @@ export class PetService implements PetServiceInterface { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (name !== undefined) { formParams.append('name', name); } @@ -497,10 +597,20 @@ export class PetService implements PetServiceInterface { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'multipart/form-data' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -519,8 +629,6 @@ export class PetService implements PetServiceInterface { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (additionalMetadata !== undefined) { formParams.append('additionalMetadata', additionalMetadata); } diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts index af8cc47369bf..30d37ccf3813 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts @@ -58,7 +58,6 @@ export class StoreService implements StoreServiceInterface { return false; } - /** * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @summary Delete purchase order by ID @@ -135,6 +134,20 @@ export class StoreService implements StoreServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -161,6 +174,19 @@ export class StoreService implements StoreServiceInterface { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -186,6 +212,20 @@ export class StoreService implements StoreServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -211,7 +251,23 @@ export class StoreService implements StoreServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts index 54752a85503d..17b1d6311bd6 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts @@ -58,7 +58,6 @@ export class UserService implements UserServiceInterface { return false; } - /** * This can only be done by the logged in user. * @summary Create user @@ -201,7 +200,23 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -229,7 +244,23 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -257,7 +288,23 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -285,6 +332,20 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -310,6 +371,20 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -347,6 +422,20 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -369,6 +458,20 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -398,7 +501,23 @@ export class UserService implements UserServiceInterface { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/configuration.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/configuration.ts index 005c3a26df33..67b2b9903b37 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/configuration.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/configuration.ts @@ -23,4 +23,57 @@ export class Configuration { this.basePath = configurationParameters.basePath; this.withCredentials = configurationParameters.withCredentials; } + + /** + * Select the correct content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} contentTypes - the array of content types that are available for selection + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderContentType (contentTypes: string[]): string { + if (contentTypes.length == 0) { + return undefined; + } + + let type = contentTypes.find(x => this.isJsonMime(x)); + if (type === undefined) { + return contentTypes[0]; + } + return type; + } + + /** + * Select the correct accept content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} accepts - the array of content types that are available for selection. + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderAccept(accepts: string[]): string { + if (accepts.length == 0) { + return undefined; + } + + let type = accepts.find(x => this.isJsonMime(x)); + if (type === undefined) { + return accepts[0]; + } + return type; + } + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + * application/vnd.company+json + * @param {string} mime - MIME (Multipurpose Internet Mail Extensions) + * @return {boolean} True if the given MIME is JSON, false otherwise. + */ + public isJsonMime(mime: string): boolean { + const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); + return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); + } } diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts index 93d9553627c2..522395066a63 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts @@ -57,7 +57,6 @@ export class PetService { } - /** * Add a new pet to the store * @@ -78,13 +77,31 @@ export class PetService { headers = headers.set('Authorization', 'Bearer ' + accessToken); } - return this.httpClient.post(`${this.basePath}/pet`, - body, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers = headers.set("Content-Type", httpContentTypeSelected); + } + + return this.httpClient.post(`${this.basePath}/pet`, body, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -111,12 +128,25 @@ export class PetService { headers = headers.set('Authorization', 'Bearer ' + accessToken); } - return this.httpClient.delete(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.delete(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -144,13 +174,26 @@ export class PetService { headers = headers.set('Authorization', 'Bearer ' + accessToken); } - return this.httpClient.get(`${this.basePath}/pet/findByStatus`, - { - params: queryParameters, - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/pet/findByStatus`, + { + params: queryParameters, + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -178,13 +221,26 @@ export class PetService { headers = headers.set('Authorization', 'Bearer ' + accessToken); } - return this.httpClient.get(`${this.basePath}/pet/findByTags`, - { - params: queryParameters, - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/pet/findByTags`, + { + params: queryParameters, + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -204,12 +260,25 @@ export class PetService { headers = headers.set('api_key', this.configuration.apiKeys["api_key"]); } - return this.httpClient.get(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -232,13 +301,31 @@ export class PetService { headers = headers.set('Authorization', 'Bearer ' + accessToken); } - return this.httpClient.put(`${this.basePath}/pet`, - body, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers = headers.set("Content-Type", httpContentTypeSelected); + } + + return this.httpClient.put(`${this.basePath}/pet`, body, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -263,10 +350,21 @@ export class PetService { headers = headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -278,8 +376,6 @@ export class PetService { formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()}); } - - if (name !== undefined) { formParams = formParams.append('name', name) || formParams; } @@ -318,10 +414,20 @@ export class PetService { headers = headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'multipart/form-data' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -336,8 +442,6 @@ export class PetService { formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()}); } - - if (additionalMetadata !== undefined) { formParams = formParams.append('additionalMetadata', additionalMetadata) || formParams; } diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts index 7e05cddef8e8..066157b79fb3 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts @@ -56,7 +56,6 @@ export class StoreService { } - /** * Delete purchase order by ID * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -69,12 +68,25 @@ export class StoreService { let headers = this.defaultHeaders; - return this.httpClient.delete(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.delete(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -90,12 +102,24 @@ export class StoreService { headers = headers.set('api_key', this.configuration.apiKeys["api_key"]); } - return this.httpClient.get(`${this.basePath}/store/inventory`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/store/inventory`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -110,12 +134,25 @@ export class StoreService { let headers = this.defaultHeaders; - return this.httpClient.get(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -130,13 +167,29 @@ export class StoreService { let headers = this.defaultHeaders; - return this.httpClient.post(`${this.basePath}/store/order`, - body, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers = headers.set("Content-Type", httpContentTypeSelected); + } + + return this.httpClient.post(`${this.basePath}/store/order`, body, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } } diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts index f8146a76f454..55e54992b377 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts @@ -56,7 +56,6 @@ export class UserService { } - /** * Create user * This can only be done by the logged in user. @@ -69,13 +68,29 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.post(`${this.basePath}/user`, - body, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers = headers.set("Content-Type", httpContentTypeSelected); + } + + return this.httpClient.post(`${this.basePath}/user`, body, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -90,13 +105,29 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.post(`${this.basePath}/user/createWithArray`, - body, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers = headers.set("Content-Type", httpContentTypeSelected); + } + + return this.httpClient.post(`${this.basePath}/user/createWithArray`, body, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -111,13 +142,29 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.post(`${this.basePath}/user/createWithList`, - body, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers = headers.set("Content-Type", httpContentTypeSelected); + } + + return this.httpClient.post(`${this.basePath}/user/createWithList`, body, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -132,12 +179,25 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.delete(`${this.basePath}/user/${encodeURIComponent(String(username))}`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.delete(`${this.basePath}/user/${encodeURIComponent(String(username))}`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -152,12 +212,25 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.get(`${this.basePath}/user/${encodeURIComponent(String(username))}`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/user/${encodeURIComponent(String(username))}`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -184,13 +257,26 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.get(`${this.basePath}/user/login`, - { - params: queryParameters, - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/user/login`, + { + params: queryParameters, + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -201,12 +287,25 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.get(`${this.basePath}/user/logout`, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + + return this.httpClient.get(`${this.basePath}/user/logout`, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } /** @@ -225,13 +324,29 @@ export class UserService { let headers = this.defaultHeaders; - return this.httpClient.put(`${this.basePath}/user/${encodeURIComponent(String(username))}`, - body, - { - headers: headers, - withCredentials: this.configuration.withCredentials, - } - ); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers = headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers = headers.set("Content-Type", httpContentTypeSelected); + } + + return this.httpClient.put(`${this.basePath}/user/${encodeURIComponent(String(username))}`, body, + { + headers: headers, + withCredentials: this.configuration.withCredentials, + }); } } diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/configuration.ts b/samples/client/petstore/typescript-angular-v4.3/npm/configuration.ts index 005c3a26df33..67b2b9903b37 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/configuration.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/configuration.ts @@ -23,4 +23,57 @@ export class Configuration { this.basePath = configurationParameters.basePath; this.withCredentials = configurationParameters.withCredentials; } + + /** + * Select the correct content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} contentTypes - the array of content types that are available for selection + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderContentType (contentTypes: string[]): string { + if (contentTypes.length == 0) { + return undefined; + } + + let type = contentTypes.find(x => this.isJsonMime(x)); + if (type === undefined) { + return contentTypes[0]; + } + return type; + } + + /** + * Select the correct accept content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} accepts - the array of content types that are available for selection. + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderAccept(accepts: string[]): string { + if (accepts.length == 0) { + return undefined; + } + + let type = accepts.find(x => this.isJsonMime(x)); + if (type === undefined) { + return accepts[0]; + } + return type; + } + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + * application/vnd.company+json + * @param {string} mime - MIME (Multipurpose Internet Mail Extensions) + * @return {boolean} True if the given MIME is JSON, false otherwise. + */ + public isJsonMime(mime: string): boolean { + const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); + return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); + } } diff --git a/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts index c2899427ea6e..4435de468a24 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts @@ -58,7 +58,6 @@ export class PetService { return false; } - /** * * @summary Add a new pet to the store @@ -213,7 +212,25 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -253,6 +270,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -291,6 +322,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -330,6 +375,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -361,6 +420,20 @@ export class PetService { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -394,7 +467,25 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + 'application/json', + 'application/xml' + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, @@ -432,10 +523,21 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -451,8 +553,6 @@ export class PetService { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (name !== undefined) { formParams.append('name', name); } @@ -496,10 +596,20 @@ export class PetService { headers.set('Authorization', 'Bearer ' + accessToken); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + // to determine the Content-Type header let consumes: string[] = [ 'multipart/form-data' ]; + const canConsumeForm = this.canConsumeForm(consumes); let formParams: { append(param: string, value: any): void; }; @@ -518,8 +628,6 @@ export class PetService { headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); } - - if (additionalMetadata !== undefined) { formParams.append('additionalMetadata', additionalMetadata); } diff --git a/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts index 24cf96b4136b..3eb043313ff5 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts @@ -57,7 +57,6 @@ export class StoreService { return false; } - /** * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @summary Delete purchase order by ID @@ -134,6 +133,20 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -160,6 +173,19 @@ export class StoreService { headers.set('api_key', this.configuration.apiKeys["api_key"]); } + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -185,6 +211,20 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -210,7 +250,23 @@ export class StoreService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, diff --git a/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts index 5bd7cdbac655..7fd15126b811 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts @@ -57,7 +57,6 @@ export class UserService { return false; } - /** * This can only be done by the logged in user. * @summary Create user @@ -200,7 +199,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -228,7 +243,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -256,7 +287,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Post, @@ -284,6 +331,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, headers: headers, @@ -309,6 +370,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -346,6 +421,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -368,6 +457,20 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, headers: headers, @@ -397,7 +500,23 @@ export class UserService { let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845 - headers.set('Content-Type', 'application/json'); + // to determine the Accept header + let httpHeaderAccepts: string[] = [ + 'application/xml', + 'application/json' + ]; + let httpHeaderAcceptSelected: string = this.configuration.selectHeaderAccept(httpHeaderAccepts); + if (httpHeaderAcceptSelected != undefined) { + headers.set("Accept", httpHeaderAcceptSelected); + } + + // to determine the Content-Type header + let consumes: string[] = [ + ]; + let httpContentTypeSelected:string = this.configuration.selectHeaderContentType(consumes); + if (httpContentTypeSelected != undefined) { + headers.set('Content-Type', httpContentTypeSelected); + } let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Put, diff --git a/samples/client/petstore/typescript-angular-v4/npm/configuration.ts b/samples/client/petstore/typescript-angular-v4/npm/configuration.ts index 005c3a26df33..67b2b9903b37 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/configuration.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/configuration.ts @@ -23,4 +23,57 @@ export class Configuration { this.basePath = configurationParameters.basePath; this.withCredentials = configurationParameters.withCredentials; } + + /** + * Select the correct content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} contentTypes - the array of content types that are available for selection + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderContentType (contentTypes: string[]): string { + if (contentTypes.length == 0) { + return undefined; + } + + let type = contentTypes.find(x => this.isJsonMime(x)); + if (type === undefined) { + return contentTypes[0]; + } + return type; + } + + /** + * Select the correct accept content-type to use for a request. + * Uses {@link Configuration#isJsonMime} to determine the correct accept content-type. + * If no content type is found return the first found type if the contentTypes is not empty + * @param {string[]} accepts - the array of content types that are available for selection. + * @returns {string} the selected content-type or undefined if no selection could be made. + */ + public selectHeaderAccept(accepts: string[]): string { + if (accepts.length == 0) { + return undefined; + } + + let type = accepts.find(x => this.isJsonMime(x)); + if (type === undefined) { + return accepts[0]; + } + return type; + } + + /** + * Check if the given MIME is a JSON MIME. + * JSON MIME examples: + * application/json + * application/json; charset=UTF8 + * APPLICATION/JSON + * application/vnd.company+json + * @param {string} mime - MIME (Multipurpose Internet Mail Extensions) + * @return {boolean} True if the given MIME is JSON, false otherwise. + */ + public isJsonMime(mime: string): boolean { + const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); + return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); + } }