From 3bb77bc093621f7cab745a7e0c6a4fb375099c54 Mon Sep 17 00:00:00 2001 From: Diptodas123 Date: Mon, 7 Oct 2024 18:45:50 +0530 Subject: [PATCH 1/3] Add --openapi:config-file option in cds compile --- lib/compile/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/compile/index.js b/lib/compile/index.js index f10bd66..c511cb3 100644 --- a/lib/compile/index.js +++ b/lib/compile/index.js @@ -88,6 +88,26 @@ function toOpenApiOptions(csdl, csn, options = {}) { } } + if (result["config-file"]) { + try { + const fileContent = require(result["config-file"]); + for (let key in fileContent) { + if (key === "odata-version" && result["odataVersion"] || key in result) { + delete fileContent[key]; + } else if (key === "odata-version") { + result["odataVersion"] = fileContent[key]; + } else if (key === "diagram") { + result["diagram"] = fileContent[key] === "true" ? true : false; + } else { + result[key] = JSON.stringify(fileContent[key]); + } + } + delete result["config-file"]; + } catch (error) { + console.log(error); + } + } + const protocols = _getProtocols(csdl, csn); if (result.url) { From 5b3be4f121dbb440c638f859a2f2dc5b5c3f8923 Mon Sep 17 00:00:00 2001 From: Dipto-at-sap Date: Tue, 8 Oct 2024 15:53:33 +0530 Subject: [PATCH 2/3] Optimize code --- lib/compile/index.js | 73 ++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/lib/compile/index.js b/lib/compile/index.js index c511cb3..a744270 100644 --- a/lib/compile/index.js +++ b/lib/compile/index.js @@ -1,29 +1,30 @@ const csdl2openapi = require('./csdl2openapi') const cds = require('@sap/cds/lib'); +const fs = require("fs"); module.exports = function processor(csn, options = {}) { - const edmOptions = Object.assign({ - odataOpenapiHints: true, // hint to cds-compiler - edm4OpenAPI: true, // downgrades certain OData errors to warnings in cds-compiler - to: 'openapi' // hint to cds.compile.to.edm (usually set by CLI, but also do this in programmatic usages) - }, options) - - // must not be part of function* otherwise thrown errors are swallowed - const csdl = cds.compile.to.edm(csn, edmOptions); - let openApiDocs = {}; - - if (csdl[Symbol.iterator]) { // generator function means multiple services - openApiDocs = _getOpenApiForMultipleServices(csdl, csn, options) - } else { - const openApiOptions = toOpenApiOptions(csdl, csn, options) - openApiDocs = _getOpenApi(csdl, openApiOptions); - } + const edmOptions = Object.assign({ + odataOpenapiHints: true, // hint to cds-compiler + edm4OpenAPI: true, // downgrades certain OData errors to warnings in cds-compiler + to: 'openapi' // hint to cds.compile.to.edm (usually set by CLI, but also do this in programmatic usages) + }, options) + + // must not be part of function* otherwise thrown errors are swallowed + const csdl = cds.compile.to.edm(csn, edmOptions); + let openApiDocs = {}; - if(Object.keys(openApiDocs).length == 1){ - return openApiDocs[Object.keys(openApiDocs)[0]]; - } + if (csdl[Symbol.iterator]) { // generator function means multiple services + openApiDocs = _getOpenApiForMultipleServices(csdl, csn, options) + } else { + const openApiOptions = toOpenApiOptions(csdl, csn, options) + openApiDocs = _getOpenApi(csdl, openApiOptions); + } + + if (Object.keys(openApiDocs).length == 1) { + return openApiDocs[Object.keys(openApiDocs)[0]]; + } - return _iterate(openApiDocs); + return _iterate(openApiDocs); } function _getOpenApiForMultipleServices(csdl, csn, options) { @@ -88,25 +89,23 @@ function toOpenApiOptions(csdl, csn, options = {}) { } } - if (result["config-file"]) { - try { - const fileContent = require(result["config-file"]); - for (let key in fileContent) { - if (key === "odata-version" && result["odataVersion"] || key in result) { - delete fileContent[key]; - } else if (key === "odata-version") { - result["odataVersion"] = fileContent[key]; - } else if (key === "diagram") { - result["diagram"] = fileContent[key] === "true" ? true : false; - } else { - result[key] = JSON.stringify(fileContent[key]); - } - } - delete result["config-file"]; - } catch (error) { - console.log(error); + if (result["config-file"]){ + if(fs.existsSync(result["config-file"])) { + const fileContent = require(result["config-file"]); + Object.keys(fileContent).forEach((key) => { + if (!(key in result)) { // inline options take precedence + result[key] = JSON.stringify(fileContent[key]); } + if (key === "odata-version" && !result["odataVersion"]) { + result["odataVersion"] = fileContent[key]; + } else if (key === "diagram") { + result["diagram"] = !result["diagram"] && fileContent[key] === "true"; + } + }); + } else { + throw new Error("Error while parsing the openapi configuration file"); } +} const protocols = _getProtocols(csdl, csn); From f82116d47fbeb1a8dc603fee800cf293321c6592 Mon Sep 17 00:00:00 2001 From: Dipto-at-sap Date: Wed, 9 Oct 2024 12:55:42 +0530 Subject: [PATCH 3/3] Update Changelog to document the --openapi:config-file option --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39cfecb..487673f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,3 +58,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). ### Added - Initial release +- Introduced --openapi:config-file option to incorporate all the options for cds compile command in a JSON configuration file, inline options take precedence over those defined in the configuration file. \ No newline at end of file