Skip to content

Commit c12cc59

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

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/swaggerToTS.js

+9-10
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)