Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/cap-js/ORD into fix/69-add-…
Browse files Browse the repository at this point in the history
…missing-top-level-property-entitytypes
  • Loading branch information
aramovic79 committed Dec 3, 2024
2 parents 7fd665f + 6ef7a87 commit 8199513
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
27 changes: 27 additions & 0 deletions __tests__/__mocks__/noEntitiesInServiceDefinitionCsn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"definitions": {
"EbMtEmitter": {
"kind": "service"
},
"EbMtEmitter.sendMsg": {
"kind": "action"
},
"EbMtEmitter.MyEvent": {
"kind": "event",
"elements": {
"id": {
"key": true,
"type": "cds.Integer"
},
"message": {
"type": "cds.String"
}
}
}
},
"meta": {
"creator": "CDS Compiler v5.4.4",
"flavor": "inferred"
},
"$version": "2.0"
}
29 changes: 29 additions & 0 deletions __tests__/noEntitiesInServiceDefinition.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const cds = require("@sap/cds");
const csn = require("./__mocks__/noEntitiesInServiceDefinitionCsn.json");
const path = require("path");

describe("Tests for ORD document when there no events or entities in service definitions", () => {
let ord;
beforeAll(() => {
jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00");
ord = require("../lib/ord");
cds.root = path.join(__dirname, "bookshop");
cds.env = {};
});

afterAll(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});

test("Successfully create ORD Document: no entityTypeMappings field in apiResource", () => {
const document = ord(csn);

expect(document).not.toBeUndefined();
expect(document.apiResources).toHaveLength(1);
expect(document.eventResources).toHaveLength(1);
expect(document.apiResources[0].ordId).toEqual(expect.stringContaining("EbMtEmitter"));
expect(document.eventResources[0].ordId).toEqual(expect.stringContaining("EbMtEmitter"));
expect(document.apiResources[0].entityTypeMappings).toBeUndefined();
});
});
5 changes: 0 additions & 5 deletions __tests__/unittest/__snapshots__/templates.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ exports[`templates createAPIResourceTemplate should create API resource template
{
"apiProtocol": "odata-v4",
"description": "Description for MyService",
"entityTypeMappings": [
{
"entityTypeTargets": undefined,
},
],
"entryPoints": [
"/odata/v4/my",
],
Expand Down
7 changes: 5 additions & 2 deletions lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,14 @@ const createAPIResourceTemplate = (serviceName, serviceDefinition, appConfig, pa
extensible: {
supported: "no",
},
entityTypeMappings: [{ entityTypeTargets: appConfig.odmEntities }],
// conditionally setting the entityTypeMappings field based on odmEntities in appConfig
...(appConfig.odmEntities?.length > 0 && { entityTypeMappings: [{entityTypeTargets: appConfig.odmEntities}] }),
...ordExtensions,
};

if (obj.visibility === RESOURCE_VISIBILITY.public) apiResources.push(obj);
if (obj.visibility === RESOURCE_VISIBILITY.public) {
apiResources.push(obj);
}
});

return apiResources;
Expand Down

0 comments on commit 8199513

Please sign in to comment.