Skip to content

Commit f176716

Browse files
authored
[typescript-axios] avoid stringifying header string values (OpenAPITools#12919)
* feat(typescript-axios) don't stringify string headers * samples
1 parent b722fd9 commit f176716

File tree

13 files changed

+77
-32
lines changed

13 files changed

+77
-32
lines changed

modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,16 @@ export const {{classname}}AxiosParamCreator = function (configuration?: Configur
146146
}
147147
{{/isArray}}
148148
{{^isArray}}
149-
if ({{paramName}} !== undefined && {{paramName}} !== null) {
149+
{{! `val == null` covers for both `null` and `undefined`}}
150+
if ({{paramName}} != null) {
150151
{{#isString}}
151152
localVarHeaderParameter['{{baseName}}'] = String({{paramName}});
152153
{{/isString}}
153154
{{^isString}}
154-
localVarHeaderParameter['{{baseName}}'] = String(JSON.stringify({{paramName}}));
155+
{{! isString is falsy also for $ref that defines a string or enum type}}
156+
localVarHeaderParameter['{{baseName}}'] = typeof {{paramName}} === 'string'
157+
? {{paramName}}
158+
: JSON.stringify({{paramName}});
155159
{{/isString}}
156160
}
157161
{{/isArray}}

modules/openapi-generator/src/test/resources/3_0/petstore-with-complex-headers.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ paths:
4343
type: array
4444
items:
4545
$ref: '#/components/requestBodies/Pet'
46+
- name: Accept
47+
in: header
48+
schema:
49+
$ref: '#/components/schemas/MediaType'
4650
requestBody:
4751
$ref: '#/components/requestBodies/Pet'
4852
put:
@@ -578,6 +582,9 @@ components:
578582
name: api_key
579583
in: header
580584
schemas:
585+
MediaType:
586+
type: string
587+
enum: ['application/json', 'application/xml']
581588
Order:
582589
title: Pet Order
583590
description: An order for a pets from the pet store

samples/client/petstore/typescript-axios/builds/default/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
318318
// oauth required
319319
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
320320

321-
if (apiKey !== undefined && apiKey !== null) {
321+
if (apiKey != null) {
322322
localVarHeaderParameter['api_key'] = String(apiKey);
323323
}
324324

samples/client/petstore/typescript-axios/builds/es6-target/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
318318
// oauth required
319319
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
320320

321-
if (apiKey !== undefined && apiKey !== null) {
321+
if (apiKey != null) {
322322
localVarHeaderParameter['api_key'] = String(apiKey);
323323
}
324324

samples/client/petstore/typescript-axios/builds/test-petstore/api.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,7 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration)
24082408
localVarHeaderParameter['enum_header_string_array'] = mapped.join(COLLECTION_FORMATS["csv"]);
24092409
}
24102410

2411-
if (enumHeaderString !== undefined && enumHeaderString !== null) {
2411+
if (enumHeaderString != null) {
24122412
localVarHeaderParameter['enum_header_string'] = String(enumHeaderString);
24132413
}
24142414

@@ -2485,12 +2485,16 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration)
24852485
localVarQueryParameter['int64_group'] = int64Group;
24862486
}
24872487

2488-
if (requiredBooleanGroup !== undefined && requiredBooleanGroup !== null) {
2489-
localVarHeaderParameter['required_boolean_group'] = String(JSON.stringify(requiredBooleanGroup));
2488+
if (requiredBooleanGroup != null) {
2489+
localVarHeaderParameter['required_boolean_group'] = typeof requiredBooleanGroup === 'string'
2490+
? requiredBooleanGroup
2491+
: JSON.stringify(requiredBooleanGroup);
24902492
}
24912493

2492-
if (booleanGroup !== undefined && booleanGroup !== null) {
2493-
localVarHeaderParameter['boolean_group'] = String(JSON.stringify(booleanGroup));
2494+
if (booleanGroup != null) {
2495+
localVarHeaderParameter['boolean_group'] = typeof booleanGroup === 'string'
2496+
? booleanGroup
2497+
: JSON.stringify(booleanGroup);
24942498
}
24952499

24962500

@@ -3431,7 +3435,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
34313435
// oauth required
34323436
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
34333437

3434-
if (apiKey !== undefined && apiKey !== null) {
3438+
if (apiKey != null) {
34353439
localVarHeaderParameter['api_key'] = String(apiKey);
34363440
}
34373441

samples/client/petstore/typescript-axios/builds/with-complex-headers/api.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ export interface Category {
6565
*/
6666
'name'?: string;
6767
}
68+
/**
69+
*
70+
* @export
71+
* @enum {string}
72+
*/
73+
74+
export const MediaType = {
75+
Json: 'application/json',
76+
Xml: 'application/xml'
77+
} as const;
78+
79+
export type MediaType = typeof MediaType[keyof typeof MediaType];
80+
81+
6882
/**
6983
* An order for a pets from the pet store
7084
* @export
@@ -256,10 +270,11 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
256270
* @param {Pet} pet Pet object that needs to be added to the store
257271
* @param {Pet} [header1]
258272
* @param {Array<Pet>} [header2]
273+
* @param {MediaType} [accept]
259274
* @param {*} [options] Override http request option.
260275
* @throws {RequiredError}
261276
*/
262-
addPet: async (pet: Pet, header1?: Pet, header2?: Array<Pet>, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
277+
addPet: async (pet: Pet, header1?: Pet, header2?: Array<Pet>, accept?: MediaType, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
263278
// verify required parameter 'pet' is not null or undefined
264279
assertParamExists('addPet', 'pet', pet)
265280
const localVarPath = `/pet`;
@@ -278,15 +293,23 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
278293
// oauth required
279294
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
280295

281-
if (header1 !== undefined && header1 !== null) {
282-
localVarHeaderParameter['header1'] = String(JSON.stringify(header1));
296+
if (header1 != null) {
297+
localVarHeaderParameter['header1'] = typeof header1 === 'string'
298+
? header1
299+
: JSON.stringify(header1);
283300
}
284301

285302
if (header2) {
286303
let mapped = header2.map(value => (<any>"Array<Pet>" !== "Array<string>") ? JSON.stringify(value) : (value || ""));
287304
localVarHeaderParameter['header2'] = mapped.join(COLLECTION_FORMATS["csv"]);
288305
}
289306

307+
if (accept != null) {
308+
localVarHeaderParameter['Accept'] = typeof accept === 'string'
309+
? accept
310+
: JSON.stringify(accept);
311+
}
312+
290313

291314

292315
localVarHeaderParameter['Content-Type'] = 'application/json';
@@ -329,7 +352,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
329352
// oauth required
330353
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
331354

332-
if (apiKey !== undefined && apiKey !== null) {
355+
if (apiKey != null) {
333356
localVarHeaderParameter['api_key'] = String(apiKey);
334357
}
335358

@@ -624,11 +647,12 @@ export const PetApiFp = function(configuration?: Configuration) {
624647
* @param {Pet} pet Pet object that needs to be added to the store
625648
* @param {Pet} [header1]
626649
* @param {Array<Pet>} [header2]
650+
* @param {MediaType} [accept]
627651
* @param {*} [options] Override http request option.
628652
* @throws {RequiredError}
629653
*/
630-
async addPet(pet: Pet, header1?: Pet, header2?: Array<Pet>, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<void>> {
631-
const localVarAxiosArgs = await localVarAxiosParamCreator.addPet(pet, header1, header2, options);
654+
async addPet(pet: Pet, header1?: Pet, header2?: Array<Pet>, accept?: MediaType, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<void>> {
655+
const localVarAxiosArgs = await localVarAxiosParamCreator.addPet(pet, header1, header2, accept, options);
632656
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
633657
},
634658
/**
@@ -730,11 +754,12 @@ export const PetApiFactory = function (configuration?: Configuration, basePath?:
730754
* @param {Pet} pet Pet object that needs to be added to the store
731755
* @param {Pet} [header1]
732756
* @param {Array<Pet>} [header2]
757+
* @param {MediaType} [accept]
733758
* @param {*} [options] Override http request option.
734759
* @throws {RequiredError}
735760
*/
736-
addPet(pet: Pet, header1?: Pet, header2?: Array<Pet>, options?: any): AxiosPromise<void> {
737-
return localVarFp.addPet(pet, header1, header2, options).then((request) => request(axios, basePath));
761+
addPet(pet: Pet, header1?: Pet, header2?: Array<Pet>, accept?: MediaType, options?: any): AxiosPromise<void> {
762+
return localVarFp.addPet(pet, header1, header2, accept, options).then((request) => request(axios, basePath));
738763
},
739764
/**
740765
*
@@ -828,12 +853,13 @@ export class PetApi extends BaseAPI {
828853
* @param {Pet} pet Pet object that needs to be added to the store
829854
* @param {Pet} [header1]
830855
* @param {Array<Pet>} [header2]
856+
* @param {MediaType} [accept]
831857
* @param {*} [options] Override http request option.
832858
* @throws {RequiredError}
833859
* @memberof PetApi
834860
*/
835-
public addPet(pet: Pet, header1?: Pet, header2?: Array<Pet>, options?: AxiosRequestConfig) {
836-
return PetApiFp(this.configuration).addPet(pet, header1, header2, options).then((request) => request(this.axios, this.basePath));
861+
public addPet(pet: Pet, header1?: Pet, header2?: Array<Pet>, accept?: MediaType, options?: AxiosRequestConfig) {
862+
return PetApiFp(this.configuration).addPet(pet, header1, header2, accept, options).then((request) => request(this.axios, this.basePath));
837863
}
838864

839865
/**

samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/api.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration)
20062006
localVarHeaderParameter['enum_header_string_array'] = mapped.join(COLLECTION_FORMATS["csv"]);
20072007
}
20082008

2009-
if (enumHeaderString !== undefined && enumHeaderString !== null) {
2009+
if (enumHeaderString != null) {
20102010
localVarHeaderParameter['enum_header_string'] = String(enumHeaderString);
20112011
}
20122012

@@ -2083,12 +2083,16 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration)
20832083
localVarQueryParameter['int64_group'] = int64Group;
20842084
}
20852085

2086-
if (requiredBooleanGroup !== undefined && requiredBooleanGroup !== null) {
2087-
localVarHeaderParameter['required_boolean_group'] = String(JSON.stringify(requiredBooleanGroup));
2086+
if (requiredBooleanGroup != null) {
2087+
localVarHeaderParameter['required_boolean_group'] = typeof requiredBooleanGroup === 'string'
2088+
? requiredBooleanGroup
2089+
: JSON.stringify(requiredBooleanGroup);
20882090
}
20892091

2090-
if (booleanGroup !== undefined && booleanGroup !== null) {
2091-
localVarHeaderParameter['boolean_group'] = String(JSON.stringify(booleanGroup));
2092+
if (booleanGroup != null) {
2093+
localVarHeaderParameter['boolean_group'] = typeof booleanGroup === 'string'
2094+
? booleanGroup
2095+
: JSON.stringify(booleanGroup);
20922096
}
20932097

20942098

@@ -3076,7 +3080,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
30763080
// oauth required
30773081
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
30783082

3079-
if (apiKey !== undefined && apiKey !== null) {
3083+
if (apiKey != null) {
30803084
localVarHeaderParameter['api_key'] = String(apiKey);
30813085
}
30823086

samples/client/petstore/typescript-axios/builds/with-interfaces/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
318318
// oauth required
319319
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
320320

321-
if (apiKey !== undefined && apiKey !== null) {
321+
if (apiKey != null) {
322322
localVarHeaderParameter['api_key'] = String(apiKey);
323323
}
324324

samples/client/petstore/typescript-axios/builds/with-node-imports/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
322322
// oauth required
323323
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
324324

325-
if (apiKey !== undefined && apiKey !== null) {
325+
if (apiKey != null) {
326326
localVarHeaderParameter['api_key'] = String(apiKey);
327327
}
328328

samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/api/another/level/pet-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration)
9898
// oauth required
9999
await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration)
100100

101-
if (apiKey !== undefined && apiKey !== null) {
101+
if (apiKey != null) {
102102
localVarHeaderParameter['api_key'] = String(apiKey);
103103
}
104104

0 commit comments

Comments
 (0)