Search code examples
javaodataolingo

Using Olingo v2 Java as client for PATCH to OData v2 service


I'm trying to use Olingo to provide a client for interacting with an OData service (also written in Olingo). I'm trying to send a PATCH. However, the standard validation routines are kicking in and if I do not include those elements of the entity that are marked as non-nullable using the standard Olingo tools, I get an error.

in https://olingo.apache.org/doc/odata2/tutorials/OlingoV2BasicClientSample.html it says:

With an HTTP MERGE/PATCH it is also possible to send only the to be updated data as POST Body and omitting the unchanged data. But this is (currently) not shown within this sample.

Unfortunately I'm not sure how to do this, there, doesn't seem to be anywhere to flag to the EntityProvider.writeEntry method that it is a PATCH not a POST/PUT

EntityProviderWriteProperties properties = EntityProviderWriteProperties
            .serviceRoot(rootUri).omitJsonWrapper(true).contentOnly(true)
            .build();

        // serialize data into ODataResponse object
        ODataResponse response = EntityProvider.writeEntry(contentType,
                entitySet, data, properties);

At this point in my code I get an error if "data" does not contain an entry for my non-nullable fields. The response also returns null values for all the attributes of the entity that aren't in my "data".

I deal with this by manipulating the response to remove all entries not in my "data" after the "standard" generation, but imagine that there must be a better way, even if I can't see it. Any suggestions on how to deal with this?


Solution

  • You have to create an "ExpandSelectTreeNode" which contains only the name of the selected properties to be serialized. Assuming that your data is a HashMap with the values you can use following code as an example to start from:

    // data = new HashMap<>();
    ExpandSelectTreeNode node = ExpandSelectTreeNode.entitySet(entitySet)
        .selectedProperties(new ArrayList<String>(data.keySet())).build();
    
    EntityProviderWriteProperties properties = EntityProviderWriteProperties
                .serviceRoot(rootUri).omitJsonWrapper(true).contentOnly(true)
                .expandSelectTree(node)
                .build();
    
    // serialize data into ODataResponse object
    ODataResponse response = EntityProvider.writeEntry(contentType,
            entitySet, data, properties);
    

    Best Regards