From 07df0759d325786bebdfb2a55544bda683b9dfc2 Mon Sep 17 00:00:00 2001 From: stoetti <31430612+stoetti@users.noreply.github.com> Date: Sat, 23 Sep 2017 09:48:05 +0200 Subject: [PATCH] [TypeScript][Angular] Better support for "Accept", "Content-Type" (#6454) * add consumes and produces as corresponding headers if present * add check for empty array fix copy/paste error * fix styling * add isJsonMime filter produces- and consumes-Arrays for json-mime items --- .../typescript-angular/api.service.mustache | 14 +++++++ .../default/api/pet.service.ts | 42 +++++++++++++++++++ .../default/api/store.service.ts | 16 +++++++ .../default/api/user.service.ts | 32 ++++++++++++++ .../npm/api/pet.service.ts | 42 +++++++++++++++++++ .../npm/api/store.service.ts | 16 +++++++ .../npm/api/user.service.ts | 32 ++++++++++++++ .../with-interfaces/api/pet.service.ts | 42 +++++++++++++++++++ .../with-interfaces/api/store.service.ts | 16 +++++++ .../with-interfaces/api/user.service.ts | 32 ++++++++++++++ 10 files changed, 284 insertions(+) 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 5da473f5ccbb..3c59792963a4 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 @@ -78,6 +78,11 @@ export class {{classname}} { return false; } + public isJsonMime(mime: string): boolean { + const jsonMime: RegExp = new RegExp('(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$'); + return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); + } + {{#operation}} /** * {{¬es}} @@ -175,6 +180,11 @@ export class {{classname}} { '{{{mediaType}}}'{{#hasMore}},{{/hasMore}} {{/consumes}} ]; + + if (consumes != null && consumes.length > 0) { + headers.set('Content-Type', consumes.filter(item => this.isJsonMime(item)).join(";")); + } + let canConsumeForm = this.canConsumeForm(consumes); let useForm = false; {{#formParams}} @@ -194,6 +204,10 @@ export class {{classname}} { {{/produces}} ]; + if (produces != null && produces.length > 0) { + headers.set('Accept', produces.filter(item => this.isJsonMime(item)).join(';')); + } + {{#authMethods}} // authentication ({{name}}) required {{#isApiKey}} 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 b05fe8fc6371..e4937a071c6d 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 @@ -230,6 +230,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -285,6 +289,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -335,6 +343,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -385,6 +397,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -432,6 +448,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (api_key) required if (this.configuration.apiKeys["api_key"]) { headers.set('api_key', this.configuration.apiKeys["api_key"]); @@ -474,6 +494,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -523,6 +547,11 @@ export class PetService { let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + + if ((consumes != null) && (consumes.length > 0)) { + headers.set('Content-Type', consumes.join(';')); + } + let canConsumeForm = this.canConsumeForm(consumes); let useForm = false; let formParams = new (useForm ? FormData : URLSearchParams as any)() as { @@ -535,6 +564,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -590,6 +623,11 @@ export class PetService { let consumes: string[] = [ 'multipart/form-data' ]; + + if ((consumes != null) && (consumes.length > 0)) { + headers.set('Content-Type', consumes.join(';')); + } + let canConsumeForm = this.canConsumeForm(consumes); let useForm = false; useForm = canConsumeForm; @@ -602,6 +640,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { 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 8765ad7c4a93..7044e00d1dd5 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 @@ -160,6 +160,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, @@ -191,6 +195,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (api_key) required if (this.configuration.apiKeys["api_key"]) { headers.set('api_key', this.configuration.apiKeys["api_key"]); @@ -234,6 +242,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -271,6 +283,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); 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 4322a11f7efb..07057355f0c8 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 @@ -225,6 +225,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -265,6 +269,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -305,6 +313,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -346,6 +358,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, @@ -384,6 +400,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -434,6 +454,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -466,6 +490,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -509,6 +537,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/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 b05fe8fc6371..e4937a071c6d 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 @@ -230,6 +230,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -285,6 +289,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -335,6 +343,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -385,6 +397,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -432,6 +448,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (api_key) required if (this.configuration.apiKeys["api_key"]) { headers.set('api_key', this.configuration.apiKeys["api_key"]); @@ -474,6 +494,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -523,6 +547,11 @@ export class PetService { let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + + if ((consumes != null) && (consumes.length > 0)) { + headers.set('Content-Type', consumes.join(';')); + } + let canConsumeForm = this.canConsumeForm(consumes); let useForm = false; let formParams = new (useForm ? FormData : URLSearchParams as any)() as { @@ -535,6 +564,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -590,6 +623,11 @@ export class PetService { let consumes: string[] = [ 'multipart/form-data' ]; + + if ((consumes != null) && (consumes.length > 0)) { + headers.set('Content-Type', consumes.join(';')); + } + let canConsumeForm = this.canConsumeForm(consumes); let useForm = false; useForm = canConsumeForm; @@ -602,6 +640,10 @@ export class PetService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { 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 8765ad7c4a93..7044e00d1dd5 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 @@ -160,6 +160,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, @@ -191,6 +195,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (api_key) required if (this.configuration.apiKeys["api_key"]) { headers.set('api_key', this.configuration.apiKeys["api_key"]); @@ -234,6 +242,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -271,6 +283,10 @@ export class StoreService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); 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 4322a11f7efb..07057355f0c8 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 @@ -225,6 +225,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -265,6 +269,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -305,6 +313,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -346,6 +358,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, @@ -384,6 +400,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -434,6 +454,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -466,6 +490,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -509,6 +537,10 @@ export class UserService { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/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 cfba17412165..cfcf1c48b7e1 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 @@ -231,6 +231,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -286,6 +290,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -336,6 +344,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -386,6 +398,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -433,6 +449,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (api_key) required if (this.configuration.apiKeys["api_key"]) { headers.set('api_key', this.configuration.apiKeys["api_key"]); @@ -475,6 +495,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -524,6 +548,11 @@ export class PetService implements PetServiceInterface { let consumes: string[] = [ 'application/x-www-form-urlencoded' ]; + + if ((consumes != null) && (consumes.length > 0)) { + headers.set('Content-Type', consumes.join(';')); + } + let canConsumeForm = this.canConsumeForm(consumes); let useForm = false; let formParams = new (useForm ? FormData : URLSearchParams as any)() as { @@ -536,6 +565,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { @@ -591,6 +624,11 @@ export class PetService implements PetServiceInterface { let consumes: string[] = [ 'multipart/form-data' ]; + + if ((consumes != null) && (consumes.length > 0)) { + headers.set('Content-Type', consumes.join(';')); + } + let canConsumeForm = this.canConsumeForm(consumes); let useForm = false; useForm = canConsumeForm; @@ -603,6 +641,10 @@ export class PetService implements PetServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (petstore_auth) required // oauth required if (this.configuration.accessToken) { 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 6a3d0fc745cb..b6edf9a45ada 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 @@ -161,6 +161,10 @@ export class StoreService implements StoreServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, @@ -192,6 +196,10 @@ export class StoreService implements StoreServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + // authentication (api_key) required if (this.configuration.apiKeys["api_key"]) { headers.set('api_key', this.configuration.apiKeys["api_key"]); @@ -235,6 +243,10 @@ export class StoreService implements StoreServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -272,6 +284,10 @@ export class StoreService implements StoreServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); 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 593c8d71a92d..706562c088b2 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 @@ -226,6 +226,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -266,6 +270,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -306,6 +314,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json'); @@ -347,6 +359,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Delete, @@ -385,6 +401,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -435,6 +455,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -467,6 +491,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + let requestOptions: RequestOptionsArgs = new RequestOptions({ method: RequestMethod.Get, @@ -510,6 +538,10 @@ export class UserService implements UserServiceInterface { 'application/json' ]; + if ((produces != null) && (produces.length > 0)) { + headers.set('Accept', produces.join(';')); + } + headers.set('Content-Type', 'application/json');