0

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?

wombling - Chris Paine
  • 1,651
  • 1
  • 18
  • 17

2 Answers2

4

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

mibo
  • 56
  • 2
  • That works perfectly, an awful lot better than the post JSON production manipulation that I was doing. Thank you. Wish I'd been able to find this in the Olingo doco. – wombling - Chris Paine Nov 04 '15 at 12:54
0

Is the contenttype from the client application/json-patch+json ?

NicoJuicy
  • 3,435
  • 4
  • 40
  • 66
  • No the content type of the payload a client sends will always be "application/json" in OData V2 for Json documents. – chrisam Nov 05 '15 at 16:00