Skip to content

Commit 21c5b74

Browse files
committed
Handle objects without properties
1 parent 0dc6c7e commit 21c5b74

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@manifoldco/swagger-to-ts",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Convert Swagger files to TypeScript",
55
"main": "dist/swaggerToTS.js",
66
"scripts": {

src/swaggerToTS.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const buildTypes = (spec, namespace) => {
2727
};
2828

2929
// Returns primitive type, or 'object' or 'any'
30-
const getType = ({ $ref, items, type }) => {
30+
const getType = ({ $ref, items, type, ...value }, nestedName) => {
3131
if ($ref) {
3232
const [refName, refProperties] = getRef($ref);
3333
return TYPES[refProperties.type] || refName || 'any';
@@ -37,6 +37,10 @@ const buildTypes = (spec, namespace) => {
3737
return `${TYPES[refProperties.type] || refName || 'any'}[]`;
3838
}
3939
return `${TYPES[items.type] || 'any'}[]`;
40+
} else if (value.properties) {
41+
// If this is a nested object, let’s add it to the stack for later
42+
queue.push([nestedName, { $ref, items, type, ...value }]);
43+
return nestedName;
4044
}
4145

4246
return TYPES[type] || type || 'any';
@@ -72,7 +76,8 @@ const buildTypes = (spec, namespace) => {
7276
Object.entries(properties).forEach(([key, value]) => {
7377
const optional = !Array.isArray(required) || required.indexOf(key) === -1;
7478
const name = `${camelCase(key)}${optional ? '?' : ''}`;
75-
const type = getType(value);
79+
const newID = camelCase(`${ID}_${key}`);
80+
const type = getType(value, newID);
7681

7782
if (typeof value.description === 'string') {
7883
// Print out descriptions as comments, but only if there’s something there (.*)
@@ -81,14 +86,8 @@ const buildTypes = (spec, namespace) => {
8186
);
8287
}
8388

84-
// If this is a nested object, let’s add it to the stack for later
85-
if (type === 'object') {
86-
const newID = camelCase(`${ID}_${key}`);
87-
queue.push([newID, value]);
88-
output.push(`${name}: ${newID};`);
89-
return;
90-
} else if (Array.isArray(value.enum)) {
91-
const newID = camelCase(`${ID}_${key}`);
89+
// Save enums for later
90+
if (Array.isArray(value.enum)) {
9291
enumQueue.push([newID, value.enum]);
9392
output.push(`${name}: ${newID};`);
9493
return;

0 commit comments

Comments
 (0)