@sap/cds-compiler 4.0.2 → 4.2.2

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 (101) hide show
  1. package/CHANGELOG.md +200 -5
  2. package/bin/cdsc.js +18 -15
  3. package/doc/CHANGELOG_BETA.md +16 -0
  4. package/doc/CHANGELOG_DEPRECATED.md +15 -0
  5. package/lib/api/main.js +33 -13
  6. package/lib/api/options.js +2 -2
  7. package/lib/api/validate.js +25 -25
  8. package/lib/base/location.js +6 -7
  9. package/lib/base/message-registry.js +123 -42
  10. package/lib/base/messages.js +18 -10
  11. package/lib/base/model.js +43 -10
  12. package/lib/checks/defaultValues.js +6 -6
  13. package/lib/checks/elements.js +11 -10
  14. package/lib/checks/foreignKeys.js +0 -5
  15. package/lib/checks/manyNavigations.js +33 -0
  16. package/lib/checks/onConditions.js +22 -14
  17. package/lib/checks/queryNoDbArtifacts.js +132 -73
  18. package/lib/checks/selectItems.js +4 -55
  19. package/lib/checks/sql-snippets.js +15 -4
  20. package/lib/checks/types.js +3 -3
  21. package/lib/checks/utils.js +4 -3
  22. package/lib/checks/validator.js +3 -1
  23. package/lib/compiler/.eslintrc.json +2 -1
  24. package/lib/compiler/assert-consistency.js +71 -40
  25. package/lib/compiler/base.js +7 -2
  26. package/lib/compiler/builtins.js +40 -41
  27. package/lib/compiler/checks.js +415 -367
  28. package/lib/compiler/classes.js +62 -0
  29. package/lib/compiler/cycle-detector.js +9 -9
  30. package/lib/compiler/define.js +124 -90
  31. package/lib/compiler/extend.js +115 -88
  32. package/lib/compiler/finalize-parse-cdl.js +26 -25
  33. package/lib/compiler/generate.js +57 -49
  34. package/lib/compiler/index.js +56 -56
  35. package/lib/compiler/kick-start.js +10 -7
  36. package/lib/compiler/moduleLayers.js +1 -1
  37. package/lib/compiler/populate.js +180 -144
  38. package/lib/compiler/propagator.js +10 -9
  39. package/lib/compiler/resolve.js +321 -246
  40. package/lib/compiler/shared.js +812 -433
  41. package/lib/compiler/tweak-assocs.js +114 -50
  42. package/lib/compiler/utils.js +241 -46
  43. package/lib/edm/.eslintrc.json +40 -1
  44. package/lib/edm/annotations/genericTranslation.js +721 -707
  45. package/lib/edm/annotations/preprocessAnnotations.js +88 -77
  46. package/lib/edm/csn2edm.js +389 -378
  47. package/lib/edm/edm.js +679 -770
  48. package/lib/edm/edmAnnoPreprocessor.js +132 -146
  49. package/lib/edm/edmInboundChecks.js +29 -27
  50. package/lib/edm/edmPreprocessor.js +689 -648
  51. package/lib/edm/edmUtils.js +279 -300
  52. package/lib/gen/Dictionary.json +34 -10
  53. package/lib/gen/language.checksum +1 -1
  54. package/lib/gen/language.interp +1 -1
  55. package/lib/gen/languageParser.js +2857 -2856
  56. package/lib/json/from-csn.js +77 -51
  57. package/lib/json/to-csn.js +15 -15
  58. package/lib/language/antlrParser.js +2 -1
  59. package/lib/language/genericAntlrParser.js +52 -43
  60. package/lib/language/language.g4 +61 -64
  61. package/lib/language/multiLineStringParser.js +2 -0
  62. package/lib/main.d.ts +65 -0
  63. package/lib/model/csnRefs.js +37 -19
  64. package/lib/model/csnUtils.js +51 -18
  65. package/lib/model/revealInternalProperties.js +30 -22
  66. package/lib/modelCompare/compare.js +149 -41
  67. package/lib/modelCompare/utils/filter.js +55 -25
  68. package/lib/optionProcessor.js +21 -9
  69. package/lib/render/manageConstraints.js +20 -17
  70. package/lib/render/toCdl.js +63 -23
  71. package/lib/render/toHdbcds.js +2 -2
  72. package/lib/render/toRename.js +4 -9
  73. package/lib/render/toSql.js +82 -35
  74. package/lib/render/utils/common.js +11 -9
  75. package/lib/render/utils/unique.js +52 -0
  76. package/lib/transform/db/applyTransformations.js +62 -21
  77. package/lib/transform/db/assertUnique.js +7 -8
  78. package/lib/transform/db/associations.js +2 -2
  79. package/lib/transform/db/cdsPersistence.js +9 -9
  80. package/lib/transform/db/constraints.js +47 -17
  81. package/lib/transform/db/expansion.js +138 -68
  82. package/lib/transform/db/flattening.js +98 -30
  83. package/lib/transform/db/rewriteCalculatedElements.js +20 -14
  84. package/lib/transform/db/temporal.js +1 -1
  85. package/lib/transform/db/transformExists.js +8 -7
  86. package/lib/transform/db/views.js +73 -33
  87. package/lib/transform/draft/db.js +11 -9
  88. package/lib/transform/draft/odata.js +1 -1
  89. package/lib/transform/{forOdataNew.js → forOdata.js} +10 -7
  90. package/lib/transform/forRelationalDB.js +148 -136
  91. package/lib/transform/localized.js +92 -54
  92. package/lib/transform/odata/toFinalBaseType.js +3 -3
  93. package/lib/transform/{transformUtilsNew.js → transformUtils.js} +13 -111
  94. package/lib/transform/translateAssocsToJoins.js +14 -28
  95. package/lib/utils/file.js +7 -7
  96. package/lib/utils/moduleResolve.js +210 -121
  97. package/lib/utils/objectUtils.js +1 -1
  98. package/package.json +5 -5
  99. package/share/messages/check-proper-type-of.md +1 -1
  100. package/share/messages/{check-proper-type.md → def-missing-type.md} +3 -5
  101. package/share/messages/message-explanations.json +1 -1
@@ -3,12 +3,16 @@
3
3
  const { setProp } = require('../../base/model');
4
4
  const { CompilerAssertion } = require('../../base/error');
5
5
  const {
6
- forEachDefinition, applyTransformationsOnNonDictionary, applyTransformationsOnDictionary, implicitAs, cloneCsnNonDict, getUtils,
6
+ forEachDefinition,
7
+ applyTransformationsOnNonDictionary,
8
+ applyTransformationsOnDictionary,
9
+ implicitAs,
10
+ cloneCsnNonDict,
7
11
  forEachMemberRecursively,
8
12
  } = require('../../model/csnUtils');
9
13
  const { getBranches } = require('./flattening');
10
14
  const { getColumnMap } = require('./views');
11
- const { checkForeignKeyAccess } = require('../../checks/onConditions');
15
+ const { requireForeignKeyAccess } = require('../../checks/onConditions');
12
16
 
13
17
  const cloneCsnOptions = { hiddenPropertiesToClone: [ '_art', '_links', '$env', '$scope' ] };
14
18
 
@@ -19,11 +23,13 @@ const cloneCsnOptions = { hiddenPropertiesToClone: [ '_art', '_links', '$env', '
19
23
  *
20
24
  * @param {CSN.Model} csn
21
25
  * @param {CSN.Options} options
26
+ * @param {object} csnUtils
22
27
  * @param {string} pathDelimiter
23
- * @param {Function} error
28
+ * @param {object} messageFunctions
24
29
  */
25
- function rewriteCalculatedElementsInViews( csn, options, pathDelimiter, error ) {
26
- const { inspectRef, effectiveType } = getUtils(csn, 'init-all');
30
+ function rewriteCalculatedElementsInViews( csn, options, csnUtils, pathDelimiter, messageFunctions ) {
31
+ const { inspectRef, effectiveType } = csnUtils;
32
+ const { error } = messageFunctions;
27
33
 
28
34
  const views = [];
29
35
  const entities = [];
@@ -121,7 +127,7 @@ function rewriteCalculatedElementsInViews( csn, options, pathDelimiter, error )
121
127
  }
122
128
  else {
123
129
  // It's a managed association - access of the foreign keys is allowed
124
- checkForeignKeyAccess(parent, i, csnPath, (errorIndex) => {
130
+ requireForeignKeyAccess(parent, i, (errorIndex) => {
125
131
  error('ref-unexpected-navigation', csnPath, {
126
132
  '#': 'calc-non-fk', id, elemref: parent, name: value[errorIndex].id || value[errorIndex],
127
133
  });
@@ -412,7 +418,7 @@ function rewriteCalculatedElementsInViews( csn, options, pathDelimiter, error )
412
418
  * @param {CSN.QuerySelect} SELECT
413
419
  * @returns {object}
414
420
  */
415
- function getDirectlyAdressableElements( SELECT ) {
421
+ function getDirectlyAddressableElements( SELECT ) {
416
422
  const { from } = SELECT;
417
423
  if (from.ref) {
418
424
  return from._art.elements;
@@ -422,7 +428,7 @@ function rewriteCalculatedElementsInViews( csn, options, pathDelimiter, error )
422
428
  }
423
429
  else if (from.SET) {
424
430
  // args[0] could be SELECT or UNION
425
- return getDirectlyAdressableElements({ from: from.SET.args[0] });
431
+ return getDirectlyAddressableElements({ from: from.SET.args[0] });
426
432
  }
427
433
  else if (from.args) {
428
434
  const mergedElements = Object.create(null);
@@ -432,7 +438,7 @@ function rewriteCalculatedElementsInViews( csn, options, pathDelimiter, error )
432
438
  mergedElements[elementName] = arg._art.elements[elementName];
433
439
  }
434
440
  else if (arg.SET) {
435
- return getDirectlyAdressableElements({ from: arg.SET.args[0] });
441
+ return getDirectlyAddressableElements({ from: arg.SET.args[0] });
436
442
  }
437
443
  else if (arg.SELECT) { // TODO: UNION
438
444
  for (const elementName in arg.SELECT.elements)
@@ -440,7 +446,7 @@ function rewriteCalculatedElementsInViews( csn, options, pathDelimiter, error )
440
446
  }
441
447
  else if (arg.args) { // TODO: Is it safe to do recursion here?
442
448
  for (const subarg of arg.args) {
443
- const elements = getDirectlyAdressableElements({ from: subarg });
449
+ const elements = getDirectlyAddressableElements({ from: subarg });
444
450
  for (const elementName in elements)
445
451
  mergedElements[elementName] = elements[elementName];
446
452
  }
@@ -465,8 +471,8 @@ function rewriteCalculatedElementsInViews( csn, options, pathDelimiter, error )
465
471
  */
466
472
  function makeAllCalculatedElementsExplicitColumns( elements, SELECT, containsExpandInline ) {
467
473
  const cleanupCallbacks = [];
468
- const root = getDirectlyAdressableElements(SELECT);
469
- const columnMap = getColumnMap( { SELECT });
474
+ const root = getDirectlyAddressableElements(SELECT);
475
+ const columnMap = getColumnMap( { SELECT }, csnUtils );
470
476
  const hasStar = SELECT.columns.includes('*');
471
477
  const unfoldingMap = {};
472
478
  let starContainsCalculated = false;
@@ -699,9 +705,9 @@ function processCalculatedElementsInEntities( csn ) {
699
705
  */
700
706
  function removeDummyValueInEntity( artifact, path ) {
701
707
  applyTransformationsOnDictionary(artifact.elements, {
702
- value: (parent, prop, value, p, root) => {
708
+ value: (parent, prop, value, p, elements) => {
703
709
  if (!value.stored)
704
- delete root[p[p.length - 1]];
710
+ delete elements[p[p.length - 1]];
705
711
  },
706
712
  }, {}, path.concat( 'elements' ));
707
713
  }
@@ -5,7 +5,7 @@ const {
5
5
  } = require('../../model/csnUtils');
6
6
  const { implicitAs } = require('../../model/csnRefs');
7
7
  const { setProp } = require('../../base/model');
8
- const { getTransformers } = require('../transformUtilsNew');
8
+ const { getTransformers } = require('../transformUtils');
9
9
 
10
10
  const validToString = '@cds.valid.to';
11
11
  const validFromString = '@cds.valid.from';
@@ -53,22 +53,23 @@ const { ModelError } = require('../../base/error');
53
53
  function handleExists( csn, options, error, inspectRef, initDefinition, dropDefinitionCache ) {
54
54
  const generatedExists = new WeakMap();
55
55
  forEachDefinition(csn, (artifact, artifactName) => {
56
+ // drop cache: Otherwise, the projection/query hack below won't work, because csnRefs
57
+ // thinks that the artifact was already initialized (including all queries).
58
+ dropDefinitionCache(artifact);
56
59
  if (artifact.projection) // do the same hack we do for the other stuff...
57
60
  artifact.query = { SELECT: artifact.projection };
58
61
 
59
62
  if (artifact.query) {
60
- forAllQueries(artifact.query, (query, path) => {
63
+ forAllQueries(artifact.query, function handleExistsQuery(query, path) {
61
64
  if (!generatedExists.has(query)) {
62
65
  const toProcess = []; // Collect all expressions we need to process here
63
- if (query.SELECT && query.SELECT.where && query.SELECT.where.length > 1)
66
+ if (query.SELECT?.where?.length > 1)
64
67
  toProcess.push([ path.slice(0, -1), path.concat('where') ]);
65
68
 
66
-
67
- if (query.SELECT && query.SELECT.columns)
69
+ if (query.SELECT?.columns)
68
70
  toProcess.push([ path.slice(0, -1), path.concat('columns') ]);
69
71
 
70
-
71
- if (query.SELECT && query.SELECT.from.on )
72
+ if (query.SELECT?.from.on)
72
73
  toProcess.push([ path.slice(0, -1), path.concat([ 'from', 'on' ]) ]);
73
74
 
74
75
  for (const [ , exprPath ] of toProcess) {
@@ -268,7 +269,7 @@ function handleExists( csn, options, error, inspectRef, initDefinition, dropDefi
268
269
 
269
270
  const { links } = inspectRef(exprPath.concat(i));
270
271
 
271
- const assocs = links.filter(link => link.art && link.art.target).map(link => current.ref[link.idx]);
272
+ const assocs = links.filter(link => link.art?.target).map(link => current.ref[link.idx]);
272
273
 
273
274
  checkForInvalidAssoc(assocs);
274
275
  }
@@ -1,10 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  const {
4
- getUtils, cloneCsnNonDict, applyTransformationsOnNonDictionary,
4
+ getUtils, cloneCsnNonDict, applyTransformationsOnNonDictionary, forEachDefinition,
5
5
  } = require('../../model/csnUtils');
6
- const { implicitAs } = require('../../model/csnRefs');
6
+ const { implicitAs, columnAlias } = require('../../model/csnRefs');
7
7
  const { ModelError } = require('../../base/error');
8
+ const { setProp } = require('../../base/model');
8
9
 
9
10
  /**
10
11
  * If a mixin association is published, return the mixin association.
@@ -66,10 +67,11 @@ function usesMixinAssociation( query, association, associationName ) {
66
67
  * @returns {(query: CSN.Query, artifact: CSN.Artifact, artName: string, path: CSN.Path) => void} Transformer function for views
67
68
  */
68
69
  function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
70
+ const csnUtils = getUtils(csn);
69
71
  const {
70
72
  get$combined, isAssocOrComposition,
71
73
  inspectRef, queryOrMain, // csnRefs
72
- } = getUtils(csn);
74
+ } = csnUtils;
73
75
  const pathDelimiter = options.forHana && (options.sqlMapping === 'hdbcds') ? '.' : '_';
74
76
  const { error, info } = messageFunctions;
75
77
  const doA2J = !(options.transformation === 'hdbcds' && options.sqlMapping === 'hdbcds');
@@ -213,7 +215,7 @@ function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
213
215
  else
214
216
  info('query-ignoring-assoc-in-union', queryPath, { name: elemName, '#': 'std' });
215
217
 
216
- elem._ignore = true;
218
+ elem.$ignore = true;
217
219
  }
218
220
  else {
219
221
  error(null, queryPath, { name: elemName }, 'Association $(NAME) can\'t be published in a SAP HANA CDS UNION');
@@ -270,7 +272,7 @@ function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
270
272
  }, {}, elementsPath.concat(elemName));
271
273
  }
272
274
 
273
- if (!mixinElem._ignore)
275
+ if (!mixinElem.$ignore)
274
276
  columnMap[elemName] = { ref: [ mixinElemName ], as: elemName };
275
277
 
276
278
  if (query.SELECT) {
@@ -311,7 +313,7 @@ function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
311
313
 
312
314
 
313
315
  /**
314
- * Loop over the columns and call all of the given functions with the column and the path
316
+ * Loop over the columns and call all the given functions with the column and the path
315
317
  *
316
318
  * @param {Function[]} functions
317
319
  * @param {CSN.Column[]} columns
@@ -332,6 +334,7 @@ function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
332
334
  */
333
335
  // eslint-disable-next-line complexity
334
336
  function transformViewOrEntity( query, artifact, artName, path ) {
337
+ csnUtils.initDefinition(artifact);
335
338
  const { elements } = queryOrMain(query, artifact);
336
339
  // We use the elements from the leading query/main artifact - adapt the path
337
340
  const elementsPath = elements === artifact.elements ? path.slice(0, 2).concat('elements') : path.concat('elements');
@@ -340,7 +343,7 @@ function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
340
343
  let hasNonAssocElements = false;
341
344
  const isSelect = query && query.SELECT;
342
345
  const isProjection = !!artifact.projection || query && query.SELECT && !query.SELECT.columns;
343
- const columnMap = getColumnMap(query);
346
+ const columnMap = getColumnMap(query, csnUtils);
344
347
  const isSelectStar = query && query.SELECT && query.SELECT.columns && query.SELECT.columns.indexOf('*') !== -1;
345
348
 
346
349
  // check all queries/subqueries for mixin publishing inside of unions -> forbidden in hdbcds
@@ -365,7 +368,7 @@ function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
365
368
  addForeignKeysToColumns(columnMap, elem, elemName);
366
369
  }
367
370
  // Views must have at least one element that is not an unmanaged assoc
368
- if (!elem.on && !elem._ignore)
371
+ if (!elem.on && !elem.$ignore)
369
372
  hasNonAssocElements = true;
370
373
 
371
374
  // (180 b) Create MIXINs for association elements in projections or views (those that are not mixins by themselves)
@@ -383,7 +386,7 @@ function getViewTransformer( csn, options, messageFunctions, transformCommon ) {
383
386
 
384
387
  if (isSelect) {
385
388
  // Build new columns from the column map - bring elements and columns back in sync basically
386
- query.SELECT.columns = Object.keys(elements).filter(elem => !elements[elem]._ignore).map(key => stripLeadingSelf(columnMap[key]));
389
+ query.SELECT.columns = Object.keys(elements).filter(elem => !elements[elem].$ignore).map(key => stripLeadingSelf(columnMap[key]));
387
390
  // If following an association, explicitly set the implicit alias
388
391
  // due to an issue with HANA - this seems to only have an effect on ref files with hdbcds-hdbcds, so only run then
389
392
  const columnProcessors = [];
@@ -476,45 +479,82 @@ function getLastRefStepString( ref ) {
476
479
  }
477
480
 
478
481
  /**
479
- * Build a map of the resulting names (i.e. the element name of the column) and references to the respective columns
482
+ * This function is similar to csnRefs()' `columnName()`, but does not split the
483
+ * last `col.ref` segment on `.`.
480
484
  *
485
+ * TODO: The HDBCDS backend relies on this. Also the HDI backend relies
486
+ * on this for virtual elements somehow. That can probably be fixed
487
+ * by using csnRefs()'s `getElement()`.
488
+ * TODO: Remove this function; update HDBCDS/HDI
489
+ *
490
+ * @param {CSN.Column} col
491
+ * @returns {string}
492
+ */
493
+ function columnNameForMap( col ) {
494
+ return col.as || (!col.args && col.func) || (col.ref && getLastRefStepString( col.ref ));
495
+ }
496
+
497
+ /**
498
+ * Build a map of the resulting names (i.e. the element name of the column) and references
499
+ * to the respective columns.
481
500
  * This can later be used to match from elements to columns.
482
501
  *
483
502
  * @param {CSN.Query} query
503
+ * @param {object} csnUtils
484
504
  * @returns {object}
485
505
  */
486
- function getColumnMap( query ) {
506
+ function getColumnMap( query, csnUtils ) {
487
507
  const map = Object.create(null);
488
- if (query && query.SELECT && query.SELECT.columns) {
508
+ if (query?.SELECT?.columns) {
489
509
  query.SELECT.columns.forEach((col) => {
490
- if (col === '*') {
491
- // do nothing
492
- }
493
- else if (col.as) {
494
- if (!map[col.as])
495
- map[col.as] = col;
496
- }
497
- else if (col.ref) {
498
- // .id on last path step can happen with hdbcds.hdbcds and malicious CSN input - maybe also with params?
499
- // We made things right in the end with the second add of missing stuff, but why not do it
500
- // right from the getgo
501
- const last = getLastRefStepString(col.ref);
502
- if (!map[last])
503
- map[last] = col;
504
- }
505
- else if (col.func) {
506
- map[col.func] = col;
507
- }
508
- else if (!map[col]) {
509
- map[col] = col;
510
+ if (col !== '*') {
511
+ // Fallback to csnUtils for columns without any alias (internal one is created)
512
+ const as = columnNameForMap(col) || csnUtils.getColumnName( col );
513
+ if (as && !map[as])
514
+ map[as] = col;
510
515
  }
511
516
  });
512
517
  }
513
-
514
518
  return map;
515
519
  }
516
520
 
521
+
522
+ /**
523
+ * Ensure that each column in the CSN has a name. A column does not have
524
+ * a name if the column is an expression and there is no explicit alias.
525
+ * In that case an internal alias (from csnRefs()) is used and made explicit
526
+ * via non-enumerable `as`.
527
+ *
528
+ * For HDBCDS, the alias is made explicit as an enumerable property, because
529
+ * HDBCDS does not support expressions as columns without aliases.
530
+ *
531
+ * Notes:
532
+ * - The alias is removed after A2J: we rely on the compiler ignoring non-enumerable CSN properties.
533
+ * - We can't use e.g. `$as`, as csnRefs() does not use that property, and it must not
534
+ * invent another name for the column (could happen after flattening).
535
+ *
536
+ * @param {CSN.Model} csn
537
+ * @param {CSN.Options} options
538
+ * @param {object} csnUtils
539
+ */
540
+ function ensureColumnNames( csn, options, csnUtils ) {
541
+ forEachDefinition(csn, (def) => {
542
+ csnUtils.initDefinition(def);
543
+ for (const query of csnUtils.$getQueries(def) || []) {
544
+ for (const col of query._select.columns || []) {
545
+ if (col !== '*' && !columnAlias(col)) {
546
+ if (options.transformation === 'hdbcds')
547
+ col.as = csnUtils.getColumnName(col);
548
+ else
549
+ setProp(col, 'as', csnUtils.getColumnName(col));
550
+ }
551
+ }
552
+ }
553
+ });
554
+ }
555
+
517
556
  module.exports = {
518
557
  getViewTransformer,
519
558
  getColumnMap,
559
+ ensureColumnNames,
520
560
  };
@@ -5,7 +5,7 @@ const {
5
5
  getResultingName, forEachMemberRecursively,
6
6
  } = require('../../model/csnUtils');
7
7
  const { setProp, isDeprecatedEnabled } = require('../../base/model');
8
- const { getTransformers } = require('../transformUtilsNew');
8
+ const { getTransformers } = require('../transformUtils');
9
9
  const { ModelError } = require('../../base/error');
10
10
  const draftAnnotation = '@odata.draft.enabled';
11
11
  const booleanBuiltin = 'cds.Boolean';
@@ -124,16 +124,18 @@ function generateDrafts( csn, options, pathDelimiter, messageFunctions ) {
124
124
  error(null, path, 'Expecting element to have a type when used in a draft-enabled artifact');
125
125
  if (elt.key && elt.key === true && !elt.virtual)
126
126
  keys.push(elt);
127
- }, [ 'definitions', artifactName ], true, { elementsOnly: true });
127
+ }, [ 'definitions', artifactName ], false, { elementsOnly: true });
128
128
 
129
129
  // In contrast to EDM, the DB entity may have more than one technical keys but should have ideally exactly one key of type cds.UUID
130
- if (keys.length !== 1)
131
- warning(null, [ 'definitions', artifactName ], 'Entity annotated with “@odata.draft.enabled” should have exactly one key element');
132
-
133
- const uuidCount = keys.reduce((acc, k) => ((k.type === 'cds.String' && k.$renamed === 'cds.UUID' && k.length === 36) ? acc + 1 : acc), 0);
134
- if (uuidCount === 0)
135
- warning(null, [ 'definitions', artifactName ], 'Entity annotated with “@odata.draft.enabled” should have one key element of type “cds.UUID”');
136
-
130
+ if (keys.length !== 1) {
131
+ warning(null, [ 'definitions', artifactName ], { count: keys.length },
132
+ 'Entity annotated with “@odata.draft.enabled” should have exactly one key element, but found $(COUNT)');
133
+ }
134
+ else {
135
+ const uuidCount = keys.reduce((acc, k) => ((k.type === 'cds.String' && k.$renamed === 'cds.UUID' && k.length === 36) ? acc + 1 : acc), 0);
136
+ if (uuidCount === 0)
137
+ warning(null, [ 'definitions', artifactName ], 'Entity annotated with “@odata.draft.enabled” should have one key element of type “cds.UUID”');
138
+ }
137
139
 
138
140
  // Ignore boolean return value. We know that we're inside a service or else we wouldn't have reached this code.
139
141
  const matchingService = getMatchingService(artifactName) || '';
@@ -3,7 +3,7 @@
3
3
  const { forEachDefinition, getServiceNames } = require('../../model/csnUtils');
4
4
  const { forEach } = require('../../utils/objectUtils');
5
5
  const { isArtifactInSomeService, getServiceOfArtifact } = require('../odata/utils');
6
- const { getTransformers } = require('../transformUtilsNew');
6
+ const { getTransformers } = require('../transformUtils');
7
7
  const { ModelError } = require('../../base/error');
8
8
  const { makeMessageFunction } = require('../../base/messages');
9
9
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  const { makeMessageFunction } = require('../base/messages');
4
4
  const { isDeprecatedEnabled, isBetaEnabled } = require('../base/model');
5
- const transformUtils = require('./transformUtilsNew');
5
+ const transformUtils = require('./transformUtils');
6
6
  const { cloneCsnNonDict,
7
7
  forEachDefinition,
8
8
  forEachMemberRecursively,
@@ -152,6 +152,8 @@ function transform4odataWithCsn(inputModel, options) {
152
152
  { skipArtifact: isExternalServiceMember }
153
153
  );
154
154
 
155
+ flattening.linkForeignKeyAnnotationExtensionsToAssociation(csn, options);
156
+
155
157
  // All type refs must be resolved, including external APIs.
156
158
  // OData has no 'type of' so 'real' imported OData APIs marked @cds.external are safe.
157
159
  // If in the future 'other' APIs that might support type refs are imported, these refs must be
@@ -169,6 +171,7 @@ function transform4odataWithCsn(inputModel, options) {
169
171
  expansion.expandStructureReferences(csn, options, '_', { error, info, throwWithAnyError }, csnUtils, { skipArtifact: isExternalServiceMember });
170
172
  const resolved = new WeakMap();
171
173
  // No refs with struct-steps exist anymore
174
+
172
175
  flattening.flattenAllStructStepsInRefs(csn, options, resolved, '_', { skipArtifact: isExternalServiceMember });
173
176
  // No type references exist anymore
174
177
  // Needs to happen exactly between flattenAllStructStepsInRefs and flattenElements to keep model resolvable.
@@ -389,14 +392,14 @@ function transform4odataWithCsn(inputModel, options) {
389
392
 
390
393
  // Only on element level
391
394
  if(node.kind == null) {
392
- if (node['@mandatory']&& node['@Common.FieldControl'] === undefined) {
395
+ if (node['@mandatory'] && !Object.entries(node).some(([k,v]) => k === '@Common.FieldControl' || k.startsWith('@Common.FieldControl.') && v != null)) {
393
396
  setAnnotation(node, '@Common.FieldControl', { '#': 'Mandatory' });
394
397
  }
395
- if (node['@assert.range'] != null) {
396
- if (Array.isArray(node['@assert.range']) && node['@assert.range'].length === 2) {
397
- setAnnotation(node, '@Validation.Minimum', node['@assert.range'][0]);
398
- setAnnotation(node, '@Validation.Maximum', node['@assert.range'][1]);
399
- }
398
+ if (node['@assert.range'] != null &&
399
+ (Array.isArray(node['@assert.range']) &&
400
+ node['@assert.range'].length === 2)) {
401
+ setAnnotation(node, '@Validation.Minimum', node['@assert.range'][0]);
402
+ setAnnotation(node, '@Validation.Maximum', node['@assert.range'][1]);
400
403
  }
401
404
  }
402
405
  }