@@ -79,7 +79,11 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
79
79
}
80
80
81
81
// 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 {
83
87
const { $ref, items, type, ...value } = definition ;
84
88
85
89
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 {
88
92
89
93
if ( $ref ) {
90
94
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
+ }
92
99
// If a shallow array interface, return that instead
93
100
if ( refProperties . items && refProperties . items . $ref ) {
94
- return getType ( refProperties , refName ) ;
101
+ return getType ( refProperties , refName , getTypeOptions ) ;
95
102
}
96
103
if ( refProperties . type && PRIMITIVE [ refProperties . type ] ) {
97
104
return PRIMITIVE [ refProperties . type ] ;
@@ -101,13 +108,13 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
101
108
102
109
if ( items && items . $ref ) {
103
110
const [ refName ] = getRef ( items . $ref ) ;
104
- return `${ getType ( items , refName ) } []` ;
111
+ return `${ getType ( items , refName , getTypeOptions ) } []` ;
105
112
}
106
113
107
114
if ( items ) {
108
115
// if an array, keep nesting
109
116
if ( items . type === 'array' ) {
110
- return `${ getType ( items , nestedName ) } []` ;
117
+ return `${ getType ( items , nestedName , getTypeOptions ) } []` ;
111
118
}
112
119
// else if primitive, return type
113
120
if ( items . type && PRIMITIVE [ items . type ] ) {
@@ -119,7 +126,7 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
119
126
}
120
127
121
128
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 ( ' | ' ) ;
123
130
}
124
131
125
132
if ( value . properties ) {
@@ -180,7 +187,7 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
180
187
const newID = `${ ID } ${ capitalize ( formattedKey ) } ` ;
181
188
const interfaceType = Array . isArray ( value . enum )
182
189
? ` ${ value . enum . map ( option => JSON . stringify ( option ) ) . join ( ' | ' ) } ` // Handle enums in the same definition
183
- : getType ( value , newID ) ;
190
+ : getType ( value , newID , { camelcase : shouldCamelCase } ) ;
184
191
185
192
let property : Property = {
186
193
interfaceType,
@@ -205,7 +212,9 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
205
212
}
206
213
207
214
if ( ( additionalProperties as Swagger2Definition ) . type ) {
208
- const interfaceType = getType ( additionalProperties as Swagger2Definition , '' ) ;
215
+ const interfaceType = getType ( additionalProperties as Swagger2Definition , '' , {
216
+ camelcase : shouldCamelCase ,
217
+ } ) ;
209
218
output . push ( `[name: string]: ${ interfaceType } ` ) ;
210
219
}
211
220
}
0 commit comments