protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

A field with type 'bytes' is decoded into an empty array instead of an empty buffer

Open assaf-xm opened this issue 6 years ago • 3 comments

protobuf.js version: 6.8.6

Any empty 'bytes' field on a message, will be decoded into an empty javascript array. The expected behavior is that it will be decoded into an empty buffer, in order to keep consistency with the case the field has data.

  • in protobuf js v5 it was decoded into an empty buffer.

assaf-xm avatar Apr 11 '18 20:04 assaf-xm

function toObject(message, options) {
...
    if (message.bytesField != null && message.hasOwnProperty("bytesField"))
        options.bytes === String ? $util.base64.encode(message.bytesField, 0, message.bytesField.length) : options.bytes === Array ? Array.prototype.slice.call(message.bytesField) : message.bytesField

metled avatar Apr 15 '18 01:04 metled

Both 'decode' and 'toObject' return the field as an empty array. So the root cause might be on the 'decode' implementation. I'm using 'toObjcet' without setting the 'bytes' option (I don't want it base64 or an array, I want it to be kept as Buffer).

assaf-xm avatar Apr 15 '18 06:04 assaf-xm

close it please as it's already merged 👍

Amitcoh1 avatar Aug 22 '22 18:08 Amitcoh1