locka99 / opcua

A client and server implementation of the OPC UA specification written in Rust
Mozilla Public License 2.0
501 stars 131 forks source link

Support Variant array_length == 0 #145

Closed milgner closed 1 year ago

milgner commented 2 years ago

Although I personally would prefer using Variant::Empty in case of an empty array, I yesterday encountered this error when our client tried to decode the response from a server that sent a zero-length array.

Now this code does look very explicit about its intent and I was wondering whether there's anything in the OPC UA spec that forbids arrays of length 0?

In https://open62541.org/doc/0.3/types.html#variant they seem to have many different variations on dealing with array length 0.

locka99 commented 2 years ago

Mostly it's because I can't tell you a type of an array if there is nothing in it to infer a type. I'm open to fixes that comply with the spec on this.

schroeder- commented 2 years ago

Empty arrays are allowed. For decoding we can handle length 0 == null array (length=-1).

milgner commented 2 years ago

Thank you for elaborating! I'll have a closer look at the source then and think about potential solutions.

schroeder- commented 2 years ago

Found this in the specs for empty array, null array and Variant null: https://reference.opcfoundation.org/v105/Core/docs/Part6/5.1.9/ So null array == empty array is legal.

milgner commented 2 years ago

Nice! That sounds like it would be also be in accordance with the specification if decoding would return Variant::Empty in case the array length is 0? :thinking:

schroeder- commented 2 years ago

Its up to the implementation, if we want empty, null array and Variant::Empty to be decoded separate. But at least a comparison of Variant::Empty and empty/null array should return true.