Skip to content

Commit e29254b

Browse files
svnvDangoDev
authored andcommitted
Convert names containing spaces to use underscores (#75)
* Convert names containing spaces to use underscores * Only convert interface names * Also handle spaces when defs are used
1 parent dcc2cb5 commit e29254b

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

src/swagger-2.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ function sanitize(name: string): string {
5454
return name.includes('-') ? `'${name}'` : name;
5555
}
5656

57+
function spacesToUnderscores(name: string): string {
58+
return name.replace(/\s/g, '_');
59+
}
60+
5761
function parse(spec: Swagger2, options: Swagger2Options = {}): string {
5862
const shouldUseWrapper = options.wrapper !== false;
5963
const wrapper =
@@ -93,14 +97,15 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
9397

9498
if ($ref) {
9599
const [refName, refProperties] = getRef($ref);
100+
const convertedRefName = spacesToUnderscores(refName);
96101
// If a shallow array interface, return that instead
97102
if (refProperties.items && refProperties.items.$ref) {
98103
return getType(refProperties, refName);
99104
}
100105
if (refProperties.type && TYPES[refProperties.type]) {
101106
return TYPES[refProperties.type];
102107
}
103-
return refName || DEFAULT_TYPE;
108+
return convertedRefName || DEFAULT_TYPE;
104109
}
105110

106111
if (items && items.$ref) {
@@ -172,7 +177,11 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
172177
// Open interface
173178
const isExtending = includes.length ? ` extends ${includes.join(', ')}` : '';
174179

175-
output.push(`export interface ${shouldCamelCase ? camelCase(ID) : ID}${isExtending} {`);
180+
output.push(
181+
`export interface ${
182+
shouldCamelCase ? camelCase(ID) : spacesToUnderscores(ID)
183+
}${isExtending} {`
184+
);
176185

177186
// Populate interface
178187
Object.entries(allProperties).forEach(([key, value]): void => {

tests/swagger-2.test.ts

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function format(
1515
): string {
1616
return prettier.format(
1717
`
18-
${injectWarning ? `${warningMessage} \n` : ''}
19-
${wrapper} {
20-
${spec}
18+
${injectWarning ? `${warningMessage} \n` : ''}
19+
${wrapper} {
20+
${spec}
2121
}
2222
`,
2323
{
@@ -384,6 +384,65 @@ describe('Swagger 2 spec', () => {
384384

385385
expect(swaggerToTS(swagger)).toBe(ts);
386386
});
387+
388+
it('converts names with spaces to names with underscores', () => {
389+
const swagger: Swagger2 = {
390+
definitions: {
391+
'User 1': {
392+
properties: {
393+
'profile_image': { type: 'string' },
394+
'address_line_1': { type: 'string' },
395+
},
396+
type: 'object',
397+
},
398+
'User 1 Being Used': {
399+
properties: {
400+
'user': { $ref: '#/definitions/User 1' },
401+
'user_array': {
402+
type: 'array',
403+
items: { $ref: '#/definitions/User 1' },
404+
},
405+
'all_of_user': {
406+
allOf: [
407+
{ $ref: '#/definitions/User 1' },
408+
{
409+
properties: {
410+
other_field: { type: 'string' },
411+
},
412+
type: 'object',
413+
},
414+
],
415+
type: 'object',
416+
},
417+
'wrapper': {
418+
properties: {
419+
user: { $ref: '#/definitions/User 1' },
420+
},
421+
type: 'object',
422+
}
423+
},
424+
type: 'object',
425+
}
426+
},
427+
};
428+
429+
const ts = format(`
430+
export interface User_1_Being_Used {
431+
user?: User_1;
432+
user_array?: User_1[];
433+
all_of_user?: object;
434+
wrapper?: User1BeingUsedWrapper;
435+
}
436+
export interface User1BeingUsedWrapper {
437+
user?: User_1;
438+
}
439+
export interface User_1 {
440+
'profile_image'?: string;
441+
'address_line_1'?: string;
442+
}`);
443+
444+
expect(swaggerToTS(swagger)).toBe(ts);
445+
});
387446
});
388447

389448
describe('TS features', () => {

0 commit comments

Comments
 (0)