Skip to content

Commit ceb47ca

Browse files
authored
Improve array and object handling (#226)
1 parent 97a296d commit ceb47ca

File tree

6 files changed

+43
-18
lines changed

6 files changed

+43
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@manifoldco/swagger-to-ts",
33
"description": "Generate TypeScript types from Swagger OpenAPI specs",
44
"main": "dist/cjs",
5-
"version": "2.0.0-alpha.5",
5+
"version": "2.0.0-alpha.6",
66
"engines": {
77
"node": ">= 10.0.0"
88
},

src/types/OpenAPI2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ export interface OpenAPI2SchemaObject {
3939
properties?: { [index: string]: OpenAPI2SchemaObject | OpenAPI2Reference };
4040
required?: string[];
4141
title?: string;
42-
type?: OpenAPI2Type;
42+
type?: OpenAPI2Type; // allow this to be optional to cover cases when this is missing
4343
[key: string]: any; // allow arbitrary x-something properties
4444
}

src/types/OpenAPI3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ export interface OpenAPI3SchemaObject {
3636
properties?: { [key: string]: OpenAPI3SchemaObject | OpenAPI3Reference };
3737
required?: string[];
3838
title?: string;
39-
type: OpenAPI3Type;
39+
type?: OpenAPI3Type; // allow this to be optional to cover cases when this is missing
4040
[key: string]: any; // allow arbitrary x-something properties
4141
}

src/utils.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,13 @@ export function nodeType(obj: any): SchemaObjectType | undefined {
6666
return "oneOf";
6767
}
6868

69-
// object
70-
if (
71-
obj.type === "object" ||
72-
!!obj.properties ||
73-
!!obj.allOf ||
74-
!!obj.additionalProperties
75-
) {
76-
return "object";
77-
}
78-
7969
// array
80-
if (obj.type === "array") {
70+
if (obj.type === "array" || obj.items) {
8171
return "array";
8272
}
8373

84-
// other / unknown
85-
return obj.type;
74+
// return object by default
75+
return "object";
8676
}
8777

8878
/** Return OpenAPI version from definition */

tests/v2/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ describe("transformation", () => {
160160
type: "object",
161161
},
162162
object_unknown: { type: "object" },
163+
object_empty: {},
163164
},
164165
};
165166
expect(swaggerToTS(schema)).toBe(
@@ -172,6 +173,7 @@ describe("transformation", () => {
172173
};
173174
object_ref: { number?: number };
174175
object_unknown: { [key: string]: any };
176+
object_empty: { [key: string]: any };
175177
}`)
176178
);
177179
});
@@ -192,6 +194,9 @@ describe("transformation", () => {
192194
},
193195
type: "object",
194196
},
197+
inferred_array: {
198+
items: { $ref: "#/definitions/array" },
199+
},
195200
string: { type: "string" },
196201
array_ref: { items: { $ref: "#/definitions/array" }, type: "array" },
197202
},
@@ -206,6 +211,7 @@ describe("transformation", () => {
206211
numbers?: number[];
207212
refs?: definitions['string'][];
208213
};
214+
inferred_array: definitions['array'][];
209215
string: string;
210216
array_ref: definitions['array'][];
211217
}`)

tests/v3/index.test.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ describe("types", () => {
140140
type: "object",
141141
},
142142
object_unknown: { type: "object" },
143+
object_empty: {},
143144
},
144145
},
145146
};
@@ -154,6 +155,7 @@ describe("types", () => {
154155
};
155156
object_ref: { number?: number };
156157
object_unknown: { [key: string]: any };
158+
object_empty: { [key: string]: any };
157159
}
158160
}`)
159161
);
@@ -179,6 +181,9 @@ describe("types", () => {
179181
},
180182
type: "object",
181183
},
184+
inferred_array: {
185+
items: { $ref: "#/components/schemas/array" },
186+
},
182187
string: { type: "string" },
183188
array_ref: {
184189
items: { $ref: "#/components/schemas/array" },
@@ -198,6 +203,7 @@ describe("types", () => {
198203
numbers?: number[];
199204
refs?: components['schemas']['string'][];
200205
};
206+
inferred_array: components['schemas']['array'][];
201207
string: string;
202208
array_ref: components['schemas']['array'][];
203209
}
@@ -377,7 +383,7 @@ describe("OpenAPI3 features", () => {
377383
oneOf: [
378384
{ type: "string" },
379385
{ type: "number" },
380-
{ $ref: "#/components/one_of_ref" },
386+
{ $ref: "#/components/schemas/one_of_ref" },
381387
],
382388
},
383389
},
@@ -389,6 +395,26 @@ describe("OpenAPI3 features", () => {
389395
},
390396
type: "object",
391397
},
398+
one_of_inferred: {
399+
oneOf: [
400+
{
401+
properties: {
402+
kibana: {
403+
type: "object",
404+
properties: { versions: { type: "string" } },
405+
},
406+
},
407+
},
408+
{
409+
properties: {
410+
elasticsearch: {
411+
type: "object",
412+
properties: { versions: { type: "string" } },
413+
},
414+
},
415+
},
416+
],
417+
},
392418
},
393419
},
394420
};
@@ -397,8 +423,11 @@ describe("OpenAPI3 features", () => {
397423
format(`
398424
export interface components {
399425
schemas: {
400-
one_of: { options?: string | number | components['one_of_ref'] };
426+
one_of: { options?: string | number | components['schemas']['one_of_ref'] };
401427
one_of_ref: { boolean?: boolean };
428+
one_of_inferred:
429+
| { kibana?: { versions?: string } }
430+
| { elasticsearch?: { versions?: string } }
402431
}
403432
}`)
404433
);

0 commit comments

Comments
 (0)