cancel
Showing results for 
Search instead for 
Did you mean: 

CAP / CDS Nested JSON structures

former_member743968
Participant
0 Kudos

Does CAP / CDS allow us to expose 1 to 1 nested JSON objects? I see that defining a to many relationship creates an object array however, I am trying to expose nested objects without created 1 to n relationship.

  1. @sap/cds: "5.8.0
  2. cds2types: "^2.7.2"
  3. cds-swagger-ui-express: "^0.3.0"

To many scenerio:

To one scenerio causes for the entire entity to flatten on itself. I need to be able to control how whether included structures are flattened or nested.

desired structure:

{
   "ShipRequest": {
      "Pickup":{
         "Carrier":"string",
         ...
      }
   }
}<br>
View Entire Topic
david_kunz2
Advisor
Advisor

Hi akloske ,

You can also model 1-to-1 relations, does that fit your use case?

https://cap.cloud.sap/docs/cds/cdl#managed-associations

In your request, you can then perform an expand on the child entity.

Best regards,
David

former_member743968
Participant
0 Kudos

david.kunz2 This isn't ideal for my scenerio because I want to expose a virtual type with deep structure as an inbound API. I wanted to use the cds definitions to generate typescript definitions for mapping data (data will be normalized to another CDS model before it is peristed).

To use, the managed associations above, It looks like I'll need to convert all of my nested types into entities, assign each a primary key and expose them to to my odata service. This also causes swagger UI documentation to lose the ability to show the full structure of my API.

Do you know if it's possible to accomplish my original goal using CDS without converting all types to persistent entities and assigning primary keys to each?

david_kunz2
Advisor
Advisor

I see, you can have structured types, i.e.

entity Foo {

key ID: UUID;
myStruct: {
someField: Integer;
someField2: Integer;
}

See here for more information:
https://cap.cloud.sap/docs/releases/sep20#structured-elements-in-odata-v4-for-apis

former_member743968
Participant
0 Kudos

david.kunz2 Thank you for this information. Does SAP have any samples of this being implemented or more details on how to activate this feature?

I'm struggling with implementing structured types with odata flavor = "x4".

Nest a json structure into entity:

run cds.compile(csn).for.odata('x4).

metadata still flattens structures:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<script/>
<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.xml">
<edmx:Include Alias="Core" Namespace="Org.OData.Core.V1"/>
</edmx:Reference>
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="ShipExecService">
<EntityContainer Name="EntityContainer">
<EntitySet Name="Shipments" EntityType="ShipExecService.Shipments"/>
</EntityContainer>
<EntityType Name="Shipments">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.Guid" Nullable="false"/>
<Property Name="type_field1" Type="Edm.String"/>
<Property Name="type_field2" Type="Edm.String"/>
</EntityType>
<Annotations Target="ShipExecService.Shipments/ID">
<Annotation Term="Core.Computed" Bool="true"/>
</Annotations>
<Annotations Target="ShipExecService.Shipments/type_field1">
<Annotation Term="Core.Computed" Bool="true"/>
</Annotations>
<Annotations Target="ShipExecService.Shipments/type_field2">
<Annotation Term="Core.Computed" Bool="true"/>
</Annotations>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">"); vertical-align: bottom; height: 10px;">