astarte-platform / astarte

Core Astarte Repository
https://docs.astarte-platform.org/
Apache License 2.0
240 stars 46 forks source link

Empty arrays properties sent as `null` upon reconnection. #918

Open sorru94 opened 6 months ago

sorru94 commented 6 months ago

The issue arises when a server-owned property has been set by a device to an empty array. Upon disconnection and reconnection with a clean session, Astarte sends the server property values to the device. In the case of the empty array, a BSON file containing a null value is received. For example, for a string array mapping, the following BSON is received:

08 00 00 00 0a 76 00 00

Which in human readable form is:

{ "v": null }

This has been tested using Astarte v1.1.0.

Annopaolo commented 6 months ago

This is due to Scylla/Cassandra:

Note that Scylla does not distinguish an empty list from a missing value, thus assigning an empty list ([]) to a list is the same as deleting it.

(source: https://opensource.docs.scylladb.com/stable/cql/types.html)

The same behaviour also appears on datastreams:

❯curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" \
        -H "Authorization: Bearer $TOKEN" \
        "http://api.astarte.localhost/appengine/v1/test/devices/$DEVICE/interfaces/$INTERFACE/array_endpoint" \
        --data '{"data" :[]}'
{"data":[]}                                                                                                                                                  
❯curl -X GET -H "Accept: application/json" -H "Content-Type: application/json" \
        -H "Authorization: Bearer $TOKEN" \
        "http://api.astarte.localhost/appengine/v1/test/devices/$DEVICE/interfaces/$INTERFACE"
{"data":{"array_endpoint":{"reception_timestamp":"2024-04-08T16:06:49.116Z","value":null}}}