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] Added --openapi:config-file option for openapi compile and test cases for the same #49

Merged
merged 3 commits into from
Oct 17, 2024
Merged
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.
60 changes: 39 additions & 21 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,23 @@ 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 === "odata-version" && !result["odataVersion"]) {
result["odataVersion"] = fileContent[key];
} else if (key === "diagram") {
result["diagram"] = !result["diagram"] && fileContent[key] === "true";
} else if (!(key in result)) { // inline options take precedence
result[key] = JSON.stringify(fileContent[key]);
}
});
} else {
throw new Error("Error while parsing the openapi configuration file");
}
}

const protocols = _getProtocols(csdl, csn);

if (result.url) {
Expand Down Expand Up @@ -158,4 +176,4 @@ function _servicePath(csdl, csn, protocols) {

return paths;
}
}
}
13 changes: 13 additions & 0 deletions test/lib/compile/data/configFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"url": "http://foo.bar:3000",
"servers": [
{
"url": "http://foo.bar:8080"
},
{
"url": "http://foo.bar:8080/a/foo"
}
],
"odata-version": "4.1",
"diagram":"true"
}
29 changes: 29 additions & 0 deletions test/lib/compile/openapi.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { path } = require('@sap/cds/lib/utils/cds-utils');
const toOpenApi = require('../../../lib/compile');
const cds = require('@sap/cds')

Expand Down Expand Up @@ -237,6 +238,34 @@ describe('OpenAPI export', () => {
}
});

test('options: config-file - without inline options', () => {
const csn = cds.compile.to.csn(`
service A {entity E { key ID : UUID; };};`
);
const openapi = toOpenApi(csn, { 'openapi:config-file': path.resolve("./test/lib/compile/data/configFile.json") });
expect(openapi.servers).toBeTruthy();
expect(openapi).toMatchObject({ servers: [{ url: 'http://foo.bar:8080/rest/A' }, { url: "http://foo.bar:8080/a/foo/rest/A" }] });
expect(openapi.info.description).toMatch(/yuml.*diagram/i);
expect(openapi['x-odata-version']).toMatch('4.1');
});

test('options: config-file - with inline options, inline options given precedence', () => {
const csn = cds.compile.to.csn(`
@title:'It is located at http://example.com:8080' service A {entity E { key ID : UUID;};};`
);
const options = {
'openapi:config-file': path.resolve("./test/lib/compile/data/configFile.json"),
'openapi:url': "http://example.com:8080",
'odata-version': '4.0',
'openapi:diagram': "false"
}
const openapi = toOpenApi(csn, options);
expect(openapi.info.title).toMatch(/http:\/\/example.com:8080/i)
expect(openapi.info.description).not.toMatch(/yuml.*diagram/i);
expect(openapi['x-odata-version']).toMatch('4.0');
expect(openapi).toMatchObject({ servers: [{ url: 'http://foo.bar:8080/odata/v4/A' }, { url: "http://foo.bar:8080/a/foo/odata/v4/A" }] });
});

test('annotations: root entity property', () => {
const csn = cds.compile.to.csn(`
namespace sap.odm.test;
Expand Down