Fixed escape of property names

This commit is contained in:
Ferdi Koomen 2020-10-15 00:27:47 +02:00
parent 107895a7fd
commit 6cce73bddc
7 changed files with 348 additions and 295 deletions

View File

@ -0,0 +1,7 @@
import { escapeName } from './escapeName';
describe('escapeName', () => {
it('should escape', () => {
expect(escapeName('')).toEqual('');
});
});

View File

@ -0,0 +1,9 @@
export function escapeName(value: string): string {
if (value) {
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
if (!validName) {
return `'${value}'`;
}
}
return value;
}

View File

@ -1,6 +1,7 @@
import type { Model } from '../../../client/interfaces/Model';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { escapeName } from './escapeName';
import { getComment } from './getComment';
import { getType } from './getType';
@ -16,7 +17,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
if (property.$ref) {
const model = getType(property.$ref);
models.push({
name: propertyName,
name: escapeName(propertyName),
export: 'reference',
type: model.type,
base: model.base,
@ -50,7 +51,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
} else {
const model = getModel(openApi, property);
models.push({
name: propertyName,
name: escapeName(propertyName),
export: model.export,
type: model.type,
base: model.base,

View File

@ -0,0 +1,7 @@
import { escapeName } from './escapeName';
describe('escapeName', () => {
it('should escape', () => {
expect(escapeName('')).toEqual('');
});
});

View File

@ -0,0 +1,9 @@
export function escapeName(value: string): string {
if (value) {
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
if (!validName) {
return `'${value}'`;
}
}
return value;
}

View File

@ -1,6 +1,7 @@
import type { Model } from '../../../client/interfaces/Model';
import type { OpenApi } from '../interfaces/OpenApi';
import type { OpenApiSchema } from '../interfaces/OpenApiSchema';
import { escapeName } from './escapeName';
import { getComment } from './getComment';
import { getType } from './getType';
@ -16,7 +17,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
if (property.$ref) {
const model = getType(property.$ref);
models.push({
name: propertyName,
name: escapeName(propertyName),
export: 'reference',
type: model.type,
base: model.base,
@ -50,7 +51,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema,
} else {
const model = getModel(openApi, property);
models.push({
name: propertyName,
name: escapeName(propertyName),
export: model.export,
type: model.type,
base: model.base,

File diff suppressed because it is too large Load Diff