diff --git a/lib/utils/recursiveSearch.js b/lib/utils/recursiveSearch.js index d240060..eb5396f 100644 --- a/lib/utils/recursiveSearch.js +++ b/lib/utils/recursiveSearch.js @@ -1,3 +1,4 @@ +const merge = require("deepmerge"); let schemaOut = {}; function recursiveSearch(resp) { @@ -22,10 +23,41 @@ function recursiveSearch(resp) { }); } } + function finalResult(res) { schemaOut = res; } +function removeDuplicates(arr) { + return Array.isArray(arr) ? [...new Set(arr)] : arr; +} + +function mergeSchema(schema) { + if (schema.allOf) { + return schema.allOf.reduce((merged, subSchema) => { + return merge(merged, mergeSchema(subSchema)); + }, {}); + } + + if (schema.oneOf) { + return schema.oneOf.reduce((merged, subSchema) => { + return merge(merged, mergeSchema(subSchema)); + }, {}); + } + + if (schema.properties) { + const mergedProperties = {}; + for (const [key, value] of Object.entries(schema.properties)) { + mergedProperties[key] = mergeSchema(value); + if (mergedProperties[key].enum) { + mergedProperties[key].enum = removeDuplicates(mergedProperties[key].enum); + } + } + return { ...schema, properties: mergedProperties }; + } + return schema; +} + function getSchemaOut(operation) { // console.log("first callback",finalResult) let successResp = "200"; @@ -37,9 +69,14 @@ function getSchemaOut(operation) { } else if (resp201 !== undefined) { recursiveSearch(resp201); } + try { + schemaOut = mergeSchema(schemaOut); + } catch (e) { + console.log(e); + } return schemaOut; } module.exports = { - getSchemaOut, + getSchemaOut }; diff --git a/package-lock.json b/package-lock.json index 15d0d60..900a53d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@apidevtools/swagger-parser": "^10.1.0", "commander": "^10.0.1", + "deepmerge": "^4.3.1", "fs-extra": "^10.1.0", "html-to-text": "^8.2.0", "lodash": "^4.17.20", diff --git a/package.json b/package.json index 4645a57..55598e7 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "oih-gen": "bin/oih-gen.js" }, "jest": { - "testPathIgnorePatterns": ["templates/spec-integration"] + "testPathIgnorePatterns": [ + "templates/spec-integration" + ] }, "publishConfig": { "access": "public" @@ -31,6 +33,7 @@ "dependencies": { "@apidevtools/swagger-parser": "^10.1.0", "commander": "^10.0.1", + "deepmerge": "^4.3.1", "fs-extra": "^10.1.0", "html-to-text": "^8.2.0", "lodash": "^4.17.20", @@ -49,15 +52,15 @@ }, "devDependencies": { "axios": "^1.4.0", + "chai": "^4.3.7", "dayjs": "1.11.10", "eslint": "^8.54.0", "eslint-plugin-json": "^3.1.0", "formdata-node": "4.4.1", "jest": "^29.5.0", - "prettier": "^2.8.3", - "swagger-client": "^3.24.5", "mocha": "^10.7.0", + "prettier": "^2.8.3", "sinon": "^18.0.0", - "chai": "^4.3.7" + "swagger-client": "^3.24.5" } }