cancel
Showing results for 
Search instead for 
Did you mean: 

Problem accessing single entity using OData v4

ceedee666
Active Contributor

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

ceedee666
Active Contributor

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

Accepted Solutions (0)

Answers (3)

Answers (3)

ceedee666
Active Contributor

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

vobu
Active Contributor
0 Kudos

did you switch back to the alternate keys definition?

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

Would be interesting to see whether the standard approach defined in http://docs.oasis-open.org/odata/odata/v4.01/csprd05/part2-url-conventions/odata-v4.01-csprd05-part2... actually works in your case so that calling both

.../SurveyResponses(ID='some-key-string')
.../SurveyRespones(orderid=4711)
// or even
.../SurveyResponses('some-key-string')
.../SurveyRespones(4711)

would actually work as expected.

PS: can we please have a regular markdown editor in here? grrr.

ceedee666
Active Contributor
0 Kudos

Hi vobu,

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

pierre_dominique2
Contributor

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

ceedee666
Active Contributor
0 Kudos

Hi pedrodem,

thanks for the response.

Without single quotes I get the following error message: null'E65A56807C68EFCA160009027C5C71B7' is not a valid key. This error originates form the method _parseCompoundKey.

Best
Christian

former_member681427
Participant

This method not work now.

The entity defined with UUID, and then the remove the single quote in url:

entity SurveyResponses {
     key ID: UUID;

The url should combined like this:

https://<OData service>/survey/SurveyResponses(ID=056ee329-1c04-4e3a-a5e0-ce816bf8e58c,IsActiveEntity=true)