Search code examples
sapui5

Expand mock data with UI5, JSON files


I'm trying to mock some data based on existing and working oData models. The Mock Server works, but I am struggling making the $expand to do it's job. I think it's mostly a matter of me not understanding where or how to store the JSON.

The metadata file is copied verbatim from the working service and contains all entities, entitysets, associations etcetera.

Here are some of the relevant bits. From Users entity:

<NavigationProperty Name="Dealers" Relationship="Y_DP_CORE_SRV.User_Dealer" FromRole="FromRole_User_Dealer" ToRole="ToRole_User_Dealer" />

The association:

<Association Name="User_Dealer" sap:content-version="1">
  <End Type="Y_DP_CORE_SRV.User" Multiplicity="1" Role="FromRole_User_Dealer" />
  <End Type="Y_DP_CORE_SRV.Dealer" Multiplicity="*" Role="ToRole_User_Dealer" />
  <ReferentialConstraint>
    <Principal Role="FromRole_User_Dealer">
      <PropertyRef Name="Id" />
    </Principal>
    <Dependent Role="ToRole_User_Dealer">
      <PropertyRef Name="Id" />
    </Dependent>
  </ReferentialConstraint>
</Association>

I can get Users('PRX-00015'). I cannot get Users('PRX-00015')/Dealers or Users('PRX-00015')?$expand=Dealers. There are no errors, but also no data.

Here's Users.JSON:

[{
    "__metadata": {
        "id": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Users('PRX-00015')",
        "uri": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Users('PRX-00015')",
        "type": "Y_DP_CORE_SRV.User"
    },
    "Id": "PRX-00015",
    "FullName": "Jorg",
    "Email": "",
    "Telephone": "",
    "InternalUser": false,
    "Enabled": true,
    "Dealers": {
        "results": [{
            "__metadata": {
                "id": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA2002')",
                "uri": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA2002')",
                "type": "Y_DP_CORE_SRV.Dealer"
            },
            "Id": "AA2002"
        }, {
            "__metadata": {
                "id": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA1046')",
                "uri": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA1046')",
                "type": "Y_DP_CORE_SRV.Dealer"
            },
            "Id": "AA1046"
        }]
    },
}]

I can also use the unexpanded version of Dealers and move the array into a Dealers.json file of it's own, in which case the line looks like

"Dealers": {
    "__deferred": {
        "uri": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Users('PRX-00015')/Dealers"
    }
}

And Dealers.json

[{
    "__metadata": {
        "id": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA2002')",
        "uri": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA2002')",
        "type": "Y_DP_CORE_SRV.Dealer"
    },
    "Id": "AA2002"
}, {
    "__metadata": {
        "id": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA1046')",
        "uri": "http://localhost/sap/opu/odata/sap/Y_DP_CORE_SRV/Dealers('AA1046')",
        "type": "Y_DP_CORE_SRV.Dealer"
    },
    "Id": "AA1046"
}]

All of these result in an empty Dealers array (Dealers.length being 0). Anyone know how this works?


Solution

  • Normally project structure looks like this:

    webapp/
     - localService/
       - mockdata/
         - Y_DP_CORE_SRV.User
         - Y_DP_CORE_SRV.Dealer.json
       - metadata.json
    

    But it doesn't really matter where your files are, it's matter how is your MockServer is implemented. The MockServer should mock real urls to your real server and return fake data from the files provided by you. Have you taken a look at this example already - https://sapui5.hana.ondemand.com/test-resources/sap/ui/templateapps/demokit/master-detail/webapp/localService/mockserver.js ?

    Regarding expands. Normally, in xml views you can expand related entities with binding property by defining expand parameter. In javascript it works similar way. I can provide few examples when I get to my computer, but It's not really related to the MockServer since all expand parameters stay the same regardless it's mockdata or real data from the real backend...

    UPDATE: here is an example how to expand models from the views and access properties:

    <QuickView
        binding="{path: 'to_Supplier', parameters: { expand: 'to_Address,to_PrimaryContactPerson' }}">
        <QuickViewPage
            title="{CompanyName}"
            description="{to_PrimaryContactPerson/FormattedContactName}">
                <QuickViewGroup>
                    <QuickViewGroupElement value="{to_Address/FormattedAddress}"/>
                </QuickViewGroup>
        </QuickViewPage>
    </QuickView>