Skip to content

Commit 15c6e94

Browse files
committed
feat: add support for empty schemas meaning "any JSON" as well as object schemas with no properties
1 parent 74bd72e commit 15c6e94

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

lib/process-document.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ export async function processOpenApiDocument(
113113
isTypeOnly: true,
114114
});
115115

116+
typesFile.addImportDeclaration({
117+
namedImports: ['Jsonifiable'],
118+
moduleSpecifier: 'type-fest',
119+
isTypeOnly: true,
120+
});
121+
122+
typesFile.addImportDeclaration({
123+
namedImports: ['JsonifiableObject'],
124+
moduleSpecifier: 'type-fest/source/jsonifiable.js',
125+
isTypeOnly: true,
126+
});
127+
116128
const typesModuleSpecifier =
117129
`./${typesFile.getBaseNameWithoutExtension()}.js` ||
118130
relative(commandsFile.getDirectoryPath(), typesFile.getFilePath());

lib/process-schema.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,21 @@ export function schemaToType(
244244
if (schemaObject.enum?.every((e) => e === null)) {
245245
return {
246246
name,
247-
hasQuestionToken: false,
247+
hasQuestionToken,
248248
type: 'null',
249249
docs,
250250
};
251251
}
252252

253+
if (!schemaObject.properties) {
254+
return {
255+
name,
256+
hasQuestionToken,
257+
type: withNullUnion('JsonifiableObject', schemaObject.nullable),
258+
docs,
259+
};
260+
}
261+
253262
return {
254263
name,
255264
hasQuestionToken,
@@ -322,13 +331,33 @@ export function schemaToType(
322331
};
323332
}
324333

334+
// empty schemaObject
335+
if (Object.keys(schemaObject).length === 0) {
336+
return {
337+
name,
338+
hasQuestionToken,
339+
type: maybeWithUndefined(
340+
withNullUnion(
341+
'Jsonifiable',
342+
'nullable' in schemaObject && schemaObject.nullable,
343+
),
344+
!options.disallowUndefined && hasQuestionToken,
345+
),
346+
docs,
347+
};
348+
}
349+
325350
const type =
326351
schemaObject.type === 'string' && schemaObject.format?.includes('date')
327352
? 'Date'
328353
: schemaObject.type?.toString() || 'never';
329354

330355
if (type === 'never') {
331-
console.warn('unknown type in %j', schemaObject);
356+
console.warn(
357+
'unsupported type in %j with parent %j',
358+
schemaObject,
359+
parentSchema,
360+
);
332361
}
333362

334363
return {

0 commit comments

Comments
 (0)