diff --git a/package.json b/package.json index 9c5564df..f2b99a58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi-typescript-codegen", - "version": "0.9.1", + "version": "0.9.2", "description": "Library that generates Typescript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", diff --git a/src/openApi/v2/parser/getOperationParameterName.ts b/src/openApi/v2/parser/getOperationParameterName.ts index e84069d8..e21e83c8 100644 --- a/src/openApi/v2/parser/getOperationParameterName.ts +++ b/src/openApi/v2/parser/getOperationParameterName.ts @@ -1,5 +1,7 @@ import camelCase from 'camelcase'; +const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; + /** * Replaces any invalid characters from a parameter name. * For example: 'filter.someProperty' becomes 'filterSomeProperty'. @@ -9,5 +11,5 @@ export function getOperationParameterName(value: string): string { .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); - return camelCase(clean); + return camelCase(clean).replace(reservedWords, '_$1'); } diff --git a/src/openApi/v3/parser/getOperationParameterName.ts b/src/openApi/v3/parser/getOperationParameterName.ts index e84069d8..e21e83c8 100644 --- a/src/openApi/v3/parser/getOperationParameterName.ts +++ b/src/openApi/v3/parser/getOperationParameterName.ts @@ -1,5 +1,7 @@ import camelCase from 'camelcase'; +const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g; + /** * Replaces any invalid characters from a parameter name. * For example: 'filter.someProperty' becomes 'filterSomeProperty'. @@ -9,5 +11,5 @@ export function getOperationParameterName(value: string): string { .replace(/^[^a-zA-Z]+/g, '') .replace(/[^\w\-]+/g, '-') .trim(); - return camelCase(clean); + return camelCase(clean).replace(reservedWords, '_$1'); } diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index ca5248f8..d8d81f83 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -951,6 +951,8 @@ export type ModelWithProperties = { boolean?: boolean; reference?: ModelWithString; 'property with space'?: string; + default?: string; + try?: string; readonly '@namespace.string'?: string; readonly '@namespace.integer'?: number; } @@ -1630,6 +1632,12 @@ export const $ModelWithProperties = { 'property with space': { type: 'string', }, + default: { + type: 'string', + }, + try: { + type: 'string', + }, '@namespace.string': { type: 'string', isReadOnly: true, @@ -2059,6 +2067,7 @@ export class ParametersService { * @param parameterPath1 This is the parameter that goes into the path * @param parameterPath2 This is the parameter that goes into the path * @param parameterPath3 This is the parameter that goes into the path + * @param _default This is the parameter with a reserved keyword * @throws ApiError */ public static async callWithWeirdParameterNames( @@ -2069,6 +2078,7 @@ export class ParametersService { parameterPath1?: string, parameterPath2?: string, parameterPath3?: string, + _default?: string, ): Promise { const result = await __request({ method: 'GET', @@ -3363,6 +3373,8 @@ export type ModelWithProperties = { boolean?: boolean; reference?: ModelWithString; 'property with space'?: string; + default?: string; + try?: string; readonly '@namespace.string'?: string; readonly '@namespace.integer'?: number; } @@ -4197,6 +4209,12 @@ export const $ModelWithProperties = { 'property with space': { type: 'string', }, + default: { + type: 'string', + }, + try: { + type: 'string', + }, '@namespace.string': { type: 'string', isReadOnly: true, @@ -4697,6 +4715,7 @@ export class ParametersService { * @param parameterPath1 This is the parameter that goes into the path * @param parameterPath2 This is the parameter that goes into the path * @param parameterPath3 This is the parameter that goes into the path + * @param _default This is the parameter with a reserved keyword * @throws ApiError */ public static async callWithWeirdParameterNames( @@ -4708,6 +4727,7 @@ export class ParametersService { parameterPath1?: string, parameterPath2?: string, parameterPath3?: string, + _default?: string, ): Promise { const result = await __request({ method: 'GET', diff --git a/test/spec/v2.json b/test/spec/v2.json index b7355487..27e01e2b 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -133,6 +133,13 @@ "type": "string", "required": false }, + { + "description": "This is the parameter with a reserved keyword", + "name": "default", + "in": "path", + "type": "string", + "required": false + }, { "description": "This is the parameter that goes into the request header", "name": "parameter.header", @@ -1141,6 +1148,12 @@ "property with space": { "type": "string" }, + "default": { + "type": "string" + }, + "try": { + "type": "string" + }, "@namespace.string": { "type": "string", "readOnly": true diff --git a/test/spec/v3.json b/test/spec/v3.json index 9b139746..55e9c51b 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -173,6 +173,16 @@ "type": "string" } }, + { + "description": "This is the parameter with a reserved keyword", + "name": "default", + "in": "path", + "required": false, + "nullable": false, + "schema": { + "type": "string" + } + }, { "description": "This is the parameter that goes into the request header", "name": "parameter.header", @@ -1804,6 +1814,12 @@ "property with space": { "type": "string" }, + "default": { + "type": "string" + }, + "try": { + "type": "string" + }, "@namespace.string": { "type": "string", "readOnly": true