Skip to content

Commit fe22880

Browse files
authored
Merge pull request ferdikoomen#1192 from mb21/keywords
Escape reserved keywords in schema names
2 parents 2a26cb7 + 31ce4a2 commit fe22880

File tree

6 files changed

+75
-51
lines changed

6 files changed

+75
-51
lines changed

src/openApi/v2/parser/getModels.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Model } from '../../../client/interfaces/Model';
2+
import { reservedWords } from '../../v3/parser/getOperationParameterName';
23
import type { OpenApi } from '../interfaces/OpenApi';
34
import { getModel } from './getModel';
45
import { getType } from './getType';
@@ -9,7 +10,7 @@ export const getModels = (openApi: OpenApi): Model[] => {
910
if (openApi.definitions.hasOwnProperty(definitionName)) {
1011
const definition = openApi.definitions[definitionName];
1112
const definitionType = getType(definitionName);
12-
const model = getModel(openApi, definition, true, definitionType.base);
13+
const model = getModel(openApi, definition, true, definitionType.base.replace(reservedWords, '_$1'));
1314
models.push(model);
1415
}
1516
}

src/openApi/v3/parser/getModels.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Model } from '../../../client/interfaces/Model';
22
import type { OpenApi } from '../interfaces/OpenApi';
33
import { getModel } from './getModel';
4+
import { reservedWords } from './getOperationParameterName';
45
import { getType } from './getType';
56

67
export const getModels = (openApi: OpenApi): Model[] => {
@@ -10,7 +11,7 @@ export const getModels = (openApi: OpenApi): Model[] => {
1011
if (openApi.components.schemas.hasOwnProperty(definitionName)) {
1112
const definition = openApi.components.schemas[definitionName];
1213
const definitionType = getType(definitionName);
13-
const model = getModel(openApi, definition, true, definitionType.base);
14+
const model = getModel(openApi, definition, true, definitionType.base.replace(reservedWords, '_$1'));
1415
models.push(model);
1516
}
1617
}

src/openApi/v3/parser/getOperationParameterName.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import camelCase from 'camelcase';
22

3-
const reservedWords =
3+
export const reservedWords =
44
/^(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;
55

66
/**

test/__snapshots__/index.spec.ts.snap

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -549,54 +549,7 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
549549
export { OpenAPI } from './core/OpenAPI';
550550
export type { OpenAPIConfig } from './core/OpenAPI';
551551

552-
export type { ArrayWithArray as ArrayWithArrayDto } from './models/ArrayWithArray';
553-
export type { ArrayWithBooleans as ArrayWithBooleansDto } from './models/ArrayWithBooleans';
554-
export type { ArrayWithNumbers as ArrayWithNumbersDto } from './models/ArrayWithNumbers';
555-
export type { ArrayWithProperties as ArrayWithPropertiesDto } from './models/ArrayWithProperties';
556-
export type { ArrayWithReferences as ArrayWithReferencesDto } from './models/ArrayWithReferences';
557-
export type { ArrayWithStrings as ArrayWithStringsDto } from './models/ArrayWithStrings';
558-
export type { CommentWithBackticks as CommentWithBackticksDto } from './models/CommentWithBackticks';
559-
export type { CommentWithBreaks as CommentWithBreaksDto } from './models/CommentWithBreaks';
560-
export type { CommentWithExpressionPlaceholders as CommentWithExpressionPlaceholdersDto } from './models/CommentWithExpressionPlaceholders';
561-
export type { CommentWithQuotes as CommentWithQuotesDto } from './models/CommentWithQuotes';
562-
export type { CommentWithReservedCharacters as CommentWithReservedCharactersDto } from './models/CommentWithReservedCharacters';
563-
export type { CommentWithSlashes as CommentWithSlashesDto } from './models/CommentWithSlashes';
564-
export type { Date as DateDto } from './models/Date';
565-
export type { DictionaryWithArray as DictionaryWithArrayDto } from './models/DictionaryWithArray';
566-
export type { DictionaryWithDictionary as DictionaryWithDictionaryDto } from './models/DictionaryWithDictionary';
567-
export type { DictionaryWithProperties as DictionaryWithPropertiesDto } from './models/DictionaryWithProperties';
568-
export type { DictionaryWithReference as DictionaryWithReferenceDto } from './models/DictionaryWithReference';
569-
export type { DictionaryWithString as DictionaryWithStringDto } from './models/DictionaryWithString';
570-
export type { EnumFromDescription as EnumFromDescriptionDto } from './models/EnumFromDescription';
571-
export { EnumWithExtensions as EnumWithExtensionsDto } from './models/EnumWithExtensions';
572-
export { EnumWithNumbers as EnumWithNumbersDto } from './models/EnumWithNumbers';
573-
export { EnumWithStrings as EnumWithStringsDto } from './models/EnumWithStrings';
574-
export type { ModelThatExtends as ModelThatExtendsDto } from './models/ModelThatExtends';
575-
export type { ModelThatExtendsExtends as ModelThatExtendsExtendsDto } from './models/ModelThatExtendsExtends';
576-
export type { ModelWithArray as ModelWithArrayDto } from './models/ModelWithArray';
577-
export type { ModelWithBoolean as ModelWithBooleanDto } from './models/ModelWithBoolean';
578-
export type { ModelWithCircularReference as ModelWithCircularReferenceDto } from './models/ModelWithCircularReference';
579-
export type { ModelWithDictionary as ModelWithDictionaryDto } from './models/ModelWithDictionary';
580-
export type { ModelWithDuplicateImports as ModelWithDuplicateImportsDto } from './models/ModelWithDuplicateImports';
581-
export type { ModelWithDuplicateProperties as ModelWithDuplicatePropertiesDto } from './models/ModelWithDuplicateProperties';
582-
export { ModelWithEnum as ModelWithEnumDto } from './models/ModelWithEnum';
583-
export type { ModelWithEnumFromDescription as ModelWithEnumFromDescriptionDto } from './models/ModelWithEnumFromDescription';
584-
export type { ModelWithInteger as ModelWithIntegerDto } from './models/ModelWithInteger';
585-
export type { ModelWithNestedEnums as ModelWithNestedEnumsDto } from './models/ModelWithNestedEnums';
586-
export type { ModelWithNestedProperties as ModelWithNestedPropertiesDto } from './models/ModelWithNestedProperties';
587-
export type { ModelWithNullableString as ModelWithNullableStringDto } from './models/ModelWithNullableString';
588-
export type { ModelWithOrderedProperties as ModelWithOrderedPropertiesDto } from './models/ModelWithOrderedProperties';
589-
export type { ModelWithPattern as ModelWithPatternDto } from './models/ModelWithPattern';
590-
export type { ModelWithProperties as ModelWithPropertiesDto } from './models/ModelWithProperties';
591-
export type { ModelWithReference as ModelWithReferenceDto } from './models/ModelWithReference';
592-
export type { ModelWithString as ModelWithStringDto } from './models/ModelWithString';
593-
export type { SimpleBoolean as SimpleBooleanDto } from './models/SimpleBoolean';
594-
export type { SimpleFile as SimpleFileDto } from './models/SimpleFile';
595-
export type { SimpleInteger as SimpleIntegerDto } from './models/SimpleInteger';
596-
export type { SimpleReference as SimpleReferenceDto } from './models/SimpleReference';
597-
export type { SimpleString as SimpleStringDto } from './models/SimpleString';
598-
export type { SimpleStringWithPattern as SimpleStringWithPatternDto } from './models/SimpleStringWithPattern';
599-
552+
export { $_default } from './schemas/$_default';
600553
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
601554
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
602555
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
@@ -664,6 +617,18 @@ export { TypesService } from './services/TypesService';
664617
"
665618
`;
666619

620+
exports[`v2 should generate: ./test/generated/v2/models/_default.ts 1`] = `
621+
"/* istanbul ignore file */
622+
/* tslint:disable */
623+
/* eslint-disable */
624+
625+
export type _default = {
626+
name?: string;
627+
};
628+
629+
"
630+
`;
631+
667632
exports[`v2 should generate: ./test/generated/v2/models/ArrayWithArray.ts 1`] = `
668633
"/* istanbul ignore file */
669634
/* tslint:disable */
@@ -1441,6 +1406,20 @@ export type SimpleStringWithPattern = string;
14411406
"
14421407
`;
14431408

1409+
exports[`v2 should generate: ./test/generated/v2/schemas/$_default.ts 1`] = `
1410+
"/* istanbul ignore file */
1411+
/* tslint:disable */
1412+
/* eslint-disable */
1413+
export const $_default = {
1414+
properties: {
1415+
name: {
1416+
type: 'string',
1417+
},
1418+
},
1419+
} as const;
1420+
"
1421+
`;
1422+
14441423
exports[`v2 should generate: ./test/generated/v2/schemas/$ArrayWithArray.ts 1`] = `
14451424
"/* istanbul ignore file */
14461425
/* tslint:disable */
@@ -3614,6 +3593,7 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
36143593
export { OpenAPI } from './core/OpenAPI';
36153594
export type { OpenAPIConfig } from './core/OpenAPI';
36163595

3596+
36173597
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
36183598
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
36193599
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
@@ -3706,6 +3686,18 @@ export { UploadService } from './services/UploadService';
37063686
"
37073687
`;
37083688

3689+
exports[`v3 should generate: ./test/generated/v3/models/_default.ts 1`] = `
3690+
"/* istanbul ignore file */
3691+
/* tslint:disable */
3692+
/* eslint-disable */
3693+
3694+
export type _default = {
3695+
name?: string;
3696+
};
3697+
3698+
"
3699+
`;
3700+
37093701
exports[`v3 should generate: ./test/generated/v3/models/ArrayWithArray.ts 1`] = `
37103702
"/* istanbul ignore file */
37113703
/* tslint:disable */
@@ -4831,6 +4823,20 @@ export type SimpleStringWithPattern = string | null;
48314823
"
48324824
`;
48334825

4826+
exports[`v3 should generate: ./test/generated/v3/schemas/$_default.ts 1`] = `
4827+
"/* istanbul ignore file */
4828+
/* tslint:disable */
4829+
/* eslint-disable */
4830+
export const $_default = {
4831+
properties: {
4832+
name: {
4833+
type: 'string',
4834+
},
4835+
},
4836+
} as const;
4837+
"
4838+
`;
4839+
48344840
exports[`v3 should generate: ./test/generated/v3/schemas/$ArrayWithArray.ts 1`] = `
48354841
"/* istanbul ignore file */
48364842
/* tslint:disable */

test/spec/v2.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,14 @@
14661466
}
14671467
]
14681468
},
1469+
"default": {
1470+
"type": "object",
1471+
"properties": {
1472+
"name": {
1473+
"type": "string"
1474+
}
1475+
}
1476+
},
14691477
"ModelWithPattern": {
14701478
"description": "This is a model that contains a some patterns",
14711479
"type": "object",

test/spec/v3.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,14 @@
25112511
}
25122512
}
25132513
},
2514+
"default": {
2515+
"type": "object",
2516+
"properties": {
2517+
"name": {
2518+
"type": "string"
2519+
}
2520+
}
2521+
},
25142522
"Pageable": {
25152523
"type": "object",
25162524
"properties": {

0 commit comments

Comments
 (0)