Beyond ABAPdesigned by@Tejoram97
Beyond ABAP / CORE ABAP / OData V2 vs V4 – Protocol Deep Dive

OData V2 vs V4 – Protocol Deep Dive

CORE ABAP · ABAP Development ·

Open the interactive lesson →
  1. Versions and HTTP headers
  2. JSON payload shape
  3. Data types: dates, times and numbers
  4. Query options and filter functions
  5. Operations, references and updates
  6. Model features new in V4
  7. Moving a UI5 app from the V2 to the V4 model
  8. Sources
  9. Interview questions

This page adds the protocol-level details of OData V2 vs V4 that the other pages do not cover. It builds on: CORE ABAP › 08 · OData (SAP Gateway) (feature table), SAPUI5 › 04 · Data, Binding & Navigation (V2 vs V4 model), SAP Fiori › 03 · SAP Fiori Elements (two stacks) and RAP › 03 (which binding to choose). Every fact below is taken from the OData standard (OASIS / odata.org) or the SAPUI5 documentation; the source is named under each part.

Part 1 – Versions and HTTP headers

Client and service negotiate the protocol version through HTTP headers. V4 renamed them and dropped the minimum-version header: the service simply uses the highest version both sides support.

Purpose

OData V2

OData V4

Version of this request / response

DataServiceVersion (e.g. DataServiceVersion: 1.0)

OData-Version

Highest version the client accepts

MaxDataServiceVersion

OData-MaxVersion

Lowest version the client accepts

MinDataServiceVersion (V3)

removed

Id of a created entity

DataServiceId (V3)

OData-EntityId

Source: OASIS – What's New in OData V4.0 (2.3.4, 2.3.5); V2 header example: odata.org V2 JSON format.

Part 2 – JSON payload shape

The most visible difference when you debug a service: V2 'verbose JSON' wraps everything in d, V4 JSON is flat and uses @odata. control information.

2.1 OData V2 (verbose JSON)

{ "d": {
"results": [
{ "__metadata": { "uri": ".../SalesOrders('4711')", "type": "...SalesOrder" },
"SalesOrder": "4711" }
],
"__count": "42",
"__next": ".../SalesOrders?$skiptoken=10"
} }

2.2 OData V4 (JSON)

{ "@odata.context": "$metadata#SalesOrders",
"@odata.count": 42,
"value": [
{ "@odata.etag": "W/\"…\"", "SalesOrder": "4711" }
],
"@odata.nextLink": "SalesOrders?$skiptoken=10" }

Source: odata.org V2 JSON format (sections 3, 6, 7); OASIS OData JSON Format 4.01 (3.1, 4.2, 4.4, 4.5).

Part 3 – Data types: dates, times and numbers

Date/time handling is the classic V2 → V4 migration pitfall: V4 removed Edm.DateTime and Edm.Time and introduced separate types for a date, a time of day and a duration.

Topic

OData V2

OData V4

Date + time

Edm.DateTime (no time zone), JSON "/Date(1700000000000)/" = milliseconds since 1 Jan 1970

Edm.DateTime removed; use Edm.DateTimeOffset (carries the time zone offset)

Date only

no own type (DateTime at midnight)

Edm.Date

Time of day

Edm.Time

Edm.TimeOfDay (Edm.Time removed)

Duration

–

Edm.Duration

Single-precision float

Edm.Float

removed (use Edm.Single / Edm.Double)

Int64 / Decimal in JSON

JSON strings (Edm.Int64 as string)

JSON numbers by default; request strings with IEEE754Compatible=true

Source: odata.org V2 JSON format (section 4); OASIS – What's New in V4.0 (4.2.1–4.2.4); OASIS JSON Format 4.01 (3.2).

Part 4 – Query options and filter functions

Topic

OData V2

OData V4

Inline total count

$inlinecount=allpages (or none)

$count=true

Count only

/SalesOrders/$count

/SalesOrders/$count – also usable inside $filter and $orderby

Substring test

substringof('Mill', CustomerName) – the long string is the second argument

contains(CustomerName, 'Mill') – the long string is the first argument (case-sensitive)

Collection conditions

not available

lambda operators any / all, e.g. $filter=Items/any(i: i/Quantity gt 100)

Free-text search

no standard option (SAP: search=)

$search

Hierarchy depth

–

$levels (inside $expand)

Projection of expanded data

$select=Items/Material at top level

$select covers only the base set; nest it: $expand=Items($select=Material)

Watch the argument order when you migrate a filter: substringof(a, b) becomes contains(b, a). any is false for an empty collection, all is true for an empty collection. $it (the current instance) can be used everywhere in V4, not only inside lambdas.

Source: odata.org V2 URI conventions (3.1, 4.5, 4.9); OASIS – What's New in V4.0 (2.6, 3.2); OASIS URL Conventions 4.01 (5.1.1).

Part 5 – Operations, references and updates

Topic

OData V2

OData V4

Custom operations

service operations / function imports, called by URI with query-string parameters

actions (side effects, POST) and functions (no side effects, GET); both can be bound to an entity or collection; functions can be used in $filter / $orderby

Unbound entry points

function import

ActionImport (POST) and FunctionImport (GET)

Links between entities

Categories(1)/$links/Products

/$ref (e.g. Categories(1)/Products/$ref); resolve with $entity?$id=…

Partial update

MERGE (or PATCH)

PATCH only – MERGE removed

Upsert

–

PATCH or PUT to a non-existing key can create the entity

Single-part key in URL

SalesOrders('4711')

same; canonical form is the value only, without the property name

In $batch, V4 requests inside a change set need a Content-ID, and by default processing stops at the first error. Response codes are explicit: 204 for a null value, 404 for a missing entity.

Source: odata.org V2 URI conventions (3.2, 3.3, 6); OASIS – What's New in V4.0 (2.6.7, 2.7, 2.9, 3.1, 4.7); OASIS URL Conventions 4.01 (4.3.1).

Part 6 – Model features new in V4

Source: OASIS – What's New in V4.0 (2.2, 2.3.3, 4.2.5–4.2.7, 4.3.4, 4.4.3, 4.8, 4.10); OASIS URL Conventions 4.01 (4.3.2).

Part 7 – Moving a UI5 app from the V2 to the V4 model

SAP's own reasons for the separate V4 model: protocol incompatibility between OData V4 and V2, API clean-up and simplification, and alignment with the V4 naming standards. What changes for the developer:

Topic

V2 model

V4 model

Binding parameters

expand, select

the real query option names: $expand, $select

Reading data

getData, getObject, getProperty on the model

not available – use the binding context API

CRUD

model methods create, read, update, remove

no such methods – implicit through list/context bindings

Operations

callFunction

operation binding (bindContext(…).invoke())

Batch control

model-wide (deferred / change groups)

per binding via group parameters; $direct switches batching off for one binding

Aggregation / trees

AnalyticalBinding, ODataTreeBinding

$$aggregate binding parameter

Data cache

global entity cache in the model

no global cache – data is kept per binding

Metadata

metadata loaded with the model; AnnotationHelper

ODataMetaModel, loaded on demand; AnnotationHelper not supported

Source: SAPUI5 documentation – Transition from OData V2 to OData V4.

Part 8 – Sources

odata.org's V2 page states that OData Version 4.0 is the current recommended version.

Part 9 – Interview questions

Q1. How do you recognise V2 vs V4 from a JSON response?

V2 verbose JSON wraps everything in d; collections are in d.results, entries carry __metadata, the count is __count. V4 has no wrapper: the collection is value, control information is @odata.context, @odata.count, @odata.nextLink, @odata.etag. The response header is DataServiceVersion in V2 and OData-Version in V4.

Q2. What happens to Edm.DateTime when you move to V4?

It is removed. A timestamp becomes Edm.DateTimeOffset (with time zone), a pure date Edm.Date, a time Edm.TimeOfDay (Edm.Time is also removed). V2 sends DateTime as /Date(ms)/; client code that parses that format must change.

Q3. How do you migrate a substringof filter?

substringof('Mill', CustomerName) becomes contains(CustomerName, 'Mill') – the arguments swap. For conditions on a collection, V4 offers any / all, e.g. Items/any(i: i/Quantity gt 100).

Q4. Function import vs action vs function?

V2 has function imports. V4 splits them: an action has side effects and is called with POST, a function has none and is called with GET; both can be bound to an entity or collection, and functions can appear in $filter and $orderby. In RAP, BDEF actions become V4 actions.

Q5. What changes when a UI5 app moves from the V2 to the V4 model?

No model CRUD methods or callFunction – everything goes through bindings, contexts and operation bindings; parameters use the real names $expand / $select; batching is controlled per binding ($direct, group IDs); there is no global cache; aggregation uses $$aggregate; metadata comes from ODataMetaModel.