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

OpenAPI: Revert changes from PR #42 #57

Merged
merged 2 commits into from
Dec 3, 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 @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

- Fixed allowedValues on all primitive types.
- Removed duplicates in `tags`.
- No longer append protocol and service name information to the server URL incase of `openapi:servers` option.

## Version 1.0.7 - 17.10.2024

Expand Down
18 changes: 5 additions & 13 deletions lib/compile/csdl2openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports.csdl2openapi = function (
'x-sap-api-type': 'ODATAV4',
'x-odata-version': csdl.$Version,
'x-sap-shortText': getShortText(csdl, entityContainer),
servers: getServers(serviceRoot, serversObject, csdl),
servers: getServers(serviceRoot, serversObject),
tags: entityContainer ? getTags(entityContainer) : {},
paths: entityContainer ? getPaths(entityContainer) : {},
components: getComponents(csdl, entityContainer)
Expand Down Expand Up @@ -564,21 +564,13 @@ module.exports.csdl2openapi = function (
* @param {object} serversObject Input servers object
* @return {Array} The list of servers
*/
function getServers(serviceRoot, serversObject, csdl) {
function getServers(serviceRoot, serversObject) {
let servers;

if (serversObject && csdl.$EntityContainer ) {
if (serversObject) {
try {
servers = JSON.parse(serversObject);
// if csdl.$Version is 4.01 then protocol is rest if it is less than 4.01 then protocol is odata
const protocol = csdl.$Version <= "4.01" ? "odata/v4" : "rest";
const serviceName = nameParts(csdl.$EntityContainer).qualifier;
// append /protocol/{$serviceName} to the URL
servers.forEach((server) => {
server.url = server.url + "/" + protocol + "/" + serviceName;
});
servers = JSON.parse(serversObject);
} catch (err) {
throw new Error(`The input server object is invalid.`);
throw new Error(`The input server object is invalid.`);
}

if (!servers.length) {
Expand Down
6 changes: 3 additions & 3 deletions test/lib/compile/openapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ service CatalogService {
const serverObj = "[{\n \"url\": \"https://{customer1Id}.saas-app.com:{port}/v2\",\n \"variables\": {\n \"customer1Id\": \"demo\",\n \"description\": \"Customer1 ID assigned by the service provider\"\n }\n}, {\n \"url\": \"https://{customer2Id}.saas-app.com:{port}/v2\",\n \"variables\": {\n \"customer2Id\": \"demo\",\n \"description\": \"Customer2 ID assigned by the service provider\"\n }\n}]"
const openapi = toOpenApi(csn, { 'openapi:servers': serverObj });
expect(openapi.servers).toBeTruthy();
expect(openapi.servers[0].url).toMatch('https://{customer1Id}.saas-app.com:{port}/v2/odata/v4/A')
expect(openapi.servers[0].url).toMatch('https://{customer1Id}.saas-app.com:{port}/v2')
});


Expand All @@ -277,7 +277,7 @@ service CatalogService {
);
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).toMatchObject({ servers: [{ url: 'http://foo.bar:8080' }, { url: "http://foo.bar:8080/a/foo" }] });
expect(openapi.info.description).toMatch(/yuml.*diagram/i);
expect(openapi['x-odata-version']).toMatch('4.1');
});
Expand All @@ -296,7 +296,7 @@ service CatalogService {
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" }] });
expect(openapi).toMatchObject({ servers: [{ url: 'http://foo.bar:8080' }, { url: "http://foo.bar:8080/a/foo" }] });
});

test('annotations: root entity property', () => {
Expand Down
Loading