Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/swagger-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function sanitize(name: string): string {
return name.includes('-') ? `'${name}'` : name;
}

function spacesToUnderscores(name: string): string {
return name.replace(/\s/g, '_');
}

function parse(spec: Swagger2, options: Swagger2Options = {}): string {
const shouldUseWrapper = options.wrapper !== false;
const wrapper =
Expand Down Expand Up @@ -93,14 +97,15 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {

if ($ref) {
const [refName, refProperties] = getRef($ref);
const convertedRefName = spacesToUnderscores(refName);
// If a shallow array interface, return that instead
if (refProperties.items && refProperties.items.$ref) {
return getType(refProperties, refName);
}
if (refProperties.type && TYPES[refProperties.type]) {
return TYPES[refProperties.type];
}
return refName || DEFAULT_TYPE;
return convertedRefName || DEFAULT_TYPE;
}

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

output.push(`export interface ${shouldCamelCase ? camelCase(ID) : ID}${isExtending} {`);
output.push(
`export interface ${
shouldCamelCase ? camelCase(ID) : spacesToUnderscores(ID)
}${isExtending} {`
);

// Populate interface
Object.entries(allProperties).forEach(([key, value]): void => {
Expand Down
65 changes: 62 additions & 3 deletions tests/swagger-2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function format(
): string {
return prettier.format(
`
${injectWarning ? `${warningMessage} \n` : ''}
${wrapper} {
${spec}
${injectWarning ? `${warningMessage} \n` : ''}
${wrapper} {
${spec}
}
`,
{
Expand Down Expand Up @@ -384,6 +384,65 @@ describe('Swagger 2 spec', () => {

expect(swaggerToTS(swagger)).toBe(ts);
});

it('converts names with spaces to names with underscores', () => {
const swagger: Swagger2 = {
definitions: {
'User 1': {
properties: {
'profile_image': { type: 'string' },
'address_line_1': { type: 'string' },
},
type: 'object',
},
'User 1 Being Used': {
properties: {
'user': { $ref: '#/definitions/User 1' },
'user_array': {
type: 'array',
items: { $ref: '#/definitions/User 1' },
},
'all_of_user': {
allOf: [
{ $ref: '#/definitions/User 1' },
{
properties: {
other_field: { type: 'string' },
},
type: 'object',
},
],
type: 'object',
},
'wrapper': {
properties: {
user: { $ref: '#/definitions/User 1' },
},
type: 'object',
}
},
type: 'object',
}
},
};

const ts = format(`
export interface User_1_Being_Used {
user?: User_1;
user_array?: User_1[];
all_of_user?: object;
wrapper?: User1BeingUsedWrapper;
}
export interface User1BeingUsedWrapper {
user?: User_1;
}
export interface User_1 {
'profile_image'?: string;
'address_line_1'?: string;
}`);

expect(swaggerToTS(swagger)).toBe(ts);
});
});

describe('TS features', () => {
Expand Down