googleapis / googleapis/google-cloud-node
toProto3JSON doesn't correctly handle map of bytes
- Dominant language
- TypeScript
- Stars
- 3.2k
- Forks
- 712
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 99
Description
We have some protobuf messages that contain fields of type `map`. 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 ()
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:
```diff
message MessageWithMap {
map map_field = 3;
map string_map_field = 4;
map long_map_field = 5;
+ map bytes_map_field = 6;
}
```
3. In `typescript/test/unit/map.ts`, add this to both `message` and `json`:
```diff
'minus one': '-1',
zero: '0',
},
+ bytesMapField: {
+ key1: 'dmFsdWUgMQo=',
+ key2: 'dmFsdWUgMgo=',
+ },
};
```
4. Run tests: `npm test`
6. Observe test failure
Contributor guide
Assessment
This issue has not been assessed yet.