Technical Articles
OData V4 code based implementation – Overview
This blog is meant as an introduction of a series of blogs in which I will explain the use of the new SAP Gateway V4 framework.
It is meant for those readers that must create OData V4 series now and that cannot wait until an end-2-end support for OData V4 will be available through the new ABAP programming model.
Before starting code based OData V4 development you should check my blog OData service development options where I outline in more detail what the recommended options for OData development are right now.
Updates
-
- 13.12.2017 – added link to the first how to guide and the blog that explains the OData service devlopment options in more detail
- 19.05.2021 – published the source code on GitHub
Table of contents
This blog is part of blog series about OData V4 code based development
OData V4 code based implementation – Overview
OData V4 code based implementation I (basic interface, read access)
OData V4 code based implementation I (basic interface, create&update)
Demo system ES5
The service has been deployed in the ES5 demo system
In order to access the service you have to
Sign up for a demo account on ES5 here
More details about the ES5 demo system as such you will find in my following blog
New SAP Gateway Demo System available
Source code
The source code can be found on GitHub.
Data provider class – zcl_e2e001_odata_v4_so_data
Model provider class – zcl_e2e001_odata_v4_so_model
Exception class – zcx_e2e001_odata_v4_so
Interface – zif_e2e001_odata_v4_so_types
Consumption view – sales order – ze2e001_c_salesorder
Consumption view sales order items – ze2e001_c_salesorderitem
Interface view – ze2e001_i_salesorderitem_e
Consumption view – ze2e001_c_salesorder
What’s new in the protocol?
The main paradigm: Reduction of data
The main paradigm of the OData V4 paradigm is the reduction of data. This reduction is achieved through a more powerful query language and a new optimized JSON protocol. At the same time it is possible to leverage richter metadata as compared to OData V2.
New JSON format
The OData V4 protocol comes with a very lean JSON procol. The response payload now basically only contains name value pairs. The metadata has been reduced to a a single line
"@odata.context" : "$metadata#SalesOrder/$entity"
as opposed to the more richer metadata information in the V2 response payload, both highlighted in blue in the following figure.
Cross service references
Cross service navigation enables inter communication of services. With this, navigation properties of entities of one service can reach entities of another service in a service group. With the support of cross service navigation several requirements of SAP Fiori like applications can be addressed.
1.) The rich metadata can be leveraged but at the same time not the complete data model has to be loaded at startup time of an application. There is rather the option to have a lazy loading of parts of a service model on demand.
2.) Services can be reused more easily since services can be partioned without the loss of navigation.
Examples of such services that can be reused in various SAP Fiori applications are Users, Attachments, Conditions, Addresses, …
Please note:
Cross service references are only possible within one service group.
If a request like the following is issued:
…/SalesOrderItems(SalesOrder='500000000', SalesOrderItem='10')/_Product
one would receive a response like the following where it is indicated via the @odata.context annotation that this response stems from another service.
{
"@odata.context" : "../../sap/Product/0001/$metadata#Product/$entity",
"value" : [
{
"Product" : "HT-1000",
"Currency" : "EUR",
...
}
]
}
Support for Any and All
New is the support of the query options any and all.
With these it is now possible to find all sales orders where at least one of the items contains a particular product
…/SalesOrders?$filter = _Item/any(d:d/Product eq 'HT-1007')
or it is possible to find all sales orders where every item has a price greater than $100
…/SalesOrders?$filter = _Item/all(d:d/Price ge 100)
Filter on expanded result sets
New in OData V4 is the option for filtering on each level of an expanded enttiy set. In the following figure you see a request that
- reads all business partners that are located in ‘Walldorf’
- reads all sales orders of those business partners where the gross amount exceeds 1100 Euro
- reads only those items that contain the product HT-1041.
What’s new in the framework?
Advanced, intermediate and basic interfaces
The API that is used to develop OData V4 services has significantly changed compared to the API that is used to develop OData V2 services.
When you implement the methods of the basic interface you will get a working OData V4 service that will satisfy most requests. Complex requests such as $expand are then handled by the framework that will call the methods for the basic interface in the correct order.
The mplementation of the intermediate or advanced interface should however also be taken into account if your service implementation would be able to handle specific requests such as specific $expand or navigation calls more efficient than by calling the methods of the basic interface recursively.
Name | Purpose |
/IWBEP/IF_V4_DP_BASIC |
|
/IWBEP/IF_V4_DP_INTERMEDIATE |
|
/IWBEP/IF_V4_DP_ADVANCED |
|
/IWBEP/IF_V4_DP_BATCH |
|
/IWBEP/IF_V4_DP_PROCESS_STEPS |
|
io_request and io_response
All interface methods have an import parameter called io_request. It can be used to retrieve all information you need to handle the request in your service implemenation.
A UPDATE_ENTITY method for example will have the following methods
- GET_BUSI_DATA to retrieve entity data from the request, for example the payload of the incoming request.
- GET_ENTITY_SET to retrieve the entity set of the processed entity. So we can switch to entity set specific methods
The corresponding parameter ip_response is used to return business data to the SAP Gateway framework and to tell the framework which processing steps the service implementation has handled iself (see todo and done flags below).
Generic framework support – example $expand
As already mentioned you will get a working OData V4 service by only implementing the methods of the basic interface.If a client calls the following URL:
GET .../ze2e001_salesorder/0001/SalesOrder('500000000')?$select=Salesorder,Customer&
$expand=_Item($select=Salesorderitem,Product,Grossamountintransaccurrency,Transactioncurrency)
the SAP Gateway framework will call the following basic methods in your service implementation:
# | Method call | Purpose |
1 | …_BASIC~READ_ENTITY | This method will read the data of the sales order header |
2 | …_BASIC~READ_REF_TARGET_KEY_DATA | This method will read the key fields of the items that can be used by the READ_ENTITY_LIST method as a $filter statement |
3 | …_BASIC~READ_ENTITY_LIST |
This method reads the items.
|
and would finally return the following data
{
"@odata.context" : "$metadata#SalesOrder(Salesorder,Customer,_Item(Salesorderitem,Product,Grossamountintransaccurrency,Transactioncurrency,Salesorder))/$entity",
"Salesorder" : "500000000",
"Customer" : "100000000",
"_Item" : [
{
"Salesorder" : "500000000",
"Salesorderitem" : "10",
"Product" : "HT-1000",
"Transactioncurrency" : "EUR",
"Grossamountintransaccurrency" : 1137.64
},
{
"Salesorder" : "500000000",
"Salesorderitem" : "20",
"Product" : "HT-1001",
"Transactioncurrency" : "EUR",
"Grossamountintransaccurrency" : 2972.62
},
...
]
}
Please note:
With OData V4 now query options are supported on all levels of an $expand statement.
ToDo and Done-Flags
The SAP Gateway V4 framework has introduced so called ToDo-Flags which provide a hint for the application developer what his implemenations has to do. Depending ont the query options that have been used in the request you will get simple list with boolean values for the following flags:
deltatoken, select, filter, skip, orderby, skiptoken, search, top, …
Done-Flags confirm that the response fits to the request. They allow the application developer to inform the framework to handle feature generically e.g., $top, $skip, and $select. Using such flags also allows an implementation tobe compatible in the future. Instead of a wrong result an exception will be raised if a done flag is not set.
The list of todo and done flags will vary depending on the method which is called. (READ; READ_LIST, CREATE, …)
For a simple GET request with a $filter query option:
…/SalesOrder?$filter=Customer eq ‚SAP‘
a service implementation would have to look like as follows.
At the beginning of our service implementation we have to check whether we have to handle the filter option. For this we call the method io_request->get_todos. Then we have to check whether the flag ls_todo_list-process-filter is set. If yes, the filter string is requested via the method io_request->get_filter_osql_where_clause and the flag that we have handled the filter query option is set in the structure ls_done_list. This information is at the end sent back to the framework via the method io_response->set_is_done that takes the done-list as a parameter.
io_request->get_todos( importing es_todo_list = ls_todo_list ).
...
if ls_todo_list-process-filter = abap_true.
io_request->get_filter_osql_where_clause( importing ev_osql_where_clause = lv_where_clause ).
ls_done_list-filter = abap_true.
endif.
...
" Report list of request options handled by application
io_response->set_is_done( ls_done_list ).
Hello Andre Fischer,
Most wanted blog, thank you so much 🙂
It is really awseome..!!!..
Regards,
Lokeswar.
Hi,
Very informative blog. Thanks
From which ABAP Netweaver version, developers can start using V4?
Thanks,
Naga
Odata v4 was first supported as of SAP Netweaver 750 SP4.
This implementation still lacks some features. There is however planned a downport of SAP Gateway features from 752 to older releases down to 750 SPx, if the corresponding 750 SPx allows such a downport .
Best regards
Hi Andre Fishcher,
I have implemented code based ODATA V4. Everything is working as expected. We are facing some issue with Quantity and currency formatting.
Case 1 Quantity is zero and reference UOM is empty
{
"error" : {
"code" : "/IWBEP/CM_V4_COS/041",
"message" : "Conversion error for property MATERIALNETWEIGHT in entity type BASICDATA",
"@SAP__common.ExceptionCategory" : "Gateway_Framework_Error",
"@SAP__common.Application" : {
"ServiceRepository" : "DEFAULT",
"ServiceId" : "ZGW0026V1_MATERIAL_MASTER",
"ServiceVersion" : "0001"
},
"@SAP__common.TransactionId" : "AC73D61EBF1801E0E005F97393825AAE",
"@SAP__common.Timestamp" : "20201111144648.802259",
"@SAP__common.ErrorResolution" : {
"Analysis" : "Run transaction /IWFND/ERROR_LOG on SAP Gateway hub system and search for entries with the timestamp above for more details",
"Note" : "See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"
},
"innererror" : {
"ErrorDetails" : [
{
"code" : "/IWBEP/CM_V4_COS/035",
"message" : "Measure formatting with unit failed",
"exception" : "/IWBEP/CX_V4_CONVERSION",
"@SAP__common.Severity" : "error"
},
{
"code" : "/IWBEP/CM_V4_COS/036",
"message" : "No configuration for unit of measure",
"exception" : "/IWBEP/CX_V4_CONVERSION",
"@SAP__common.Severity" : "error"
}
]
}
}
}
Case 1 Amount is zero and reference currency code is empty
{
"error" : {
"code" : "/IWBEP/CM_V4_COS/041",
"message" : "Conversion error for property PRICE in entity type OPERATIONS",
"@SAP__common.ExceptionCategory" : "Gateway_Framework_Error",
"@SAP__common.Application" : {
"ServiceRepository" : "DEFAULT",
"ServiceId" : "ZGW0026V1_PRODUCTION_ORD",
"ServiceVersion" : "0001"
},
"@SAP__common.TransactionId" : "AC73D61EBF1801E0E005F97393825AAE",
"@SAP__common.Timestamp" : "20201111145642.537439",
"@SAP__common.ErrorResolution" : {
"Analysis" : "Run transaction /IWFND/ERROR_LOG on SAP Gateway hub system and search for entries with the timestamp above for more details",
"Note" : "See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"
},
"innererror" : {
"ErrorDetails" : [
{
"code" : "/IWBEP/CM_V4_COS/034",
"message" : "Amount formatting with currency failed",
"exception" : "/IWBEP/CX_V4_CONVERSION",
"@SAP__common.Severity" : "error"
},
{
"code" : "/IWBEP/CM_V4_COS/043",
"message" : "Currency does not exist",
"exception" : "/IWBEP/CX_V4_CONVERSION",
"@SAP__common.Severity" : "error"
}
]
}
}
I can disable formatting. But we have multiple currency and need formatting based on currency.
Regards
Karthikeyan Kamalesan
Hi Andre,
The blog is very nice and helpful.
But i am unable to open the source code that you have given. As because i am new to Odata i just want to see the coding style.
Thanks
Ranjan
Hi Andre Fischer, how are you?
I've seen that SEGW transaction is in maintenance mode.
What is the correct tool to use if I'm on an ABAP ECC environment and we still use standard tools as SE38, SE80 etc?
Is Eclipse a mandatory way to go? If so is there any documentation showing how to do this from absolutely scratch?
Thank you!
Douglas
Hi Douglas,
thanks for bringing up this question.
SEGW is still the tool of choice when developing OData V2 services as they are currently used in ECC systems as well as in SAP S/4 HANA systems.
Only when it comes to the development of OData V4 services the recommendation is to use pure ABAP code based development. Here my recommendation would be ABAP in Eclipse but it is not mandatory. SE24 would work.
Links that show how develop OData V2 services from scratch you can find in my blog about OData development options.
Best Regards,
Andre
Hi Andre, thank you very much for the reply and clarifications around this!
Do you know of any guide to building OData V4 services from scratch using SE24 ?
Thank you and best regards,
Douglas
Hi Douglas,
sure. You can use my three blogs that I have mentioned above that explain code based development of OData V4 services.
When using SE24 be sure to use the source code based view.
Best Regards,
Andre
Hi Andre,
that is clear. However, with SEGW you can generate quite a few services; eg via a simple redefinition for ODP. Are you going to add some features, ie one interface for an ODATA-Service via ODP, in order to implement that in an efficient and stable way? The ODP framework consists of several interfaces which are not released. So redoing that yourself is not that straightforward and involves quite some maintenance.
A lot of 3rd party applications which might consume OData do not really like v2. And, especially important for data export like ODP, v2 has same nasty deficiencies regarding date/time. You will lose some information. That got solved with v4.
thanks, C
Your blogs have been very useful. There is one thing to be aware of though, with respect to setting the 'done' flags. I would recommend setting those flags when the filter (for example) criteria have been applied successfully, not immediately after you have retrieved the criteria - just in case...
You are right.
I'll change the sample code.
Thanks and best regards
Andre
Very Informative blog!!!!
Hi Andre,
Very nice blog!
for those also searching, the path of the service is:
https://sapes5.sapdevcenter.com/sap/opu/odata4/sap/ze2e001/default/sap/ze2e001_salesorder/0001/$metadata
Otherwise: any news of the availability of the downport for 7.50?
Kind regards,
Jacques
Hi Jacques,
please check SAP Note 2512479 "SAP Gateway Foundation Support Package Stack Definition"
https://launchpad.support.sap.com/#/notes/2512479
The latest downport of the current V4 capabilities is part of SAP_GWFND 750 SP12.
I am working on feature list but this is not yet available.
Regards,
Andre
How do I create a deep insert
I am getting an error:
"code" : "/IWBEP/CM_V4S_RUN/066",
"message" : "Application did not set DONE list for an expand node '_OBJECT'"
How do I set a done list for an expand node ?
In the context of SAP Fiori Launchpad Notifications I was able to solve a similar issue by applying the Note 2860780 - Expand functionality of entity in Notification channel.
Can we leverage cds view as Reference data source for generating version 4 Odata.
Hi,
In note 2485370 it is stated that it is still recommended to use Odata V2 if it is not mandatory for the business scneario to use Odata V4. The note version is from 2017/09/11.
is this still the current recommendation, are there any restriction in Odata V4 we should be aware of if we develop our REST Services with Odata V4?
Regards,
Silvia
Hi Silvia,
I have just updated the note though the changes are not visible due to a 4 eyes principle that is in place when SAP Notes are changed.
Unfortunately the main statement still holds true that if you must use OData V4 now you have to perform a code based implementation. And here it is recommended to use the approach I described above because this way your data model will already be based on CDS views and it will be more easy to reuse this in the ABAP RESTful programming model which is available in SAP S/4HANA as of SAP S/4HANA 1909.
Restrictions with regards to the support of OData V4 are not on the service implementation side if you go for a code based implementation. The only problem you will have is that such a code based implementation is implementing the OData V4 API's of the SAP Gateway Framework, rather than using the OData protocol agnostic implementation of the ABAP RESTful Programming Model.
So you would have to adapt your coding manually when moving to the ABAP RESTful programming model at a later point in time.
The restrictions are more on the UI side since there is currently no support for OData V4 for SAP Fiori elements applications. Though this is planned.
So if you are planning to build SAP Fiori applications the recommendation is still to use OData V2.
Best Regards,
Andre Fischer
Hi Andre,
Thanks for your helpful blog.
For one of our project requirements, we had to use V4 O-Data implementation. So I referred the blog to set-up and implement my service successfully in my development environment. Our service is defined in Back-end system(SAP_ABA-Version 7.50 SP-Level 11) with the service Group registration done at Gateway system(SAP_GWFND-Version 7.52 SP-Level 0 ).
After transporting my Back-end / GW transports to IT system, my service in IT is not working (Table /IWFND/C_V4_RSAG is not having respective entries ).
Just wanted to confirm that the service registration has to be manually configured in each of IT/Production environment.
Also we need to have client-open for the configurations set-up?
Thanks & Regards,
Samson
Hi Andre,
currently the ABAP RESTful programming model which is available in SAP S/4HANA as of SAP S/4HANA 1909 has been release and is there an update for this blog?
Is the abap end-2-end scenario for odata v4 available with 1909?
Regards Holger
No, unfortunately not.
Sorry, but one additional question:
I just saw the asug abap roadmap webinar of Karl Kessler and v4 release is planned for onprem 2020 release and september is not far away 😉
Is this something to rely on? Because also SAP CAP is heavily focused on v4 and the used SAP Fiori Elements for v4.
Also Peter Spielvogel is anouncing Fiori Elements Floorplans for OData v4 comming soon (Q3/2020)
https://blogs.sap.com/2020/03/17/fiori-elements-floorplans-for-odata-v4-coming-soon/
But if this is not part of onPrem yearly release, this will be to late for most of our onprem customers.
Regards Holger
Hi Andre,
we do need the oData-Service functionality for stateful on SAP Netweaver 7.5.
Currently it is available on 1809 upwards: https://help.sap.com/viewer/68bf513362174d54b58cddec28794093/201809.002/en-US/5fb6396b8b304bce9b9dba5360128a77.html
So can you tell me if this feature is on the downport-list and if so, with which
7.5 Support Package it will be availabe then?
Thank you!
// Klaus
Hi Klaus,
no down ports have been performed from SAP S/4HANA foundation 1809 to SAP NetWeaver 750.
See also SAP Note 512479 – SAP Gateway Foundation Support Package Stack Definition
https://launchpad.support.sap.com/#/notes/2512479
1809 features are (currently) only down ported to SAP NetWeaver 752 and later.
Do you need "real" stateful session handling or would something like the softstate be sufficient?
https://blogs.sap.com/2014/10/14/how-to-use-soft-state-support-for-odata-services/
Best regards,
Andre
Hi Andre,
can you tell me where i can enable Oauth for SAP ODataV4 Services?
Best regards,
Domenik
Thanks for the blog.
Could u plz tell me what is meant by "end-2-end support of OData V4 by the new programming model".
If we are implementation code based odata service 4 ,anything wrong will happen in the next upgrade?
Regards
Suresh
Hi Pradeep, what makes you think there is going to be anything wrong with "the next upgrade"?
As Andre Fischer has already stated in the OData service development options blog:
"By choosing a service implementation based on the new ABAP RESTful Application Programming Model in SAP S/4HANA 1909, you are well prepared for the end-2-end support of the OData V4 protocol in SAP S/4HANA 2020 FPS1"
The essensial here is that there is no relation between the RAP and OData V4 code based approach.
Kind Regards,
Boyan
End to end support means that in addition to the possibility to develop an OData V4 service there should be the option to consume the same using the SAP Fiori Elements list reporting template which now is also V4 based.
An existing OData V4 code based implementation will also run after the upgrade (at least it is supposed to do that, because otherwise it would be a bug in the framework).
As said the downside of the codebased implementation approach is that the implementation is specific to the protocol being used whereas using the ABAP RESTful programming model has the advantage that different binding types are available.
Ok. Thank You.
Will aggregate functionality support? I am in NW 7.50 with SP 17.
I just passed 'aggregate' function in the url but I am getting the error
/IWBEP/IF_V4_DP_ADVANCED~READ_ENTITY_LIST has to implement.
Can you provide some additional information with sample code as you given above.
Regards
Suresh
Hi Andre, thanks for your helpful blog.
For one of our project requirements, I need to Create a deep entity with multilevel, for example Sales Order -> Items -> Serials, I have followed your logic, created the entities and navigation as well, then I implemented the method /iwbep/if_v4_dp_advanced~create_entity but I am facing some issues when I provide Serial entity on payload file, if I remove the Serial entity is works.
I think that it is missing some associations between Item -> Serials, using your example as reference, can I use the same logic that you created to SalesOrder to SalesOrderItem? Or I need to do anything else?
If I execute the POST with the _Serial I get error message: "message" : "In the context of Data Services an unknown internal server error occurred".
In /IWFND/ERROR_LOG : HTTP 500 Dereferencing of the NULL reference
{
"@odata.context" : "$metadata#HeaderSet(_Item())/$entity",
"@odata.metadataEtag" : "W/\"20210727230600\"",
"SalesOrg" : "1001",
"ShipTo" : "55552581",
"SoldTo" : "55552581",
"OrderType" : "ZKE",
"OrderReason" : "Z09",
"DlvBlock" : "60",
"PoMethod" : "ZMMR",
"Ref1" : "11111",
"Name" : "TEST KRAUTE (1615)",
"MeansOfTransport" : "0001",
"PurchOrdNumber" : "04262021",
"PurchNoS" : "TEST NUMBER",
"PoDatS" : "2000-12-12",
"InvcHeaderText" : "Text Header",
"EmailAddress" : "test@test.com",
"_Item" : [
{
"ItemNo" : "1",
"Material" : "087790430",
"Batch" : "",
"Plant" : "",
"Lgort" : "",
"OrderQty" : 1.000,
"ItemCateg" : "",
"TargetQu" : "KG",
"_Serial" : [
{
"ItemNo" : "1",
"Serial" : "123"
}
]
}
]
}
In debug mode I saw that the entity_info-targer is INITIAL
In my data model the child entity contains two key fields and a grand child entity would have to use at least three key fields.
So some grandchild entity would have the following keys
My scenario is a Sales Order creation, so, the key fields in Header are the mandatories fields to create a sales order, there is no "effective association" with Item entity, but I created the navigation between them directly in the code, it works fine, when I read the deep entity the values comes. In Item entity has created ITEM_NO as a key field, also I created the navigation with Serial entity, but in this case there is a association because SERIAL entity has ITEM_NO and SERIALNUMBER as key field.
How I should model this scenario?
URI for POST: /sap/opu/odata4/sap/ztest/default/sap/zdeep/0001/HeaderSet
The URI post is correct?
current mapping:
Header -> Sales_org AND Ship_to (KEY) (1º LEVEL)
Item -> Item_no (KEY) (child)
Serial -> Item_no AND Serial_number (KEY) (grand child)
Hi,
I need to provide options for customer enhancement for ex: Add a new field / Add a new entity by Customer. Do you have suggestion/ examples how that could be done?
Hello,
We have to add Annotations like Common.Label and Aggregation.ApplySupported to the Entities for our OData V4 Service
I was wondering if this should be done directly in CDS Views, or if we should define then using code based implementation. Do you maybe have a blog/ example of a CDS View containing Annotations?
Hi Ingo,
when you are using a code based implementation you have to add the code in the model provider class via code based implementation as well.
Please note that as of SAP S/4HANA 2020 FSP1 you are able to build OData V4 Services using the ABAP Restful Application Programming Model.
Kind regards,
Andre
Hi Andre!
Which standard classes can be used to add from source code Annotations to $metadata result?
Do you have maybe an example?
Thanks!
Hi Andre.
Thank you so much for you answer, we managed to create the annotations via code based implementation.
We are trying to use the Location Header feature from OData to provide the URL for the next function that has to be called. After some debugging we managed to find the /IWCOR/IF_OD_RESPONSE_PROVIDER -->SET_HEADER method but couldn't find a way to access it in the Data Provider method of the Action. Is there a way to add custom Response Headers using code based (for example to also change the status code of the call)?
With V2 I believe it was possible using the method /iwbep/if_mgw_conv_srv_runtime~set_header.
Thank you and best regards,
Ingo
Hello Andre Fischer,
thanks for this great Blog!
Actually I'm working on a Fiori Elements Worklist App based on OData V4 using RAP. I want to use Cross-Service Reference but can't find this function in ADT RAP tooling. Is this function supported by S/4HANA 2021 and RAP?
Best regards
Christian
Hi Christian,
Cross Service Referencing is not supported by RAP.
But you can call other RAP based services via EML or OData services using OData Client Proxy Calls.
What's the scenario you want to implement?
Kind regards,
Andre