Skip to content

Commit bd9248b

Browse files
stewsksmahatirenejeglinsky
authored
Annotation expressions go GA (#2012)
Co-authored-by: Mahati Shankar <93712176+smahati@users.noreply.github.com> Co-authored-by: Rene Jeglinsky <rene.jeglinsky@sap.com>
1 parent 7fb4ed6 commit bd9248b

2 files changed

Lines changed: 72 additions & 25 deletions

File tree

advanced/odata.md

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,21 @@ The second example is for a (record type) term in the [Communication vocabulary]
542542
```
543543

544544

545-
### Expressions <Beta /> { #expression-annotations }
545+
### Expressions { #expression-annotations }
546546

547547
If the value of an OData annotation is an [expression](../cds/cdl#expressions-as-annotation-values),
548548
the OData backend provides improved handling of references and automatic mapping from
549549
CDS expression syntax to OData expression syntax.
550550

551+
One of the main use cases for such dynamic expressions is SAP Fiori. Examples:
552+
```cds
553+
@UI.Hidden: (status <> 'visible')
554+
@UI.CreateHidden : (to_Travel.TravelStatus.code != #Open)
555+
```
556+
557+
Note that SAP Fiori supports dynamic expressions only for
558+
[specific annotations](https://ui5.sap.com/#/topic/0e7b890677c240b8ba65f8e8d417c048).
559+
551560
#### Flattening
552561

553562
In contrast to [simple references](#references), the references in expression-like
@@ -672,12 +681,54 @@ Instead of relying on this copy mechanism, you can also explicitly annotate a fo
672681
annotate Books:author.ID with @Common.Text: ($self.author.name); // here $self is necessary
673682
```
674683

675-
::: warning Restriction concerning the foreign key elements of managed associations
684+
A path that addresses a key element in the target of a managed association is always rewritten
685+
to address the local foreign key element.
676686

677-
In an expression-valued annotation, it is not possible to reference the foreign key element
678-
of a managed association.
687+
Example:
688+
```cds
689+
service S {
690+
entity Travels {
691+
key id : Integer;
692+
status : Association to TravelStatus;
693+
};
694+
entity TravelStatus {
695+
key code : String(1) enum {Open = 'O'; Accepted = 'A'; Canceled = 'X'; };
696+
}
697+
@UI.CreateHidden : (travel.status.code != #Open) // [!code highlight]
698+
entity Bookings {
699+
key id : Integer;
700+
travel : Association to Travels;
701+
}
702+
}
703+
```
704+
705+
Resulting OData API:
706+
```xml
707+
<Schema Namespace="S">
708+
<!-- ... -->
709+
<EntityType Name="Travels">
710+
<!-- ... -->
711+
<NavigationProperty Name="status" Type="S.TravelStatus"/>
712+
<Property Name="status_code" Type="Edm.String" MaxLength="1"/> <!-- [!code highlight] -->
713+
</EntityType>
714+
<EntityType Name="TravelStatus">
715+
<!-- ... -->
716+
</EntityType>
717+
<EntityType Name="Bookings">
718+
<!-- ... -->
719+
<NavigationProperty Name="travel" Type="S.Travels"/>
720+
</EntityType>
721+
<Annotations Target="S.Bookings">
722+
<Annotation Term="UI.CreateHidden">
723+
<Ne>
724+
<Path>travel/status_code</Path> <!-- [!code highlight] -->
725+
<String>O</String>
726+
</Ne>
727+
</Annotation>
728+
</Annotations>
729+
</Schema>
730+
```
679731

680-
:::
681732

682733
#### Expression Translation
683734

@@ -873,10 +924,19 @@ In any case, the resulting EDMX is:
873924
</Annotation>
874925
```
875926

876-
### Dynamic Expressions { #dynamic-expressions}
927+
### EDM JSON Expression Syntax { #dynamic-expressions}
928+
929+
::: tip Use CDS expression syntax
877930

878-
OData supports dynamic expressions in annotations.
879-
For OData annotations you can use the "edm-json inline mechanism" by providing a [dynamic expression](https://docs.oasis-open.org/odata/odata-csdl-json/v4.01/odata-csdl-json-v4.01.html#_Toc38466479) as defined
931+
Use the EDM JSON expression syntax only as fallback mechanism.
932+
Whenever possible, use [expression-like annotation values](#expression-annotations) instead.
933+
For the example below, simply write `@UI.Hidden: (status <> 'visible')`.
934+
935+
:::
936+
937+
In case you want to have an expression as value for an OData annotation that cannot be
938+
written as a [CDS expression ](#expression-annotations),
939+
you can use the "edm-json inline mechanism" by providing an [EDM JSON expression](https://docs.oasis-open.org/odata/odata-csdl-json/v4.01/odata-csdl-json-v4.01.html#_Toc38466479) as defined
880940
in the [JSON representation of the OData Common Schema Language](https://docs.oasis-open.org/odata/odata-csdl-json/v4.01/odata-csdl-json-v4.01.html) enclosed in `{ $edmJson: { ... }}`.
881941

882942
Note that here the CDS syntax for string literals with single quotes (`'foo'`) applies,
@@ -901,19 +961,6 @@ is translated to:
901961
</Annotation>
902962
```
903963

904-
One of the main use cases for such dynamic expressions is SAP Fiori,
905-
but note that SAP Fiori supports dynamic expressions only for
906-
[specific annotations](https://ui5.sap.com/#/topic/0e7b890677c240b8ba65f8e8d417c048).
907-
908-
::: tip Use expression-like annotation values
909-
910-
Instead of writing annotations directly with EDM JSON syntax,
911-
try using [expression-like annotation values](#expression-annotations), which
912-
are automatically translated. For the example above you would
913-
simply write `@UI.Hidden: (status <> 'visible')`.
914-
915-
:::
916-
917964

918965
### `sap:` Annotations
919966

cds/cdl.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ Propagation of annotations can be stopped via value `null`, for example, `@anno:
13841384
:::
13851385

13861386

1387-
### Expressions as Annotation Values <Beta /> {#expressions-as-annotation-values}
1387+
### Expressions as Annotation Values {#expressions-as-annotation-values}
13881388

13891389
In order to use an expression as an annotation value, it must be enclosed in parentheses:
13901390
```cds
@@ -1481,7 +1481,7 @@ and a value written as expression `@aValueExpr: ( 11 )`, respectively.
14811481
If the annotation value is an expression, it is sometimes necessary to adapt references inside the expression
14821482
during propagation, for example, when a referenced element is renamed in a projection.
14831483
The compiler automatically takes care of the necessary rewriting. When a reference in an annotation expression
1484-
is rewritten, the `=` property is set to `true`.
1484+
is rewritten, the `=` property is adapted accordingly if the expression is a single reference, otherwise it is set to `true`.
14851485

14861486
Example:
14871487
```cds
@@ -1530,9 +1530,9 @@ rewritten to `@Common.Text: (descr)`.
15301530

15311531
::: info
15321532

1533-
There are situations where automatic rewriting doesn't work, resulting in the compiler error
1533+
There may be situations where automatic rewriting doesn't work, resulting in the compiler error
15341534
[`anno-missing-rewrite`](https://cap.cloud.sap/docs/cds/compiler/messages#anno-missing-rewrite).
1535-
Some of these situations are going to be addressed in upcoming releases.
1535+
In these cases you can overwrite the annotation with the correct expression in the new location.
15361536

15371537
:::
15381538

0 commit comments

Comments
 (0)