Skip to content

Commit 6cce73b

Browse files
committed
Fixed escape of property names
1 parent 107895a commit 6cce73b

File tree

7 files changed

+348
-295
lines changed

7 files changed

+348
-295
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { escapeName } from './escapeName';
2+
3+
describe('escapeName', () => {
4+
it('should escape', () => {
5+
expect(escapeName('')).toEqual('');
6+
});
7+
});

src/openApi/v2/parser/escapeName.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function escapeName(value: string): string {
2+
if (value) {
3+
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
4+
if (!validName) {
5+
return `'${value}'`;
6+
}
7+
}
8+
return value;
9+
}

src/openApi/v2/parser/getModelProperties.ts

Lines changed: 3 additions & 2 deletions
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 type { OpenApiSchema } from '../interfaces/OpenApiSchema';
4+
import { escapeName } from './escapeName';
45
import { getComment } from './getComment';
56
import { getType } from './getType';
67

@@ -16,7 +17,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
1617
if (property.$ref) {
1718
const model = getType(property.$ref);
1819
models.push({
19-
name: propertyName,
20+
name: escapeName(propertyName),
2021
export: 'reference',
2122
type: model.type,
2223
base: model.base,
@@ -50,7 +51,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
5051
} else {
5152
const model = getModel(openApi, property);
5253
models.push({
53-
name: propertyName,
54+
name: escapeName(propertyName),
5455
export: model.export,
5556
type: model.type,
5657
base: model.base,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { escapeName } from './escapeName';
2+
3+
describe('escapeName', () => {
4+
it('should escape', () => {
5+
expect(escapeName('')).toEqual('');
6+
});
7+
});

src/openApi/v3/parser/escapeName.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function escapeName(value: string): string {
2+
if (value) {
3+
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
4+
if (!validName) {
5+
return `'${value}'`;
6+
}
7+
}
8+
return value;
9+
}

src/openApi/v3/parser/getModelProperties.ts

Lines changed: 3 additions & 2 deletions
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 type { OpenApiSchema } from '../interfaces/OpenApiSchema';
4+
import { escapeName } from './escapeName';
45
import { getComment } from './getComment';
56
import { getType } from './getType';
67

@@ -16,7 +17,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
1617
if (property.$ref) {
1718
const model = getType(property.$ref);
1819
models.push({
19-
name: propertyName,
20+
name: escapeName(propertyName),
2021
export: 'reference',
2122
type: model.type,
2223
base: model.base,
@@ -50,7 +51,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
5051
} else {
5152
const model = getModel(openApi, property);
5253
models.push({
53-
name: propertyName,
54+
name: escapeName(propertyName),
5455
export: model.export,
5556
type: model.type,
5657
base: model.base,

0 commit comments

Comments
 (0)