SAP / SAP/cloud-sdk

com.sap.cloud.sdk.datamodel.odata omit empty fields on serialisation

Open
#742 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement feature request Java waiting for release
Dominant language
HTML
Stars
52
Forks
57
Avg merge
12h 33m
Merged PRs (30d)
18

Description

Hi all!

In the context of an OData update request we run into an error response from the SAP service called. We are using com.sap.cloud.sdk.datamodel:odata-core in version 3.65.0. The Method in Question is com.sap.cloud.sdk.datamodel.odata.helper.FluentHelperUpdate.executeRequest().

When serializing in REST calls with data from com.sap.cloud.sdk.datamodel.odata, empty fields are also rendered into the output:

{
    "insuranceId" : "ABC123",
    "terminationDate" : null,
    "terminationReason" : "UNKNOWN",
    "terminationWish" : null,
    "terminationReceived" : "/Date(1648027328522)/",
    "force" : false
}

This fails on the SAP side with HttpResult 400 - bad request, because these fields are either not allowed to be empty or not allowed to be included at all.
We are now looking for a way / a setting to prevent this, so that something like

{
    "insuranceId" : "ABC123",
    "terminationReason" : "UNKNOWN",
    "terminationReceived" : "/Date(1648027328522)/",
    "force" : false
}

is rendered out.

As a workaround, we are using com.sap.cloud.sdk.datamodel.odata.helper.FluentHelperUpdate.excludingFields() right now as follows.

final var emptyEntityFields = new ArrayList<EntityField>();
            if(cancellation.getTerminationDate() == null) emptyEntityFields.add(new EntityField("terminationDate"));
            if(cancellation.getTerminationWish() == null) emptyEntityFields.add(new EntityField("terminationWish"));
            this.dmeEpaService.updateTerminationRequest(terminationRequest)
                    .replacingEntity()
                    .excludingFields(emptyEntityFields.toArray(EntityField[]::new))
                    .executeRequest(this.httpDestination);

This workaround is cumbersome and needed on every request affected. As an example, in the jackson environment there is the setting @JsonInclude(JsonInclude.Include.NON_NULL) to accomplish this for every entity class annotated that way. See com.fasterxml.jackson.annotation
Enum JsonInclude.Include.NON_NULL
for details an an eample.

Thanks in advance and best regards

Peter Hahn

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at com.sap.cloud.sdk.datamodel.odata.helper.FluentHelperUpdate.executeRequest() and examine how OData entities are serialized for update requests. Compare the existing excludingFields() workaround with the requested behavior; done means null fields can be omitted without manually listing them while non-null and false values remain serialized.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.