Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function generateOpenAPISchema(spec: ControllerSpec, tsType: Function) {
if (!spec.components) {
spec.components = {};
}
if (!spec.components.schemas) {
spec.components.schemas = {};
}
if (tsType.name in spec.components.schemas) {
// Preserve user-provided definitions
debug(' skipping type %j as already defined', tsType.name || tsType);
return;
}
const jsonSchema = getJsonSchema(tsType);
const openapiSchema = jsonToSchemaObject(jsonSchema);
assignRelatedSchemas(spec, openapiSchema.definitions);
delete openapiSchema.definitions;
debug(' defining schema for %j: %j', tsType.name, openapiSchema);
spec.components.schemas[tsType.name] = openapiSchema;
}
debug(` inferring schema object for method %s`, op);
const paramTypes = MetadataInspector.getDesignTypeForMethod(
constructor.prototype,
op,
).parameterTypes;
const isComplexType = (ctor: Function) =>
!_.includes([String, Number, Boolean, Array, Object], ctor) &&
!isReadableStream(ctor);
for (const p of paramTypes) {
if (isComplexType(p)) {
if (!spec.definitions) {
spec.definitions = {};
}
const jsonSchema = getJsonSchema(p);
const openapiSchema = jsonToSchemaObject(jsonSchema);
if (openapiSchema.definitions) {
for (const key in openapiSchema.definitions) {
spec.definitions[key] = openapiSchema.definitions[key];
}
delete openapiSchema.definitions;
}
spec.definitions[p.name] = openapiSchema;
break;
}
}
}
return spec;
}