Search code examples
javajsonodatasap-cloud-sdk

com.sap.cloud.sdk.datamodel.odata render empty fields


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 https://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonInclude.Include.html for details.


Solution

  • Disclaimer: I'm part of the SAP Cloud SDK for Java development team.

    Unfortunately, the SAP Cloud SDK for Java currently doesn't support excluding "empty" fields (i.e. fields with value null) for create / replace (HTTP POST / PUT) requests automatically.

    We are considering adding additional APIs in the future to support such use cases.

    Please also refer to this GitHub Issue for more details.