Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CDSADAPTERS-2132] Openapi compile: Options input provided in JSON file #16882 #46

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,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.
59 changes: 39 additions & 20 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -88,6 +89,24 @@ function toOpenApiOptions(csdl, csn, options = {}) {
}
}

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);

if (result.url) {
Expand Down