Skip to Content

Problem accessing single entity using OData v4

Hi all,

I created a simple project using the CAP and Node.js. The sources are available here: https://github.com/ceedee666/rqk_ws19_v3/. My project just consist of a single CDS entity called SurveyResponses, which is exposed via a service. The CDS of the entity looks like this:

namespace de.fhaachen.rqkws19;
entity SurveyResponses {   key ID : UUID;   orderid : Integer;   orderDate : DateTime;   ...}

After adding some dummy data to the database using an SQL script I' able to access all the entities in the DB using this URL: /survey/SurveyResponses

In OData v4 it is possible to address a single entity via its identifier. Here is an example with the Northwind service on the odata.org: https://services.odata.org/Experimental/OData/OData.svc/Persons(0). When I try the same using my service, e.g. using this URL /survey/SurveyResponses('E65A56807C68EFCA160009027C5C71B7'), I get the following error message:

Expected uri token 'ODataIdentifier' could not be found in 'SurveyResponses('E65A56807C68EFCA160009027C5C71B7')' at position 17

When I try to access an entity using the following $filter expression: /survey/SurveyResponses?$filter=ID eq E65A56807C68EFCA160009027C5C71B7, I get this error message

Property 'E65A56807C68EFCA160009027C5C71B7' does not exist in type 'SurveyService.SurveyResponses'

However, If I try to access the orderid property using a filter everything works as expected. The request to /survey/SurveyResponses?$filter=orderid eq 263 results in

{
  "@odata.context": "$metadata#SurveyResponses",
  "@odata.metadataEtag": "W/\"v7XePRvoicjsrRX1rhd6CPTx23lX+KlEgqRqmcp4pts=\"",
  "value": [
    {
      "ID": "E65A56807C68EFCA160009027C5C71B7",
      "orderid": 263,
      "orderDate": "2019-10-09T14:40:18Z",
      "netPromoterScore": 4,
      "review": "",
      "marketingOptIn": false,
      "responseDate": null,
      "status": 1
    }
  ]
}

Any Idea how to solve this? Did I miss something in the definition of the entity or the service?

Thanks in advance.
Christian

Add a comment
10|10000 characters needed characters exceeded

  • I changed the CDS to this

    entity SurveyResponses {
        key ID          : UUID @odata.Type:'Edm.String';
        orderid         : Integer;

    Now I'm able to access individual entities using the following URL: /survey/SurveyResponses('E65A56807C68EFCA160009027C5C71B7'). The single quotes are now required to be a vaild Edm.String.

    The underlying problem seems to be that I create the entries in the DB using the SQL function SYUUID. This function generates UUID in the format shown in the URL above. In contrast, Edm.GUID requires the following format: 'dddddddd-dddd-dddd-dddd-dddddddddddd' where each d represents [A-Fa-f0-9] (cf. https://www.odata.org/documentation/odata-version-2-0/overview/). I'll try again with the correct Edm.GUID format.

    Christian

Related questions

2 Answers

  • Posted on Oct 14, 2019 at 12:24 PM

    After adding test data in the correct format into the data base (dddddddd-dddd-dddd-dddd-dddddddddddd) the request now works as expected.

    With the URL /survey/SurveyResponses(E65A5680-7C68-EFCA-1600-09027C5C71B7) the result is

    {
      "@odata.context": "$metadata#SurveyResponses/$entity",
      "@odata.metadataEtag": "W/\"v7XePRvoicjsrRX1rhd6CPTx23lX+KlEgqRqmcp4pts=\"",
      "ID": "E65A5680-7C68-EFCA-1600-09027C5C71B7",
      "orderid": 263,
      "orderDate": "2019-10-09T14:40:18Z",
      "netPromoterScore": 4,
      "review": "",
      "marketingOptIn": false,
      "responseDate": null,
      "status": 1
    }

    Christian

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Volker Buzek,

      no, currently I have the initial version:

      entity SurveyResponses {
           key ID: UUID;

      This works as expected using a correct Edm.GUID (dddddddd-dddd-dddd-dddd-dddddddddddd). Best part is that when adding an entity using a POST request the correct GUID is created automatically.

      Having alternate keys is a great idea as well. I will try this out next and post the results here.
      But what is the difference between the two versions you posted? For me they look exactly identical, or am I missing something?

      Christian

  • Posted on Oct 14, 2019 at 11:14 AM

    Hi,

    You don't need to add single quotes. This should work:

    /survey/SurveyResponses(E65A56807C68EFCA160009027C5C71B7)

    EDIT: by default UUIDs are mapped to Edm.Guid but you can change this behavior if you want -> https://cap.cloud.sap/docs/guides/domain-models#mapping-uuids-to-odata

    Cheers,

    Pierre

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.