Skip to content

Commit

Permalink
feat: cds parsing now works properly with multiple files (#824)
Browse files Browse the repository at this point in the history
* feat: cds parsing now works properly with multiple files

* chore: fix pnpmlock

* chore: update snapshots

* chore: handle windows

* chore: handle windows ?
  • Loading branch information
nlunets authored Jun 10, 2024
1 parent 29a46b5 commit 06d1cab
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-colts-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ux/fe-mockserver-plugin-cds': minor
---

CDS Parsing now respects proper CDS syntax for multiple files
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports[`V4 Requestor Sticky create, discard, try to update the discarded item:
exports[`V4 Requestor can create data through a call 1`] = `
{
"@odata.context": "../$metadata#RootElement/$entity",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 555,
Expand All @@ -56,7 +56,7 @@ exports[`V4 Requestor can create data through a call 1`] = `
exports[`V4 Requestor can create data through a call 2`] = `
{
"@odata.context": "../$metadata#RootElement/$entity",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 666,
Expand Down Expand Up @@ -505,6 +505,7 @@ exports[`V4 Requestor can get the metadata 1`] = `
<Annotations Target="sap.fe.core.ActionVisibility.DraftAdministrativeData/DraftUUID">
<Annotation Term="UI.Hidden" Bool="true"/>
<Annotation Term="Common.Label" String="{i18n>Draft_DraftUUID}"/>
<Annotation Term="Core.ComputedDefaultValue" Bool="true"/>
</Annotations>
<Annotations Target="sap.fe.core.ActionVisibility.DraftAdministrativeData/CreationDateTime">
<Annotation Term="Common.Label" String="{i18n>Draft_CreationDateTime}"/>
Expand Down Expand Up @@ -598,7 +599,7 @@ exports[`V4 Requestor can get the root 1`] = `
exports[`V4 Requestor can get the root 2`] = `
{
"@odata.context": "$metadata",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"value": [
{
"kind": "EntitySet",
Expand Down Expand Up @@ -644,7 +645,7 @@ exports[`V4 Requestor can reload the data 1`] = `
exports[`V4 Requestor can update data through a call 1`] = `
{
"@odata.context": "../$metadata#RootElement/$entity",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 556,
Expand All @@ -661,7 +662,7 @@ exports[`V4 Requestor can update data through a call 1`] = `
exports[`V4 Requestor can update data through a call 2`] = `
{
"@odata.context": "$metadata#RootElement(ID=556)",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 556,
Expand All @@ -678,7 +679,7 @@ exports[`V4 Requestor can update data through a call 2`] = `
exports[`V4 Requestor can update data through a call 3`] = `
{
"@odata.context": "$metadata#RootElement(ID=556)/Prop1",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 556,
Expand All @@ -695,7 +696,7 @@ exports[`V4 Requestor can update data through a call 3`] = `
exports[`V4 Requestor get one data 1`] = `
{
"@odata.context": "$metadata#RootElement(ID=2)",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 2,
Expand All @@ -714,7 +715,7 @@ exports[`V4 Requestor get one data 2`] = `""`;
exports[`V4 Requestor get one data 3`] = `
{
"@odata.context": "$metadata#RootElement(ID=2)",
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"HasActiveEntity": false,
"HasDraftEntity": false,
"ID": 2,
Expand Down
2 changes: 1 addition & 1 deletion packages/fe-mockserver-core/test/unit/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('V4 Requestor', function () {
{
"@odata.context": "$metadata#RootElement(ID=1,IsActiveEntity=true)/_Elements",
"@odata.count": 3,
"@odata.metadataEtag": "W/"606c-jGYf/2yxvlcG7Z+VRm6krl+f58U"",
"@odata.metadataEtag": "W/"60af-tCc45VlNT7csKPRiK0amTPyY35E"",
"value": [
{
"HasActiveEntity": true,
Expand Down
4 changes: 2 additions & 2 deletions packages/fe-mockserver-plugin-cds/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"test-u": "jest --ci --forceExit --detectOpenHandles --colors -u"
},
"peerDependencies": {
"@sap/cds-compiler": "^4"
"@sap/cds-compiler": "^4.8"
},
"devDependencies": {
"@sap/cds-compiler": "^4"
"@sap/cds-compiler": "^4.8"
}
}
21 changes: 16 additions & 5 deletions packages/fe-mockserver-plugin-cds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export interface IFileLoader {
export interface IMetadataProcessor {
loadMetadata(filePath: string): Promise<string>;
}

import { compileSources, to } from '@sap/cds-compiler';
import { compileSync, to } from '@sap/cds-compiler';
import path from 'path';
import { commonCDS } from './common.cds';

Expand All @@ -34,22 +33,34 @@ export default class CDSMetadataProvider implements IMetadataProcessor {
* @returns a promise with the EDMX content
*/
async loadMetadata(filePath: string): Promise<string> {
let cdsContent = await this.fileLoader.loadFile(filePath);
const cdsContent = await this.fileLoader.loadFile(filePath);
if (filePath.endsWith('.xml')) {
return cdsContent;
}
const usingReg = new RegExp(/from '([^']+)/g);
const matches = cdsContent.match(usingReg);
const fileCache: Record<string, string> = {};
fileCache[filePath] = cdsContent;

fileCache[path.resolve('./dummyHomme/common.cds')] = commonCDS;

fileCache['@sap/cds/common'] = commonCDS;
if (matches) {
let additionalFiles: string[] = matches.map((match) => /from '([^']+)/.exec(match)![1]);
additionalFiles = additionalFiles.filter((fileName) => fileName !== '@sap/cds/common');

for (const additionalFilesKey of additionalFiles) {
cdsContent += await this.fileLoader.loadFile(path.resolve(path.dirname(filePath), additionalFilesKey));
const additionalFilePath = path.resolve(path.dirname(filePath), additionalFilesKey);
fileCache[additionalFilePath] = await this.fileLoader.loadFile(additionalFilePath);
}
}

const csn = compileSources({ 'string.cds': cdsContent, 'common.cds': commonCDS }, {});
const csn = compileSync(
[filePath],
path.dirname(filePath),
{ cdsHome: path.resolve('./dummyHomme') },
fileCache
);
const edmx = to.edmx.all(csn, {
odataVersion: this.options?.odataVersion || 'v4',
odataForeignKeys: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ exports[`FE Mockserver CDS Plugin can compile valid CDS file using common stuff
<Property Name="descr" Type="Edm.String" MaxLength="1000"/>
<Property Name="code" Type="Edm.String" MaxLength="3" Nullable="false"/>
</EntityType>
<Annotations Target="sap.fe.mockserver.ValidCDSWithCommon.RootElement/ID">
<Annotation Term="Core.ComputedDefaultValue" Bool="true"/>
</Annotations>
<Annotations Target="sap.fe.mockserver.ValidCDSWithCommon.RootElement/Prop1">
<Annotation Term="Common.Label" String="First Prop"/>
</Annotations>
Expand Down Expand Up @@ -405,6 +408,9 @@ exports[`FE Mockserver CDS Plugin can compile valid CDS file using common stuff
<End Role="Currencies" Type="sap.fe.mockserver.ValidCDSWithCommon.Currencies" Multiplicity="*"/>
<End Role="Currencies_texts" Type="sap.fe.mockserver.ValidCDSWithCommon.Currencies_texts" Multiplicity="0..1"/>
</Association>
<Annotations Target="sap.fe.mockserver.ValidCDSWithCommon.RootElement/ID" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Annotation Term="Core.ComputedDefaultValue" Bool="true"/>
</Annotations>
<Annotations Target="sap.fe.mockserver.ValidCDSWithCommon.RootElement/Prop1" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Annotation Term="Common.Label" String="First Prop"/>
</Annotations>
Expand Down Expand Up @@ -518,7 +524,7 @@ exports[`FE Mockserver CDS Plugin can compile valid CDS file using more common s
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
<Property Name="StringProperty" Type="Edm.String"/>
<Property Name="IntegerProperty" Type="Edm.Int32"/>
<Property Name="NumberProperty" Type="Edm.Decimal" Scale="2" Precision="4"/>
<Property Name="NumberProperty" Type="Edm.Decimal" Precision="4" Scale="2"/>
<Property Name="BooleanProperty" Type="Edm.Boolean"/>
<Property Name="DateProperty" Type="Edm.Date"/>
<Property Name="TimeProperty" Type="Edm.TimeOfDay"/>
Expand Down Expand Up @@ -769,6 +775,7 @@ exports[`FE Mockserver CDS Plugin can compile valid CDS file using more common s
<Annotations Target="Service.DraftAdministrativeData/DraftUUID">
<Annotation Term="UI.Hidden" Bool="true"/>
<Annotation Term="Common.Label" String="{i18n>Draft_DraftUUID}"/>
<Annotation Term="Core.ComputedDefaultValue" Bool="true"/>
</Annotations>
<Annotations Target="Service.DraftAdministrativeData/CreationDateTime">
<Annotation Term="Common.Label" String="{i18n>Draft_CreationDateTime}"/>
Expand Down
1 change: 1 addition & 0 deletions packages/fe-mockserver-plugin-cds/test/cds/valid-used.cds
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using {sap.fe.mockserver.ValidCDS as ValidCDS} from './valid';

annotate ValidCDS.RootElement with @(UI : {LineItem : [
{Value : ID},
Expand Down
2 changes: 1 addition & 1 deletion packages/fe-mockserver-plugin-cds/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('FE Mockserver CDS Plugin', () => {
await expect(myCDSProvider.loadMetadata(path.join(cdsDataPath, 'invalid-syntaxError.cds'))).rejects
.toMatchInlineSnapshot(`
[Error: CDS compilation failed
string.cds:5:15-17: Error: Extraneous ‹Identifier›, expecting ‘:’, ‘;’, ‘{’, ‘@’, ‘=’]
invalid-syntaxError.cds:5:15-17: Error: Extraneous ‹Identifier›, expecting ‘:’, ‘;’, ‘{’, ‘@’, ‘=’]
`);
});
it('will throw while processing invalid CDS - multiple Services', async () => {
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 06d1cab

Please sign in to comment.