Skip to content

Commit 405df66

Browse files
committed
- Added options to handle reserved property names
1 parent 8368025 commit 405df66

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.9.1",
3+
"version": "0.9.2",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

src/openApi/v2/parser/getOperationParameterName.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import camelCase from 'camelcase';
22

3+
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;
4+
35
/**
46
* Replaces any invalid characters from a parameter name.
57
* For example: 'filter.someProperty' becomes 'filterSomeProperty'.
@@ -9,5 +11,5 @@ export function getOperationParameterName(value: string): string {
911
.replace(/^[^a-zA-Z]+/g, '')
1012
.replace(/[^\w\-]+/g, '-')
1113
.trim();
12-
return camelCase(clean);
14+
return camelCase(clean).replace(reservedWords, '_$1');
1315
}

src/openApi/v3/parser/getOperationParameterName.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import camelCase from 'camelcase';
22

3+
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;
4+
35
/**
46
* Replaces any invalid characters from a parameter name.
57
* For example: 'filter.someProperty' becomes 'filterSomeProperty'.
@@ -9,5 +11,5 @@ export function getOperationParameterName(value: string): string {
911
.replace(/^[^a-zA-Z]+/g, '')
1012
.replace(/[^\w\-]+/g, '-')
1113
.trim();
12-
return camelCase(clean);
14+
return camelCase(clean).replace(reservedWords, '_$1');
1315
}

test/__snapshots__/index.spec.js.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,8 @@ export type ModelWithProperties = {
951951
boolean?: boolean;
952952
reference?: ModelWithString;
953953
'property with space'?: string;
954+
default?: string;
955+
try?: string;
954956
readonly '@namespace.string'?: string;
955957
readonly '@namespace.integer'?: number;
956958
}
@@ -1630,6 +1632,12 @@ export const $ModelWithProperties = {
16301632
'property with space': {
16311633
type: 'string',
16321634
},
1635+
default: {
1636+
type: 'string',
1637+
},
1638+
try: {
1639+
type: 'string',
1640+
},
16331641
'@namespace.string': {
16341642
type: 'string',
16351643
isReadOnly: true,
@@ -2059,6 +2067,7 @@ export class ParametersService {
20592067
* @param parameterPath1 This is the parameter that goes into the path
20602068
* @param parameterPath2 This is the parameter that goes into the path
20612069
* @param parameterPath3 This is the parameter that goes into the path
2070+
* @param _default This is the parameter with a reserved keyword
20622071
* @throws ApiError
20632072
*/
20642073
public static async callWithWeirdParameterNames(
@@ -2069,6 +2078,7 @@ export class ParametersService {
20692078
parameterPath1?: string,
20702079
parameterPath2?: string,
20712080
parameterPath3?: string,
2081+
_default?: string,
20722082
): Promise<void> {
20732083
const result = await __request({
20742084
method: 'GET',
@@ -3363,6 +3373,8 @@ export type ModelWithProperties = {
33633373
boolean?: boolean;
33643374
reference?: ModelWithString;
33653375
'property with space'?: string;
3376+
default?: string;
3377+
try?: string;
33663378
readonly '@namespace.string'?: string;
33673379
readonly '@namespace.integer'?: number;
33683380
}
@@ -4197,6 +4209,12 @@ export const $ModelWithProperties = {
41974209
'property with space': {
41984210
type: 'string',
41994211
},
4212+
default: {
4213+
type: 'string',
4214+
},
4215+
try: {
4216+
type: 'string',
4217+
},
42004218
'@namespace.string': {
42014219
type: 'string',
42024220
isReadOnly: true,
@@ -4697,6 +4715,7 @@ export class ParametersService {
46974715
* @param parameterPath1 This is the parameter that goes into the path
46984716
* @param parameterPath2 This is the parameter that goes into the path
46994717
* @param parameterPath3 This is the parameter that goes into the path
4718+
* @param _default This is the parameter with a reserved keyword
47004719
* @throws ApiError
47014720
*/
47024721
public static async callWithWeirdParameterNames(
@@ -4708,6 +4727,7 @@ export class ParametersService {
47084727
parameterPath1?: string,
47094728
parameterPath2?: string,
47104729
parameterPath3?: string,
4730+
_default?: string,
47114731
): Promise<void> {
47124732
const result = await __request({
47134733
method: 'GET',

test/spec/v2.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@
133133
"type": "string",
134134
"required": false
135135
},
136+
{
137+
"description": "This is the parameter with a reserved keyword",
138+
"name": "default",
139+
"in": "path",
140+
"type": "string",
141+
"required": false
142+
},
136143
{
137144
"description": "This is the parameter that goes into the request header",
138145
"name": "parameter.header",
@@ -1141,6 +1148,12 @@
11411148
"property with space": {
11421149
"type": "string"
11431150
},
1151+
"default": {
1152+
"type": "string"
1153+
},
1154+
"try": {
1155+
"type": "string"
1156+
},
11441157
"@namespace.string": {
11451158
"type": "string",
11461159
"readOnly": true

test/spec/v3.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@
173173
"type": "string"
174174
}
175175
},
176+
{
177+
"description": "This is the parameter with a reserved keyword",
178+
"name": "default",
179+
"in": "path",
180+
"required": false,
181+
"nullable": false,
182+
"schema": {
183+
"type": "string"
184+
}
185+
},
176186
{
177187
"description": "This is the parameter that goes into the request header",
178188
"name": "parameter.header",
@@ -1804,6 +1814,12 @@
18041814
"property with space": {
18051815
"type": "string"
18061816
},
1817+
"default": {
1818+
"type": "string"
1819+
},
1820+
"try": {
1821+
"type": "string"
1822+
},
18071823
"@namespace.string": {
18081824
"type": "string",
18091825
"readOnly": true

0 commit comments

Comments
 (0)