Skip to content

Commit 5d140cb

Browse files
committed
Escape reserved keywords in schema names for v2, closes ferdikoomen#1186
1 parent 05db3b9 commit 5d140cb

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
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
}

test/__snapshots__/index.spec.ts.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
548548
export { OpenAPI } from './core/OpenAPI';
549549
export type { OpenAPIConfig } from './core/OpenAPI';
550550

551+
export type { _default } from './models/_default';
551552
export type { ArrayWithArray } from './models/ArrayWithArray';
552553
export type { ArrayWithBooleans } from './models/ArrayWithBooleans';
553554
export type { ArrayWithNumbers } from './models/ArrayWithNumbers';
@@ -596,6 +597,7 @@ export type { SimpleReference } from './models/SimpleReference';
596597
export type { SimpleString } from './models/SimpleString';
597598
export type { SimpleStringWithPattern } from './models/SimpleStringWithPattern';
598599

600+
export { $_default } from './schemas/$_default';
599601
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
600602
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
601603
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
@@ -663,6 +665,18 @@ export { TypesService } from './services/TypesService';
663665
"
664666
`;
665667

668+
exports[`v2 should generate: ./test/generated/v2/models/_default.ts 1`] = `
669+
"/* istanbul ignore file */
670+
/* tslint:disable */
671+
/* eslint-disable */
672+
673+
export type _default = {
674+
name?: string;
675+
};
676+
677+
"
678+
`;
679+
666680
exports[`v2 should generate: ./test/generated/v2/models/ArrayWithArray.ts 1`] = `
667681
"/* istanbul ignore file */
668682
/* tslint:disable */
@@ -1439,6 +1453,20 @@ export type SimpleStringWithPattern = string;
14391453
"
14401454
`;
14411455

1456+
exports[`v2 should generate: ./test/generated/v2/schemas/$_default.ts 1`] = `
1457+
"/* istanbul ignore file */
1458+
/* tslint:disable */
1459+
/* eslint-disable */
1460+
export const $_default = {
1461+
properties: {
1462+
name: {
1463+
type: 'string',
1464+
},
1465+
},
1466+
} as const;
1467+
"
1468+
`;
1469+
14421470
exports[`v2 should generate: ./test/generated/v2/schemas/$ArrayWithArray.ts 1`] = `
14431471
"/* istanbul ignore file */
14441472
/* 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",

0 commit comments

Comments
 (0)