Skip to content

Commit c136b83

Browse files
clemens-smartparkingmacjohnny
authored andcommitted
[typescript][node]: Add accept header if produces is not empty (#3966)
* [typescript][node]: Add accept header if produces is not empty Uses the correct Accept media types as specified in the OpenApi specifications. Related to #3944 * Update samples files * [typescript][node]: Give presedence to 'application/json' The endpoint may support multiple formats, e.g. 'application/xml', 'application/json'. However, we don't really support xml. In this case only accept 'application/json'.
1 parent 2272743 commit c136b83

7 files changed

Lines changed: 135 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ export class {{classname}} {
134134
.replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}};
135135
let localVarQueryParameters: any = {};
136136
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
137+
{{#hasProduces}}
138+
const produces = [{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}];
139+
// give precedence to 'application/json'
140+
if (produces.indexOf('application/json') >= 0) {
141+
localVarHeaderParams.Accept = 'application/json';
142+
} else {
143+
localVarHeaderParams.Accept = produces.join(',');
144+
}
145+
{{/hasProduces}}
137146
let localVarFormParams: any = {};
138147

139148
{{#allParams}}

samples/client/petstore/typescript-node/default/api/petApi.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ export class PetApi {
205205
const localVarPath = this.basePath + '/pet/findByStatus';
206206
let localVarQueryParameters: any = {};
207207
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
208+
const produces = ['application/xml', 'application/json'];
209+
// give precedence to 'application/json'
210+
if (produces.indexOf('application/json') >= 0) {
211+
localVarHeaderParams.Accept = 'application/json';
212+
} else {
213+
localVarHeaderParams.Accept = produces.join(',');
214+
}
208215
let localVarFormParams: any = {};
209216

210217
// verify required parameter 'status' is not null or undefined
@@ -266,6 +273,13 @@ export class PetApi {
266273
const localVarPath = this.basePath + '/pet/findByTags';
267274
let localVarQueryParameters: any = {};
268275
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
276+
const produces = ['application/xml', 'application/json'];
277+
// give precedence to 'application/json'
278+
if (produces.indexOf('application/json') >= 0) {
279+
localVarHeaderParams.Accept = 'application/json';
280+
} else {
281+
localVarHeaderParams.Accept = produces.join(',');
282+
}
269283
let localVarFormParams: any = {};
270284

271285
// verify required parameter 'tags' is not null or undefined
@@ -328,6 +342,13 @@ export class PetApi {
328342
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
329343
let localVarQueryParameters: any = {};
330344
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
345+
const produces = ['application/xml', 'application/json'];
346+
// give precedence to 'application/json'
347+
if (produces.indexOf('application/json') >= 0) {
348+
localVarHeaderParams.Accept = 'application/json';
349+
} else {
350+
localVarHeaderParams.Accept = produces.join(',');
351+
}
331352
let localVarFormParams: any = {};
332353

333354
// verify required parameter 'petId' is not null or undefined
@@ -512,6 +533,13 @@ export class PetApi {
512533
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
513534
let localVarQueryParameters: any = {};
514535
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
536+
const produces = ['application/json'];
537+
// give precedence to 'application/json'
538+
if (produces.indexOf('application/json') >= 0) {
539+
localVarHeaderParams.Accept = 'application/json';
540+
} else {
541+
localVarHeaderParams.Accept = produces.join(',');
542+
}
515543
let localVarFormParams: any = {};
516544

517545
// verify required parameter 'petId' is not null or undefined

samples/client/petstore/typescript-node/default/api/storeApi.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ export class StoreApi {
137137
const localVarPath = this.basePath + '/store/inventory';
138138
let localVarQueryParameters: any = {};
139139
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
140+
const produces = ['application/json'];
141+
// give precedence to 'application/json'
142+
if (produces.indexOf('application/json') >= 0) {
143+
localVarHeaderParams.Accept = 'application/json';
144+
} else {
145+
localVarHeaderParams.Accept = produces.join(',');
146+
}
140147
let localVarFormParams: any = {};
141148

142149
(<any>Object).assign(localVarHeaderParams, options.headers);
@@ -190,6 +197,13 @@ export class StoreApi {
190197
.replace('{' + 'orderId' + '}', encodeURIComponent(String(orderId)));
191198
let localVarQueryParameters: any = {};
192199
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
200+
const produces = ['application/xml', 'application/json'];
201+
// give precedence to 'application/json'
202+
if (produces.indexOf('application/json') >= 0) {
203+
localVarHeaderParams.Accept = 'application/json';
204+
} else {
205+
localVarHeaderParams.Accept = produces.join(',');
206+
}
193207
let localVarFormParams: any = {};
194208

195209
// verify required parameter 'orderId' is not null or undefined
@@ -245,6 +259,13 @@ export class StoreApi {
245259
const localVarPath = this.basePath + '/store/order';
246260
let localVarQueryParameters: any = {};
247261
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
262+
const produces = ['application/xml', 'application/json'];
263+
// give precedence to 'application/json'
264+
if (produces.indexOf('application/json') >= 0) {
265+
localVarHeaderParams.Accept = 'application/json';
266+
} else {
267+
localVarHeaderParams.Accept = produces.join(',');
268+
}
248269
let localVarFormParams: any = {};
249270

250271
// verify required parameter 'body' is not null or undefined

samples/client/petstore/typescript-node/default/api/userApi.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ export class UserApi {
301301
.replace('{' + 'username' + '}', encodeURIComponent(String(username)));
302302
let localVarQueryParameters: any = {};
303303
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
304+
const produces = ['application/xml', 'application/json'];
305+
// give precedence to 'application/json'
306+
if (produces.indexOf('application/json') >= 0) {
307+
localVarHeaderParams.Accept = 'application/json';
308+
} else {
309+
localVarHeaderParams.Accept = produces.join(',');
310+
}
304311
let localVarFormParams: any = {};
305312

306313
// verify required parameter 'username' is not null or undefined
@@ -357,6 +364,13 @@ export class UserApi {
357364
const localVarPath = this.basePath + '/user/login';
358365
let localVarQueryParameters: any = {};
359366
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
367+
const produces = ['application/xml', 'application/json'];
368+
// give precedence to 'application/json'
369+
if (produces.indexOf('application/json') >= 0) {
370+
localVarHeaderParams.Accept = 'application/json';
371+
} else {
372+
localVarHeaderParams.Accept = produces.join(',');
373+
}
360374
let localVarFormParams: any = {};
361375

362376
// verify required parameter 'username' is not null or undefined

samples/client/petstore/typescript-node/npm/api/petApi.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ export class PetApi {
205205
const localVarPath = this.basePath + '/pet/findByStatus';
206206
let localVarQueryParameters: any = {};
207207
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
208+
const produces = ['application/xml', 'application/json'];
209+
// give precedence to 'application/json'
210+
if (produces.indexOf('application/json') >= 0) {
211+
localVarHeaderParams.Accept = 'application/json';
212+
} else {
213+
localVarHeaderParams.Accept = produces.join(',');
214+
}
208215
let localVarFormParams: any = {};
209216

210217
// verify required parameter 'status' is not null or undefined
@@ -266,6 +273,13 @@ export class PetApi {
266273
const localVarPath = this.basePath + '/pet/findByTags';
267274
let localVarQueryParameters: any = {};
268275
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
276+
const produces = ['application/xml', 'application/json'];
277+
// give precedence to 'application/json'
278+
if (produces.indexOf('application/json') >= 0) {
279+
localVarHeaderParams.Accept = 'application/json';
280+
} else {
281+
localVarHeaderParams.Accept = produces.join(',');
282+
}
269283
let localVarFormParams: any = {};
270284

271285
// verify required parameter 'tags' is not null or undefined
@@ -328,6 +342,13 @@ export class PetApi {
328342
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
329343
let localVarQueryParameters: any = {};
330344
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
345+
const produces = ['application/xml', 'application/json'];
346+
// give precedence to 'application/json'
347+
if (produces.indexOf('application/json') >= 0) {
348+
localVarHeaderParams.Accept = 'application/json';
349+
} else {
350+
localVarHeaderParams.Accept = produces.join(',');
351+
}
331352
let localVarFormParams: any = {};
332353

333354
// verify required parameter 'petId' is not null or undefined
@@ -512,6 +533,13 @@ export class PetApi {
512533
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
513534
let localVarQueryParameters: any = {};
514535
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
536+
const produces = ['application/json'];
537+
// give precedence to 'application/json'
538+
if (produces.indexOf('application/json') >= 0) {
539+
localVarHeaderParams.Accept = 'application/json';
540+
} else {
541+
localVarHeaderParams.Accept = produces.join(',');
542+
}
515543
let localVarFormParams: any = {};
516544

517545
// verify required parameter 'petId' is not null or undefined

samples/client/petstore/typescript-node/npm/api/storeApi.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ export class StoreApi {
137137
const localVarPath = this.basePath + '/store/inventory';
138138
let localVarQueryParameters: any = {};
139139
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
140+
const produces = ['application/json'];
141+
// give precedence to 'application/json'
142+
if (produces.indexOf('application/json') >= 0) {
143+
localVarHeaderParams.Accept = 'application/json';
144+
} else {
145+
localVarHeaderParams.Accept = produces.join(',');
146+
}
140147
let localVarFormParams: any = {};
141148

142149
(<any>Object).assign(localVarHeaderParams, options.headers);
@@ -190,6 +197,13 @@ export class StoreApi {
190197
.replace('{' + 'orderId' + '}', encodeURIComponent(String(orderId)));
191198
let localVarQueryParameters: any = {};
192199
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
200+
const produces = ['application/xml', 'application/json'];
201+
// give precedence to 'application/json'
202+
if (produces.indexOf('application/json') >= 0) {
203+
localVarHeaderParams.Accept = 'application/json';
204+
} else {
205+
localVarHeaderParams.Accept = produces.join(',');
206+
}
193207
let localVarFormParams: any = {};
194208

195209
// verify required parameter 'orderId' is not null or undefined
@@ -245,6 +259,13 @@ export class StoreApi {
245259
const localVarPath = this.basePath + '/store/order';
246260
let localVarQueryParameters: any = {};
247261
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
262+
const produces = ['application/xml', 'application/json'];
263+
// give precedence to 'application/json'
264+
if (produces.indexOf('application/json') >= 0) {
265+
localVarHeaderParams.Accept = 'application/json';
266+
} else {
267+
localVarHeaderParams.Accept = produces.join(',');
268+
}
248269
let localVarFormParams: any = {};
249270

250271
// verify required parameter 'body' is not null or undefined

samples/client/petstore/typescript-node/npm/api/userApi.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ export class UserApi {
301301
.replace('{' + 'username' + '}', encodeURIComponent(String(username)));
302302
let localVarQueryParameters: any = {};
303303
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
304+
const produces = ['application/xml', 'application/json'];
305+
// give precedence to 'application/json'
306+
if (produces.indexOf('application/json') >= 0) {
307+
localVarHeaderParams.Accept = 'application/json';
308+
} else {
309+
localVarHeaderParams.Accept = produces.join(',');
310+
}
304311
let localVarFormParams: any = {};
305312

306313
// verify required parameter 'username' is not null or undefined
@@ -357,6 +364,13 @@ export class UserApi {
357364
const localVarPath = this.basePath + '/user/login';
358365
let localVarQueryParameters: any = {};
359366
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
367+
const produces = ['application/xml', 'application/json'];
368+
// give precedence to 'application/json'
369+
if (produces.indexOf('application/json') >= 0) {
370+
localVarHeaderParams.Accept = 'application/json';
371+
} else {
372+
localVarHeaderParams.Accept = produces.join(',');
373+
}
360374
let localVarFormParams: any = {};
361375

362376
// verify required parameter 'username' is not null or undefined

0 commit comments

Comments
 (0)