Skip to content

Commit 92a2b8b

Browse files
authored
Fix nested arrays (#54)
1 parent c17a379 commit 92a2b8b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/swagger-2.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
9494
const [refName] = getRef(items.$ref);
9595
return `${getType(items, refName)}[]`;
9696
}
97+
9798
if (items && items.type) {
99+
// if an array, keep nesting
100+
if (items.type === 'array') {
101+
return `${getType(items, nestedName)}[]`;
102+
}
103+
// else if primitive, return type
98104
if (TYPES[items.type]) {
99105
return `${TYPES[items.type]}[]`;
100106
}
101-
// If this is an array of items, let’s add it to the stack for later
107+
// otherwise if this is an array of nested types, return that interface for later
102108
queue.push([nextInterface, items]);
103109
return `${nextInterface}[]`;
104110
}

tests/swagger-2.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,33 @@ describe('Swagger 2 spec', () => {
197197
expect(swaggerToTS(swagger)).toBe(ts);
198198
});
199199

200+
it('handles arrays of arrays of arrays', () => {
201+
const swagger: Swagger2 = {
202+
definitions: {
203+
Resource: {
204+
properties: {
205+
environments: {
206+
type: 'array',
207+
items: {
208+
type: 'array',
209+
items: { type: 'array', items: { type: 'string' } },
210+
},
211+
},
212+
},
213+
type: 'object',
214+
},
215+
},
216+
};
217+
218+
const ts = format(`
219+
export interface Resource {
220+
environments?: string[][][];
221+
}
222+
`);
223+
224+
expect(swaggerToTS(swagger)).toBe(ts);
225+
});
226+
200227
it('handles allOf', () => {
201228
const swagger: Swagger2 = {
202229
definitions: {

0 commit comments

Comments
 (0)