From f176716a617db04d5759323235c6dcb8193c75ad Mon Sep 17 00:00:00 2001 From: stropho <3704482+stropho@users.noreply.github.com> Date: Tue, 19 Jul 2022 09:55:34 +0200 Subject: [PATCH] [typescript-axios] avoid stringifying header string values (#12919) * feat(typescript-axios) don't stringify string headers * samples --- .../typescript-axios/apiInner.mustache | 8 +++- .../3_0/petstore-with-complex-headers.yaml | 7 +++ .../typescript-axios/builds/default/api.ts | 2 +- .../typescript-axios/builds/es6-target/api.ts | 2 +- .../builds/test-petstore/api.ts | 16 ++++--- .../builds/with-complex-headers/api.ts | 46 +++++++++++++++---- .../api.ts | 16 ++++--- .../builds/with-interfaces/api.ts | 2 +- .../builds/with-node-imports/api.ts | 2 +- .../api/another/level/pet-api.ts | 2 +- .../builds/with-npm-version/api.ts | 2 +- .../with-single-request-parameters/api.ts | 2 +- .../builds/with-string-enums/api.ts | 2 +- 13 files changed, 77 insertions(+), 32 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache b/modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache index 04812f66dde..e40a0cd13cf 100644 --- a/modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache @@ -146,12 +146,16 @@ export const {{classname}}AxiosParamCreator = function (configuration?: Configur } {{/isArray}} {{^isArray}} - if ({{paramName}} !== undefined && {{paramName}} !== null) { + {{! `val == null` covers for both `null` and `undefined`}} + if ({{paramName}} != null) { {{#isString}} localVarHeaderParameter['{{baseName}}'] = String({{paramName}}); {{/isString}} {{^isString}} - localVarHeaderParameter['{{baseName}}'] = String(JSON.stringify({{paramName}})); + {{! isString is falsy also for $ref that defines a string or enum type}} + localVarHeaderParameter['{{baseName}}'] = typeof {{paramName}} === 'string' + ? {{paramName}} + : JSON.stringify({{paramName}}); {{/isString}} } {{/isArray}} diff --git a/modules/openapi-generator/src/test/resources/3_0/petstore-with-complex-headers.yaml b/modules/openapi-generator/src/test/resources/3_0/petstore-with-complex-headers.yaml index 2233d4e2006..a21b6b4e0d0 100644 --- a/modules/openapi-generator/src/test/resources/3_0/petstore-with-complex-headers.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/petstore-with-complex-headers.yaml @@ -43,6 +43,10 @@ paths: type: array items: $ref: '#/components/requestBodies/Pet' + - name: Accept + in: header + schema: + $ref: '#/components/schemas/MediaType' requestBody: $ref: '#/components/requestBodies/Pet' put: @@ -578,6 +582,9 @@ components: name: api_key in: header schemas: + MediaType: + type: string + enum: ['application/json', 'application/xml'] Order: title: Pet Order description: An order for a pets from the pet store diff --git a/samples/client/petstore/typescript-axios/builds/default/api.ts b/samples/client/petstore/typescript-axios/builds/default/api.ts index 893802cbed3..9e0c0da8e17 100644 --- a/samples/client/petstore/typescript-axios/builds/default/api.ts +++ b/samples/client/petstore/typescript-axios/builds/default/api.ts @@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/es6-target/api.ts b/samples/client/petstore/typescript-axios/builds/es6-target/api.ts index 893802cbed3..9e0c0da8e17 100644 --- a/samples/client/petstore/typescript-axios/builds/es6-target/api.ts +++ b/samples/client/petstore/typescript-axios/builds/es6-target/api.ts @@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts b/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts index 0d480e40bc0..0d7722bf019 100644 --- a/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts +++ b/samples/client/petstore/typescript-axios/builds/test-petstore/api.ts @@ -2408,7 +2408,7 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration) localVarHeaderParameter['enum_header_string_array'] = mapped.join(COLLECTION_FORMATS["csv"]); } - if (enumHeaderString !== undefined && enumHeaderString !== null) { + if (enumHeaderString != null) { localVarHeaderParameter['enum_header_string'] = String(enumHeaderString); } @@ -2485,12 +2485,16 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration) localVarQueryParameter['int64_group'] = int64Group; } - if (requiredBooleanGroup !== undefined && requiredBooleanGroup !== null) { - localVarHeaderParameter['required_boolean_group'] = String(JSON.stringify(requiredBooleanGroup)); + if (requiredBooleanGroup != null) { + localVarHeaderParameter['required_boolean_group'] = typeof requiredBooleanGroup === 'string' + ? requiredBooleanGroup + : JSON.stringify(requiredBooleanGroup); } - if (booleanGroup !== undefined && booleanGroup !== null) { - localVarHeaderParameter['boolean_group'] = String(JSON.stringify(booleanGroup)); + if (booleanGroup != null) { + localVarHeaderParameter['boolean_group'] = typeof booleanGroup === 'string' + ? booleanGroup + : JSON.stringify(booleanGroup); } @@ -3431,7 +3435,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/with-complex-headers/api.ts b/samples/client/petstore/typescript-axios/builds/with-complex-headers/api.ts index 8c7d0abac4a..d963318e625 100644 --- a/samples/client/petstore/typescript-axios/builds/with-complex-headers/api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-complex-headers/api.ts @@ -65,6 +65,20 @@ export interface Category { */ 'name'?: string; } +/** + * + * @export + * @enum {string} + */ + +export const MediaType = { + Json: 'application/json', + Xml: 'application/xml' +} as const; + +export type MediaType = typeof MediaType[keyof typeof MediaType]; + + /** * An order for a pets from the pet store * @export @@ -256,10 +270,11 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) * @param {Pet} pet Pet object that needs to be added to the store * @param {Pet} [header1] * @param {Array} [header2] + * @param {MediaType} [accept] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - addPet: async (pet: Pet, header1?: Pet, header2?: Array, options: AxiosRequestConfig = {}): Promise => { + addPet: async (pet: Pet, header1?: Pet, header2?: Array, accept?: MediaType, options: AxiosRequestConfig = {}): Promise => { // verify required parameter 'pet' is not null or undefined assertParamExists('addPet', 'pet', pet) const localVarPath = `/pet`; @@ -278,8 +293,10 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (header1 !== undefined && header1 !== null) { - localVarHeaderParameter['header1'] = String(JSON.stringify(header1)); + if (header1 != null) { + localVarHeaderParameter['header1'] = typeof header1 === 'string' + ? header1 + : JSON.stringify(header1); } if (header2) { @@ -287,6 +304,12 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) localVarHeaderParameter['header2'] = mapped.join(COLLECTION_FORMATS["csv"]); } + if (accept != null) { + localVarHeaderParameter['Accept'] = typeof accept === 'string' + ? accept + : JSON.stringify(accept); + } + localVarHeaderParameter['Content-Type'] = 'application/json'; @@ -329,7 +352,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } @@ -624,11 +647,12 @@ export const PetApiFp = function(configuration?: Configuration) { * @param {Pet} pet Pet object that needs to be added to the store * @param {Pet} [header1] * @param {Array} [header2] + * @param {MediaType} [accept] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async addPet(pet: Pet, header1?: Pet, header2?: Array, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { - const localVarAxiosArgs = await localVarAxiosParamCreator.addPet(pet, header1, header2, options); + async addPet(pet: Pet, header1?: Pet, header2?: Array, accept?: MediaType, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.addPet(pet, header1, header2, accept, options); return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); }, /** @@ -730,11 +754,12 @@ export const PetApiFactory = function (configuration?: Configuration, basePath?: * @param {Pet} pet Pet object that needs to be added to the store * @param {Pet} [header1] * @param {Array} [header2] + * @param {MediaType} [accept] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - addPet(pet: Pet, header1?: Pet, header2?: Array, options?: any): AxiosPromise { - return localVarFp.addPet(pet, header1, header2, options).then((request) => request(axios, basePath)); + addPet(pet: Pet, header1?: Pet, header2?: Array, accept?: MediaType, options?: any): AxiosPromise { + return localVarFp.addPet(pet, header1, header2, accept, options).then((request) => request(axios, basePath)); }, /** * @@ -828,12 +853,13 @@ export class PetApi extends BaseAPI { * @param {Pet} pet Pet object that needs to be added to the store * @param {Pet} [header1] * @param {Array} [header2] + * @param {MediaType} [accept] * @param {*} [options] Override http request option. * @throws {RequiredError} * @memberof PetApi */ - public addPet(pet: Pet, header1?: Pet, header2?: Array, options?: AxiosRequestConfig) { - return PetApiFp(this.configuration).addPet(pet, header1, header2, options).then((request) => request(this.axios, this.basePath)); + public addPet(pet: Pet, header1?: Pet, header2?: Array, accept?: MediaType, options?: AxiosRequestConfig) { + return PetApiFp(this.configuration).addPet(pet, header1, header2, accept, options).then((request) => request(this.axios, this.basePath)); } /** diff --git a/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/api.ts b/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/api.ts index 393e19a74eb..5f6982c17be 100644 --- a/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-fake-endpoints-models-for-testing-with-http-signature/api.ts @@ -2006,7 +2006,7 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration) localVarHeaderParameter['enum_header_string_array'] = mapped.join(COLLECTION_FORMATS["csv"]); } - if (enumHeaderString !== undefined && enumHeaderString !== null) { + if (enumHeaderString != null) { localVarHeaderParameter['enum_header_string'] = String(enumHeaderString); } @@ -2083,12 +2083,16 @@ export const FakeApiAxiosParamCreator = function (configuration?: Configuration) localVarQueryParameter['int64_group'] = int64Group; } - if (requiredBooleanGroup !== undefined && requiredBooleanGroup !== null) { - localVarHeaderParameter['required_boolean_group'] = String(JSON.stringify(requiredBooleanGroup)); + if (requiredBooleanGroup != null) { + localVarHeaderParameter['required_boolean_group'] = typeof requiredBooleanGroup === 'string' + ? requiredBooleanGroup + : JSON.stringify(requiredBooleanGroup); } - if (booleanGroup !== undefined && booleanGroup !== null) { - localVarHeaderParameter['boolean_group'] = String(JSON.stringify(booleanGroup)); + if (booleanGroup != null) { + localVarHeaderParameter['boolean_group'] = typeof booleanGroup === 'string' + ? booleanGroup + : JSON.stringify(booleanGroup); } @@ -3076,7 +3080,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/with-interfaces/api.ts b/samples/client/petstore/typescript-axios/builds/with-interfaces/api.ts index d808b5aa937..43860a5248d 100644 --- a/samples/client/petstore/typescript-axios/builds/with-interfaces/api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-interfaces/api.ts @@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/with-node-imports/api.ts b/samples/client/petstore/typescript-axios/builds/with-node-imports/api.ts index 0cfec1ad4b3..0d107523ea5 100644 --- a/samples/client/petstore/typescript-axios/builds/with-node-imports/api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-node-imports/api.ts @@ -322,7 +322,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/api/another/level/pet-api.ts b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/api/another/level/pet-api.ts index d84bdc20973..ef9a08343be 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/api/another/level/pet-api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version-and-separate-models-and-api/api/another/level/pet-api.ts @@ -98,7 +98,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/with-npm-version/api.ts b/samples/client/petstore/typescript-axios/builds/with-npm-version/api.ts index 893802cbed3..9e0c0da8e17 100644 --- a/samples/client/petstore/typescript-axios/builds/with-npm-version/api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-npm-version/api.ts @@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/api.ts b/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/api.ts index 5a06a70cb99..0a09c0249f8 100644 --- a/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-single-request-parameters/api.ts @@ -318,7 +318,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); } diff --git a/samples/client/petstore/typescript-axios/builds/with-string-enums/api.ts b/samples/client/petstore/typescript-axios/builds/with-string-enums/api.ts index fd8ae456d7a..5bcc63b222e 100644 --- a/samples/client/petstore/typescript-axios/builds/with-string-enums/api.ts +++ b/samples/client/petstore/typescript-axios/builds/with-string-enums/api.ts @@ -322,7 +322,7 @@ export const PetApiAxiosParamCreator = function (configuration?: Configuration) // oauth required await setOAuthToObject(localVarHeaderParameter, "petstore_auth", ["write:pets", "read:pets"], configuration) - if (apiKey !== undefined && apiKey !== null) { + if (apiKey != null) { localVarHeaderParameter['api_key'] = String(apiKey); }