0

The update function of InventoryApi returns a ManagedObjectRepresentation object of the result.

In my code, I successfully use update() and later, try to update the returned ManagedObjectRepresentation (e.g. set some fragments), calling InventoryApi.update() again.

Calling this, I get an error code 422 with message:

Following fields are not updateable: lastUpdated

This is because the previously returned object has that field set, the REST API does not expect that field to have a value.

Now comes my question: looking inside the com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation class, I see the lastUpdated field (line 36) has a @Null annotation for certain "operations".

ManagedObjectRepresentation Java class

My loose interpretation of this is that somewhere in the SDK, if this object is used as part of a CREATE or UPDATE operation, that field should be null. If this were the case, my above update() call with a ManagedObjectRepresentation that has this field set, should not cause problems. This is not the case.

What is the actual use of this @Null annotation? Am I using the SDK wrongly? I know that one way to make the request work is simply to set the lastUpdated field to null, but I thought that was the purpose of the @Null-annotation..

Using the Java client library version 8.13.

jakob
  • 35
  • 1
  • 1
  • 5

2 Answers2

0

These @Null annotations are evaluated for validation of incoming objects on server side. As you wrote parameters must be null for the listed operations. There is also a similar @NotNull annotation used on other APIs.

InventoryApi or other Java API classes do not evaluate these annotations. Parameters are not set to null automatically.

In general I would recommend to update objects only with the properties that you want to update and exclude unchanged properties. This avoids conflicts with other parallel updates on the same object.

l2p
  • 420
  • 3
  • 9
0

NO,You are using SDK in right way. As you Mentioned set the lastUpdated field to null is the only way to update the managedObjectRepresentation. This is because the lastUpdated time is only set by the cumulocity not by the user(developer) (I think User May give the date time too far that's why they would not allow to set updatedtime).

Coming to Your question :

The Way to Update The managed Object Representation Is: Conside mo is your managedObject representation

        mo.setName("String Name");//you can set whatever you want
        mo.setLastUpdated(null);// without lastupdated you cannot update mo
        platform.getInventoryApi().update(mo);

And the use @NULL annotation is:

In this Working case You set the LastUpdated to null this will throw null pointer exception to catch the exception(overcome from the exception) @null annotation is used.

For more information about @null see Here

Dhanasekaran Don
  • 294
  • 1
  • 14