From fae91d148444029e6f1da101cb15da8d431ce6e4 Mon Sep 17 00:00:00 2001 From: NickOvt Date: Tue, 2 Jan 2024 12:50:06 +0200 Subject: [PATCH] fix(api-docs): Fixed requestBody in API docs ZMS-118 (#593) * optimize CPU usage * fix requestBody issues in API docs generation * fix requestBody issues for API generation * Revert "optimize CPU usage" This reverts commit b4fde61884963c7ae82947d61c109cf48fd67571. --- lib/tools.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/tools.js b/lib/tools.js index f71aafcc..39da2331 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -17,6 +17,8 @@ const ObjectId = require('mongodb').ObjectId; const log = require('npmlog'); const addressparser = require('nodemailer/lib/addressparser'); +const Joi = require('joi'); + let templates = false; const structuredCloneWrapper = typeof structuredClone === 'function' ? structuredClone : obj => JSON.parse(JSON.stringify(obj)); @@ -765,6 +767,10 @@ function parseJoiObject(path, joiObject, requestBodyProperties) { const data = { type: openApiType, description, required: isRequired }; if (format) { data.format = format; + + if (data.format === 'date') { + data.format = 'date-time'; + } } // enum check @@ -974,22 +980,19 @@ module.exports = { // 5) add requestBody const applicationType = spec.applicationType || 'application/json'; - operationObj.requestBody = { - content: { - [applicationType]: { - schema: { - type: 'object', - properties: {} - } - } - }, - required: true - }; - for (const reqBodyKey in spec.validationObjs?.requestBody) { - const reqBodyKeyData = spec.validationObjs.requestBody[reqBodyKey]; + if (spec.validationObjs?.requestBody && Object.keys(spec.validationObjs.requestBody).length > 0) { + operationObj.requestBody = { + content: { + [applicationType]: { + schema: {} + } + }, + required: true + }; - parseJoiObject(reqBodyKey, reqBodyKeyData, operationObj.requestBody.content[applicationType].schema.properties); + // convert to Joi object for easier parsing + parseJoiObject('schema', Joi.object(spec.validationObjs?.requestBody), operationObj.requestBody.content[applicationType]); } // 6) add parameters (queryParams + pathParams). @@ -1070,7 +1073,7 @@ module.exports = { const innerData = pathData[httpMethod]; // for every requestBody obj - for (const key in innerData.requestBody.content[Object.keys(innerData.requestBody.content)[0]].schema.properties) { + for (const key in innerData?.requestBody?.content[Object.keys(innerData.requestBody.content)[0]].schema.properties) { const reqBodyData = innerData.requestBody.content[Object.keys(innerData.requestBody.content)[0]].schema.properties[key]; parseComponetsDecoupled(reqBodyData, components.components.schemas);