@sap/cds-compiler 5.7.2 → 5.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +62 -2
  2. package/bin/cdsse.js +13 -1
  3. package/doc/CHANGELOG_BETA.md +7 -0
  4. package/lib/api/options.js +2 -1
  5. package/lib/api/validate.js +9 -0
  6. package/lib/base/location.js +1 -1
  7. package/lib/base/message-registry.js +55 -20
  8. package/lib/base/messages.js +5 -2
  9. package/lib/base/model.js +8 -6
  10. package/lib/checks/assocOutsideService.js +40 -0
  11. package/lib/checks/featureFlags.js +4 -1
  12. package/lib/checks/types.js +7 -4
  13. package/lib/checks/validator.js +3 -0
  14. package/lib/compiler/assert-consistency.js +11 -5
  15. package/lib/compiler/checks.js +79 -17
  16. package/lib/compiler/define.js +60 -3
  17. package/lib/compiler/extend.js +1 -2
  18. package/lib/compiler/generate.js +1 -1
  19. package/lib/compiler/populate.js +17 -6
  20. package/lib/compiler/propagator.js +1 -1
  21. package/lib/compiler/resolve.js +181 -150
  22. package/lib/compiler/shared.js +276 -22
  23. package/lib/compiler/tweak-assocs.js +15 -4
  24. package/lib/compiler/xpr-rewrite.js +76 -50
  25. package/lib/edm/annotations/edmJson.js +1 -1
  26. package/lib/edm/annotations/genericTranslation.js +2 -2
  27. package/lib/edm/csn2edm.js +2 -2
  28. package/lib/edm/edmPreprocessor.js +15 -9
  29. package/lib/edm/edmUtils.js +12 -5
  30. package/lib/gen/CdlGrammar.checksum +1 -0
  31. package/lib/gen/CdlParser.js +2239 -2229
  32. package/lib/gen/Dictionary.json +55 -8
  33. package/lib/json/from-csn.js +37 -17
  34. package/lib/json/to-csn.js +4 -0
  35. package/lib/language/genericAntlrParser.js +7 -0
  36. package/lib/main.d.ts +5 -1
  37. package/lib/model/cloneCsn.js +1 -0
  38. package/lib/model/csnRefs.js +1 -0
  39. package/lib/model/csnUtils.js +0 -5
  40. package/lib/modelCompare/utils/filter.js +2 -2
  41. package/lib/optionProcessor.js +2 -0
  42. package/lib/parsers/AstBuildingParser.js +72 -34
  43. package/lib/parsers/CdlGrammar.g4 +20 -19
  44. package/lib/parsers/XprTree.js +206 -0
  45. package/lib/parsers/index.js +1 -1
  46. package/lib/render/toCdl.js +61 -89
  47. package/lib/render/toSql.js +59 -29
  48. package/lib/render/utils/standardDatabaseFunctions.js +252 -15
  49. package/lib/transform/addTenantFields.js +9 -3
  50. package/lib/transform/db/assocsToQueries/transformExists.js +3 -0
  51. package/lib/transform/db/assocsToQueries/utils.js +10 -3
  52. package/lib/transform/db/expansion.js +3 -1
  53. package/lib/transform/db/flattening.js +7 -3
  54. package/lib/transform/db/killAnnotations.js +1 -0
  55. package/lib/transform/db/processSqlServices.js +70 -17
  56. package/lib/transform/draft/db.js +8 -3
  57. package/lib/transform/draft/odata.js +27 -4
  58. package/lib/transform/effective/main.js +37 -10
  59. package/lib/transform/effective/misc.js +4 -9
  60. package/lib/transform/effective/service.js +34 -0
  61. package/lib/transform/effective/types.js +28 -17
  62. package/lib/transform/forOdata.js +36 -10
  63. package/lib/transform/forRelationalDB.js +30 -18
  64. package/lib/transform/odata/adaptAnnotationRefs.js +37 -21
  65. package/lib/transform/odata/createForeignKeys.js +120 -116
  66. package/lib/transform/odata/flattening.js +10 -8
  67. package/lib/transform/transformUtils.js +58 -25
  68. package/lib/transform/translateAssocsToJoins.js +10 -6
  69. package/lib/transform/universalCsn/coreComputed.js +5 -1
  70. package/package.json +1 -1
  71. package/share/messages/message-explanations.json +1 -0
  72. package/share/messages/rewrite-not-supported.md +5 -0
  73. package/share/messages/rewrite-undefined-key.md +94 -0
@@ -4,12 +4,12 @@
4
4
  // different backends.
5
5
  // The sibling of model/transform/TransformUtil.js which works with compacted new CSN.
6
6
 
7
- const { setProp } = require('../base/model');
7
+ const { setProp, isBetaEnabled } = require('../base/model');
8
8
 
9
- const { copyAnnotations, applyTransformations, isDollarSelfOrProjectionOperand, transformAnnotationExpression } = require('../model/csnUtils');
9
+ const { copyAnnotations, applyTransformations, isDollarSelfOrProjectionOperand, isDeepEqual } = require('../model/csnUtils');
10
10
  const { getUtils } = require('../model/csnUtils');
11
11
  const { typeParameters } = require('../compiler/builtins');
12
- const { isBuiltinType, isAnnotationExpression } = require('../base/builtins');
12
+ const { isBuiltinType } = require('../base/builtins');
13
13
  const { ModelError, CompilerAssertion} = require('../base/error');
14
14
  const { forEach } = require('../utils/objectUtils');
15
15
  const { cloneCsnNonDict, cloneCsnDict } = require('../model/cloneCsn');
@@ -44,6 +44,7 @@ function getTransformers(model, options, msgFunctions, pathDelimiter = '_') {
44
44
  toFinalBaseType,
45
45
  createExposingProjection,
46
46
  createAndAddDraftAdminDataProjection,
47
+ isValidDraftAdminDataMessagesType,
47
48
  createScalarElement,
48
49
  createAssociationElement,
49
50
  createAssociationPathComparison,
@@ -195,24 +196,8 @@ function getTransformers(model, options, msgFunctions, pathDelimiter = '_') {
195
196
  '@odata.Collation': 1,
196
197
  '@odata.Unicode': 1,
197
198
  } : {};
198
- const annoNames = copyAnnotations(elem, flatElem, false, excludes);
199
- if(options.transformation === 'effective') {
200
- for(const annoName of annoNames) {
201
- if(flatElem.keys && isAnnotationExpression(elem[annoName])) {
202
- flatElem.keys.forEach(key => {
203
- if(!key[annoName]) {
204
- key[annoName] = flatElem[annoName];
205
- transformAnnotationExpression(key, annoName, {
206
- ref: (parent, prop, ref) => {
207
- if(ref[0] !== '$self')
208
- parent.ref = ['$self', ...parent.ref];
209
- }
210
- })
211
- }
212
- })
213
- }
214
- }
215
- }
199
+
200
+ copyAnnotations(elem, flatElem, false, excludes);
216
201
 
217
202
  // Copy selected type properties
218
203
  const props = ['key', 'virtual', 'masked', 'viaAll'];
@@ -243,15 +228,20 @@ function getTransformers(model, options, msgFunctions, pathDelimiter = '_') {
243
228
  * @param {bool} [suspend] suspend flattening by caller until association path step
244
229
  * @param {int} [suspendPos] suspend if starting pos is lower or equal to suspendPos and suspend is true
245
230
  * @param {bool} [revokeAtSuspendPos] revoke suspension after suspendPos (binding parameter path use case)
231
+ * @param {bool} [flattenParameters] Whether to flatten references into structured parameters. OData flattens parameters, SQL/for.effective does not.
232
+ *
233
+ * @todo: Refactor to take config object instead of N boolean arguments.
246
234
  * @returns [string[], bool]
247
235
  */
248
- function flattenStructStepsInRef(ref, path, links, scope, resolvedLinkTypes=new WeakMap(), suspend=false, suspendPos=0, revokeAtSuspendPos=false) {
236
+ function flattenStructStepsInRef(ref, path, links, scope, resolvedLinkTypes=new WeakMap(), suspend=false, suspendPos=0, revokeAtSuspendPos=false, flattenParameters = false) {
237
+ // A path is absolute if it starts with $self or a parameter. Then we must not flatten the first path step.
238
+ const pathIsAbsolute = scope === '$self' || (!flattenParameters && scope === 'param');
249
239
  // Refs of length 1 cannot contain steps - no need to check
250
- if (ref.length < 2 || (scope === '$self' && ref.length === 2)) {
240
+ if (ref.length < 2 || (pathIsAbsolute && ref.length === 2)) {
251
241
  return [ ref, false ];
252
242
  }
253
243
 
254
- const result = scope === '$self' ? [ref[0]] : [];
244
+ const result = pathIsAbsolute ? [ref[0]] : [];
255
245
  //let stack = []; // IDs of path steps not yet processed or part of a struct traversal
256
246
  if(!links && !scope) { // calculate JIT if not supplied
257
247
  const res = inspectRef(path);
@@ -262,7 +252,7 @@ function getTransformers(model, options, msgFunctions, pathDelimiter = '_') {
262
252
  return [ ref, false ];
263
253
 
264
254
  // Don't process a leading $self - it will a .art with .elements!
265
- let i = scope === '$self' ? 1 : 0;
255
+ let i = pathIsAbsolute ? 1 : 0;
266
256
 
267
257
  // read property from resolved path link
268
258
  const art = (propName) =>
@@ -413,6 +403,11 @@ function getTransformers(model, options, msgFunctions, pathDelimiter = '_') {
413
403
  if (!draftAdminDataEntity) {
414
404
  draftAdminDataEntity = createAndAddDraftAdminDataEntity();
415
405
  model.definitions['DRAFT.DraftAdministrativeData'] = draftAdminDataEntity;
406
+ if (isBetaEnabled(options, 'draftMessages')
407
+ && options.transformation === 'odata'
408
+ && !model.definitions['DRAFT.DraftAdministrativeData_DraftMessage']) {
409
+ model.definitions['DRAFT.DraftAdministrativeData_DraftMessage'] = createDraftAdminDataMessagesType();
410
+ }
416
411
  if(options.tenantDiscriminator && options.transformation !== 'odata')
417
412
  addTenantFieldToArt(model.definitions['DRAFT.DraftAdministrativeData'], options);
418
413
  }
@@ -481,10 +476,48 @@ function getTransformers(model, options, msgFunctions, pathDelimiter = '_') {
481
476
  draftIsProcessedByMe.DraftIsProcessedByMe['@Common.Label'] = '{i18n>Draft_DraftIsProcessedByMe}';
482
477
  addElement(draftIsProcessedByMe, artifact, artifactName);
483
478
 
479
+ if (isBetaEnabled(options, 'draftMessages')) {
480
+ const messages = { DraftMessages: { } };
481
+ if (options.transformation === 'odata') {
482
+ messages.DraftMessages = { items: { type: 'DRAFT.DraftAdministrativeData_DraftMessage' } };
483
+ } else {
484
+ messages.DraftMessages = { type: 'cds.LargeString' };
485
+ }
486
+ messages.DraftMessages['@cds.api.ignore'] = true;
487
+ addElement(messages , artifact, artifactName);
488
+ }
489
+
484
490
  return artifact;
485
491
  }
486
492
  }
487
493
 
494
+ // Create the artificial 'DRAFT.Draf tAdministrativeData_DraftMessage' type
495
+ // for the beta feature 'draftMessages'
496
+ function createDraftAdminDataMessagesType() {
497
+ const messagesType = {
498
+ kind: 'type',
499
+ elements: Object.create(null)
500
+ }
501
+
502
+ addElement(createScalarElement('code', 'cds.String'), messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
503
+ addElement(createScalarElement('message', 'cds.String'), messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
504
+ addElement(createScalarElement('target', 'cds.String'), messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
505
+ addElement({ 'additionalTargets': createScalarElement('items', 'cds.String') }, messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
506
+ addElement(createScalarElement('transition', 'cds.Boolean'), messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
507
+ addElement(createScalarElement('numericSeverity', 'cds.UInt8'), messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
508
+ addElement(createScalarElement('longtextUrl', 'cds.String'), messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
509
+ // the tag element not needed for now, but might be added later on
510
+ // addElement(createScalarElement('tag', 'cds.String'), messagesType, 'DRAFT.DraftAdministrativeData_DraftMessage');
511
+ // setAnnotation(messagesType.tag, '@cds.api.ignore', true);
512
+ return messagesType;
513
+ }
514
+
515
+ // Checks if the given definition is a valid 'DRAFT.DraftAdministrativeData_DraftMessage' type
516
+ function isValidDraftAdminDataMessagesType(def) {
517
+ const expectedType = createDraftAdminDataMessagesType();
518
+ return isDeepEqual(def, expectedType, false);
519
+ }
520
+
488
521
  // Create an artificial scalar element 'elemName' with final type 'typeName'.
489
522
  // Make the element a key element if 'isKey' is true.
490
523
  // Add a default value 'defaultVal' if supplied
@@ -50,6 +50,7 @@ function translateAssocsToJoinsCSN(csn, options){
50
50
  // FIXME: Move this somewhere more appropriate
51
51
  const newCsn = compactModel(model, compileOptions);
52
52
  //require('fs').writeFileSync('./csnoutput_a2j.json', JSON.stringify(newCsn, null,2))
53
+ //console.log('CSN returned by A2J:',JSON.stringify(newCsn,null,2))
53
54
  return newCsn;
54
55
  }
55
56
 
@@ -607,6 +608,7 @@ function translateAssocsToJoins(model, inputOptions = {})
607
608
  args: [ constructPathNode( [ srcAlias, prefixFK(assoc.$elementPrefix, assoc.$flatSrcFKs[i]) ] ),
608
609
  constructPathNode( [ tgtAlias, assoc.$flatTgtFKs[i] ] ) ] });
609
610
  }
611
+ // TODO: why inner "parenthesise" - comparison in `and`?
610
612
  return parenthesise((args.length > 1 ? { op: { val: 'and' }, args: [ ...args.map(parenthesise) ] } : args[0] ));
611
613
  }
612
614
  else {
@@ -689,7 +691,8 @@ function translateAssocsToJoins(model, inputOptions = {})
689
691
  // clone ON condition with rewritten paths and substituted backlink conditions
690
692
  function cloneOnCondition(expr)
691
693
  {
692
- if(expr.op?.val === 'xpr')
694
+ const op = expr.op?.val;
695
+ if(op === 'xpr' || op === 'ixpr' || op === 'nary')
693
696
  return cloneOnCondExprStream(expr);
694
697
  else
695
698
  return cloneOnCondExprTree(expr);
@@ -700,7 +703,8 @@ function translateAssocsToJoins(model, inputOptions = {})
700
703
  const result = { op: { val: expr.op.val }, args: [ ] };
701
704
  for(let i = 0; i < args.length; i++)
702
705
  {
703
- if(args[i].op?.val === 'xpr')
706
+ const op = args[i].op?.val;
707
+ if(op === 'xpr' || op === 'ixpr' || op === 'nary')
704
708
  {
705
709
  result.args.push(cloneOnCondition(args[i]));
706
710
  }
@@ -1890,15 +1894,15 @@ function walk(node, env)
1890
1894
  function walkPath(node, env)
1891
1895
  {
1892
1896
  const path = node.path;
1893
- // Ignore paths that that have no artifact (function calls etc) or that are builtins ($now, $user)
1897
+ // Ignore paths that have no artifact (function calls etc.) or that are builtins ($now, $user)
1894
1898
  // or that are parameters ($parameters or escaped paths (':')
1895
1899
  //path.length && path[ path.length-1 ]._artifact
1896
- const art = path && path.length && path[path.length-1]._artifact;
1900
+ const art = path?.length && path[path.length-1]._artifact;
1897
1901
 
1898
1902
  // regardless of the position in the query, ignore paths that have virtual path steps
1899
- if(art && path.some(ps => ps._artifact && ps._artifact.virtual && ps._artifact.virtual.val))
1903
+ if(art && path.some(ps => ps._artifact?.virtual?.val))
1900
1904
  return path;
1901
- if(art && !internalArtifactKinds.includes(art.kind))
1905
+ if(art && !internalArtifactKinds.includes(art.kind) && node.scope !== 'param')
1902
1906
  {
1903
1907
  if(env.callback)
1904
1908
  {
@@ -21,8 +21,12 @@ function setCoreComputedOnViewsAndCalculatedElements( csn, csnUtils ) {
21
21
 
22
22
  forEachDefinition(csn, (artifact, name, prop, path) => {
23
23
  if (artifact.query || artifact.projection) {
24
+ // For events and types, the query is only used for inferring the element signature.
25
+ // There, we don't want to set `@Core.Computed`.
26
+ const queryForSignatureOnly = artifact.kind === 'type' || artifact.kind === 'event';
24
27
  forAllQueries(getNormalizedQuery(artifact).query, (query) => {
25
- if (query.SELECT)
28
+ const isTopLevelQuery = query.SELECT === (artifact.query?.SELECT || artifact.projection);
29
+ if (query.SELECT && (!isTopLevelQuery || !queryForSignatureOnly))
26
30
  traverseQueryAndAttachCoreComputed(query, query.SELECT.elements || artifact.elements);
27
31
  }, path);
28
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sap/cds-compiler",
3
- "version": "5.7.2",
3
+ "version": "5.8.0",
4
4
  "description": "CDS (Core Data Services) compiler and backends",
5
5
  "homepage": "https://cap.cloud.sap/",
6
6
  "author": "SAP SE (https://www.sap.com)",
@@ -12,6 +12,7 @@
12
12
  "redirected-to-complex",
13
13
  "redirected-to-unrelated",
14
14
  "rewrite-not-supported",
15
+ "rewrite-undefined-key",
15
16
  "syntax-expecting-unsigned-int",
16
17
  "type-missing-enum-value",
17
18
  "type-unexpected-foreign-keys",
@@ -54,3 +54,8 @@ entity View as select from Base {
54
54
  In the corrected view above, the association `secondary` gets an explicit ON
55
55
  condition. For this to work it is necessary to add `secondary_id` to the
56
56
  selection list, that means, we have to explicitly use the foreign key.
57
+
58
+
59
+ ## Related Messages
60
+
61
+ - `rewrite-undefined-key`
@@ -0,0 +1,94 @@
1
+ # rewrite-undefined-key
2
+
3
+ The compiler isn’t able to rewrite an association's foreign keys,
4
+ because the redirected target is missing elements to match them.
5
+
6
+ The message's severity is `Error`.
7
+
8
+ ## Example
9
+
10
+ Erroneous code example:
11
+
12
+ ```cds
13
+ entity model.Base {
14
+ key ID : UUID;
15
+ toTarget : Association to model.Target; // (1)
16
+ }
17
+ entity model.Target {
18
+ key ID : UUID; // (2)
19
+ field : String;
20
+ }
21
+
22
+ service S {
23
+ entity Base as projection on model.Base; // ❌ (3) Can't redirect 'toTarget'
24
+
25
+ entity Target as projection on model.Target {
26
+ field, // (4) No 'ID'
27
+ };
28
+ }
29
+ ```
30
+
31
+ In the example, the projected association `toTarget` at (3) in entity
32
+ `S.Base` can't be redirected to `S.Target`, because `S.Target` does not
33
+ project element `ID` (4).
34
+
35
+ `toTarget` (1) is a managed association and hence foreign keys are inferred
36
+ for it. The compiler generates a foreign key `ID`, which corresponds to
37
+ element `ID` of `model.Target` (2).
38
+
39
+ As both entities are exposed in service `S`, the compiler tries to redirect
40
+ `S.Base:toTarget` to an entity inside the same service, to create
41
+ a “self-contained” service. It notices, however, that `S.Target` does not
42
+ have element `ID`, and therefore can't match the foreign key to a target
43
+ element and emits this error message.
44
+
45
+ ## How to Fix
46
+
47
+ If you don't need to expose association `toTarget` in `S.Target`, you can
48
+ exclude it in the projection via an `excluding` clause.
49
+
50
+ ```cds
51
+ service S {
52
+ entity Base as projection on model.Base
53
+ excluding { toTarget };
54
+ // ...
55
+ }
56
+ ```
57
+
58
+ If the association is required in the service, you need to either project
59
+ element `ID` in `S.Target`, or redirect the association explicitly.
60
+
61
+ The easiest fix is to select `ID` explicitly:
62
+ ```cds
63
+ service S {
64
+ // ...
65
+ entity Target as projection on model.Target {
66
+ field, ID, // Explicitly select element ID
67
+ };
68
+ }
69
+ ```
70
+
71
+ However, if you don't want to expose `ID`, redirect association `toTarget`
72
+ explicitly, matching the foreign key to another element:
73
+
74
+ ```cds
75
+ service S {
76
+ entity Base as projection on model.Base {
77
+ ID,
78
+ toTarget : redirected to Target { fakeID as ID }, // (1)
79
+ };
80
+ entity Target as projection on model.Target {
81
+ calculateKey() as fakeID : UUID, // (2)
82
+ field,
83
+ };
84
+ }
85
+ ```
86
+
87
+ Note that at (1), we use element `fakeID` of `S.Target` as foreign key `ID`.
88
+ That changes its semantic meaning and may not be feasible in all cases!
89
+ In the example, we assume at (2) that a key can be calculated.
90
+
91
+
92
+ ## Related Messages
93
+
94
+ - `rewrite-not-supported`