Skip to content

Commit ddfb6a1

Browse files
authored
Merge pull request #129 from manifoldco/dangodev/fix-camelcase
Fix camelcased `$ref`s
2 parents 51ac917 + 615a993 commit ddfb6a1

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/swagger-2.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
7979
}
8080

8181
// Returns primitive type, or 'object' or 'any'
82-
function getType(definition: Swagger2Definition, nestedName: string): string {
82+
function getType(
83+
definition: Swagger2Definition,
84+
nestedName: string,
85+
getTypeOptions: { camelcase: boolean }
86+
): string {
8387
const { $ref, items, type, ...value } = definition;
8488

8589
const nextInterface = camelCase(nestedName); // if this becomes an interface, it’ll need to be camelCased
@@ -88,10 +92,13 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
8892

8993
if ($ref) {
9094
const [refName, refProperties] = getRef($ref);
91-
const convertedRefName = spacesToUnderscores(refName);
95+
let convertedRefName = spacesToUnderscores(refName);
96+
if (options && options.camelcase === true) {
97+
convertedRefName = camelCase(convertedRefName);
98+
}
9299
// If a shallow array interface, return that instead
93100
if (refProperties.items && refProperties.items.$ref) {
94-
return getType(refProperties, refName);
101+
return getType(refProperties, refName, getTypeOptions);
95102
}
96103
if (refProperties.type && PRIMITIVE[refProperties.type]) {
97104
return PRIMITIVE[refProperties.type];
@@ -101,13 +108,13 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
101108

102109
if (items && items.$ref) {
103110
const [refName] = getRef(items.$ref);
104-
return `${getType(items, refName)}[]`;
111+
return `${getType(items, refName, getTypeOptions)}[]`;
105112
}
106113

107114
if (items) {
108115
// if an array, keep nesting
109116
if (items.type === 'array') {
110-
return `${getType(items, nestedName)}[]`;
117+
return `${getType(items, nestedName, getTypeOptions)}[]`;
111118
}
112119
// else if primitive, return type
113120
if (items.type && PRIMITIVE[items.type]) {
@@ -119,7 +126,7 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
119126
}
120127

121128
if (Array.isArray(value.oneOf)) {
122-
return value.oneOf.map((def): string => getType(def, '')).join(' | ');
129+
return value.oneOf.map((def): string => getType(def, '', getTypeOptions)).join(' | ');
123130
}
124131

125132
if (value.properties) {
@@ -180,7 +187,7 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
180187
const newID = `${ID}${capitalize(formattedKey)}`;
181188
const interfaceType = Array.isArray(value.enum)
182189
? ` ${value.enum.map(option => JSON.stringify(option)).join(' | ')}` // Handle enums in the same definition
183-
: getType(value, newID);
190+
: getType(value, newID, { camelcase: shouldCamelCase });
184191

185192
let property: Property = {
186193
interfaceType,
@@ -205,7 +212,9 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
205212
}
206213

207214
if ((additionalProperties as Swagger2Definition).type) {
208-
const interfaceType = getType(additionalProperties as Swagger2Definition, '');
215+
const interfaceType = getType(additionalProperties as Swagger2Definition, '', {
216+
camelcase: shouldCamelCase,
217+
});
209218
output.push(`[name: string]: ${interfaceType}`);
210219
}
211220
}

tests/swagger-2.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,31 @@ describe('Swagger 2 spec', () => {
362362
it('converts snake_case to camelCase if specified', () => {
363363
const swagger: Swagger2 = {
364364
definitions: {
365+
User_Team: {
366+
properties: {
367+
id: { type: 'string' },
368+
},
369+
type: 'object',
370+
},
365371
User: {
366372
properties: {
367373
profile_image: { type: 'string' },
368374
address_line_1: { type: 'string' },
375+
user_team: { $ref: '#/definitions/User_Team' },
369376
},
370377
type: 'object',
371378
},
372379
},
373380
};
374381

375382
const ts = format(`
383+
export interface UserTeam {
384+
id?: string;
385+
}
376386
export interface User {
377387
profileImage?: string;
378388
addressLine1?: string;
389+
userTeam?: UserTeam;
379390
}`);
380391

381392
expect(swaggerToTS(swagger, { camelcase: true })).toBe(ts);

0 commit comments

Comments
 (0)