Skip to content

toProto3JSON doesn't correctly handle map of bytes #108

@mjameswh

Description

@mjameswh

We have some protobuf messages that contain fields of type map<string, bytes>. Both protobufjs and proto3-json-serializer's fromProto3JSON appears to work properly with those fields.

However, passing such messages through toProto3JSON results in incorrect behaviors while handling the bytes value.

  • In proto3-json-serializer v2.0.0, there is no error, but the Buffer object is passed unmodified to the output object;
  • In proto3-json-serializer v2.0.2, I get the following error message:
Error: toProto3JSON: don't know how to convert value my_buffer_content
 at convertSingleValue (/.../node_modules/proto3-json-serializer/typescript/src/toproto3json.ts:50:11)
 at toProto3JSON (/.../node_modules/proto3-json-serializer/typescript/src/toproto3json.ts:149:13)
 at /.../node_modules/proto3-json-serializer/typescript/src/toproto3json.ts:137:22
 at Array.map (<anonymous>)
 at toProto3JSON (/.../node_modules/proto3-json-serializer/typescript/src/toproto3json.ts:134:27)
 ... truncated ...

The bytes values are Buffer objects. I would expect toProto3JSON to convert them to base64-encoded strings, just like it does for a simple bytes field.

I haven't tested, but from looking at the code, I believe that repeated bytes likely suffer from the same incorrect behavior.

Environment details

  • Node.js version: 20.17.0
  • npm version: 10.8.2
  • proto3-json-serializer version: 2.0.0 - 2.0.2

Steps to reproduce

  1. Start with the official proto3-json-serializer repo's main branch;

  2. In test-fixture/proto/test.proto, modify the message MessageWithMap to:

      message MessageWithMap {
          map<string, MapValue> map_field = 3;
          map<string, string> string_map_field = 4;
          map<string, int64> long_map_field = 5;
    +     map<string, bytes> bytes_map_field = 6;
      }
  3. In typescript/test/unit/map.ts, add this to both message and json:

          'minus one': '-1',
          zero: '0',
        },
    +   bytesMapField: {
    +     key1: 'dmFsdWUgMQo=',
    +     key2: 'dmFsdWUgMgo=',
    +   },
      };
    
  4. Run tests: npm test

  5. Observe test failure

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions