From e6b15b2d85d369d977d28279e39b9a6ced456c8f Mon Sep 17 00:00:00 2001 From: Domagoj Zivanovic Date: Tue, 9 Dec 2025 14:59:04 +0100 Subject: [PATCH 1/4] Adds a configuration parameter for the generated ContentType enum to avoid clashing --- src/configuration.ts | 2 ++ templates/base/http-clients/fetch-http-client.ejs | 2 +- types/index.ts | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/configuration.ts b/src/configuration.ts index ea3f48b3..1c2856bd 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -180,6 +180,8 @@ export class CodeGenConfig { successResponseStatusRange = [200, 299]; + contentTypeEnumName = 'ContentType' + extractingOptions: Partial = { requestBodySuffix: ["Payload", "Body", "Input"], requestParamsSuffix: ["Params"], diff --git a/templates/base/http-clients/fetch-http-client.ejs b/templates/base/http-clients/fetch-http-client.ejs index 9f6a5c30..39505050 100644 --- a/templates/base/http-clients/fetch-http-client.ejs +++ b/templates/base/http-clients/fetch-http-client.ejs @@ -41,7 +41,7 @@ export interface HttpResponse ex type CancelToken = Symbol | string | number; -export enum ContentType { +export enum <%~ config.contentTypeEnumName %> { Json = "application/json", JsonApi = "application/vnd.api+json", FormData = "multipart/form-data", diff --git a/types/index.ts b/types/index.ts index 48f5632f..7a302f16 100644 --- a/types/index.ts +++ b/types/index.ts @@ -674,6 +674,10 @@ export interface GenerateApiConfiguration { extractingOptions: Partial; /** update configuration object during generation */ update: (update: Partial) => void; + /** name for the generated ContentType enum + * @default ContentType + */ + contentTypeEnumName: string }; modelTypes: ModelType[]; hasFormDataRoutes: boolean; From 4a070d716e69bcea28abcea5af44e14f35ad4896 Mon Sep 17 00:00:00 2001 From: Domagoj Zivanovic Date: Tue, 9 Dec 2025 15:03:31 +0100 Subject: [PATCH 2/4] Adds changeset --- .changeset/shy-humans-pay.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shy-humans-pay.md diff --git a/.changeset/shy-humans-pay.md b/.changeset/shy-humans-pay.md new file mode 100644 index 00000000..33e805f2 --- /dev/null +++ b/.changeset/shy-humans-pay.md @@ -0,0 +1,5 @@ +--- +"swagger-typescript-api": patch +--- + +Add a configuration parameter for the generated ContentType enum to avoid name clashing From 98e289451f247221a57f1ab9696b077e958f5652 Mon Sep 17 00:00:00 2001 From: Domagoj Zivanovic Date: Tue, 9 Dec 2025 15:04:36 +0100 Subject: [PATCH 3/4] Fixes formatting --- src/configuration.ts | 2 +- types/index.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/configuration.ts b/src/configuration.ts index 1c2856bd..1e8716a0 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -180,7 +180,7 @@ export class CodeGenConfig { successResponseStatusRange = [200, 299]; - contentTypeEnumName = 'ContentType' + contentTypeEnumName = "ContentType"; extractingOptions: Partial = { requestBodySuffix: ["Payload", "Body", "Input"], diff --git a/types/index.ts b/types/index.ts index 7a302f16..a5209ea1 100644 --- a/types/index.ts +++ b/types/index.ts @@ -674,10 +674,10 @@ export interface GenerateApiConfiguration { extractingOptions: Partial; /** update configuration object during generation */ update: (update: Partial) => void; - /** name for the generated ContentType enum + /** name for the generated ContentType enum * @default ContentType - */ - contentTypeEnumName: string + */ + contentTypeEnumName: string; }; modelTypes: ModelType[]; hasFormDataRoutes: boolean; From 667c8a35d669fdbf5e4bcea8ef5a83175a8a04a3 Mon Sep 17 00:00:00 2001 From: Domagoj Zivanovic Date: Thu, 11 Dec 2025 12:06:17 +0100 Subject: [PATCH 4/4] Fixes ContentType in other templates --- .../base/http-clients/axios-http-client.ejs | 8 ++++---- .../base/http-clients/fetch-http-client.ejs | 18 +++++++++--------- templates/default/procedure-call.ejs | 15 +++++++++------ templates/modular/procedure-call.ejs | 15 +++++++++------ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/templates/base/http-clients/axios-http-client.ejs b/templates/base/http-clients/axios-http-client.ejs index bc77f346..52501dc5 100644 --- a/templates/base/http-clients/axios-http-client.ejs +++ b/templates/base/http-clients/axios-http-client.ejs @@ -13,7 +13,7 @@ export interface FullRequestParams extends Omit; /** query params */ query?: QueryParamsType; /** format of response (i.e. response.json() -> format: "json") */ @@ -30,7 +30,7 @@ export interface ApiConfig extends Omit { Json = "application/json", JsonApi = "application/vnd.api+json", FormData = "multipart/form-data", @@ -116,11 +116,11 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const responseFormat = (format || this.format) || undefined; - if (type === ContentType.FormData && body && body !== null && typeof body === "object") { + if (type === <%~ config.contentTypeEnumName %>.FormData && body && body !== null && typeof body === "object") { body = this.createFormData(body as Record); } - if (type === ContentType.Text && body && body !== null && typeof body !== "string") { + if (type === <%~ config.contentTypeEnumName %>.Text && body && body !== null && typeof body !== "string") { body = JSON.stringify(body); } diff --git a/templates/base/http-clients/fetch-http-client.ejs b/templates/base/http-clients/fetch-http-client.ejs index 39505050..d9734b6e 100644 --- a/templates/base/http-clients/fetch-http-client.ejs +++ b/templates/base/http-clients/fetch-http-client.ejs @@ -11,7 +11,7 @@ export interface FullRequestParams extends Omit { /** request path */ path: string; /** content type of request body */ - type?: ContentType; + type?: <%~ config.contentTypeEnumName %>; /** query params */ query?: QueryParamsType; /** format of response (i.e. response.json() -> format: "json") */ @@ -102,11 +102,11 @@ export class HttpClient { return queryString ? `?${queryString}` : ""; } - private contentFormatters: Record any> = { - [ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, - [ContentType.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, - [ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input, - [ContentType.FormData]: (input: any) => { + private contentFormatters: Record<<%~ config.contentTypeEnumName %>, (input: any) => any> = { + [<%~ config.contentTypeEnumName %>.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, + [<%~ config.contentTypeEnumName %>.JsonApi]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input, + [<%~ config.contentTypeEnumName %>.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input, + [<%~ config.contentTypeEnumName %>.FormData]: (input: any) => { if (input instanceof FormData) { return input; } @@ -124,7 +124,7 @@ export class HttpClient { return formData; }, new FormData()); }, - [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), + [<%~ config.contentTypeEnumName %>.UrlEncoded]: (input: any) => this.toQueryString(input), } protected mergeRequestParams(params1: RequestParams, params2?: RequestParams): RequestParams { @@ -181,7 +181,7 @@ export class HttpClient { const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {}; const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); - const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const payloadFormatter = this.contentFormatters[type || <%~ config.contentTypeEnumName %>.Json]; const responseFormat = format || requestParams.format; return this.customFetch( @@ -190,7 +190,7 @@ export class HttpClient { ...requestParams, headers: { ...(requestParams.headers || {}), - ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), + ...(type && type !== <%~ config.contentTypeEnumName %>.FormData ? { "Content-Type": type } : {}), }, signal: (cancelToken ? this.createAbortSignal(cancelToken) : requestParams.signal) || null, body: typeof body === "undefined" || body === null ? null : payloadFormatter(body), diff --git a/templates/default/procedure-call.ejs b/templates/default/procedure-call.ejs index 90af47bd..8234d35f 100644 --- a/templates/default/procedure-call.ejs +++ b/templates/default/procedure-call.ejs @@ -45,14 +45,17 @@ const wrapperArgs = _ .map(argToTmpl) .join(', ') +const enumName = config.contentTypeEnumName; + // RequestParams["type"] const requestContentKind = { - "JSON": "ContentType.Json", - "JSON_API": "ContentType.JsonApi", - "URL_ENCODED": "ContentType.UrlEncoded", - "FORM_DATA": "ContentType.FormData", - "TEXT": "ContentType.Text", -} + JSON: `${enumName}.Json`, + JSON_API: `${enumName}.JsonApi`, + URL_ENCODED: `${enumName}.UrlEncoded`, + FORM_DATA: `${enumName}.FormData`, + TEXT: `${enumName}.Text`, +}; + // RequestParams["format"] const responseContentKind = { "JSON": '"json"', diff --git a/templates/modular/procedure-call.ejs b/templates/modular/procedure-call.ejs index 8a95a5c0..a090015a 100644 --- a/templates/modular/procedure-call.ejs +++ b/templates/modular/procedure-call.ejs @@ -45,14 +45,17 @@ const wrapperArgs = _ .map(argToTmpl) .join(', ') +const enumName = config.contentTypeEnumName; + // RequestParams["type"] const requestContentKind = { - "JSON": "ContentType.Json", - "JSON_API": "ContentType.JsonApi", - "URL_ENCODED": "ContentType.UrlEncoded", - "FORM_DATA": "ContentType.FormData", - "TEXT": "ContentType.Text", -} + JSON: `${enumName}.Json`, + JSON_API: `${enumName}.JsonApi`, + URL_ENCODED: `${enumName}.UrlEncoded`, + FORM_DATA: `${enumName}.FormData`, + TEXT: `${enumName}.Text`, +}; + // RequestParams["format"] const responseContentKind = { "JSON": '"json"',