googleapis / googleapis/google-cloud-node
proto3-json-serializer Fails to Serialize Repeated Fields in Array
- Dominant language
- TypeScript
- Stars
- 3.2k
- Forks
- 712
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 99
Description
### Please make sure you have searched for information in the following guides.
- [X] Search the issues already opened: https://github.com/GoogleCloudPlatform/google-cloud-node/issues
- [X] Search StackOverflow: http://stackoverflow.com/questions/tagged/google-cloud-platform+node.js
- [X] Check our Troubleshooting guide: https://github.com/googleapis/google-cloud-node/blob/main/docs/troubleshooting.md
- [X] Check our FAQ: https://github.com/googleapis/google-cloud-node/blob/main/docs/faq.md
- [X] Check our libraries HOW-TO: https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md
- [X] Check out our authentication guide: https://github.com/googleapis/google-auth-library-nodejs
- [X] Check out handwritten samples for many of our APIs: https://github.com/GoogleCloudPlatform/nodejs-docs-samples
### Link to the code that reproduces this issue. A link to a **public** Github Repository or gist with a minimal reproduction.
N/A
### A step-by-step description of how to reproduce the issue, based on the linked reproduction.
**proto3-json-serializer version:** 2.0.2
**Node.js version:** v20.13.1
**protobufjs version:** "^7.4.0"
**Operating system:** Windows
**Description:** The proto3-json-serializer is not serializing repeated fields in arrays properly. When attempting to serialize a message with repeated fields, the resulting JSON output is missing the array data.
**1. Use the following JsonPayload:**
```
{
"hierarchyData": {
"articleHierarchies": [
{
"hierarchyId": "Z1",
"hierarchyNode": "1093895"
},
{
"hierarchyId": "Z1",
"hierarchyNode": "109389502"
}
]
}
}
```
**2. Serialize the message using the provided logic:**
```
import fs from 'fs';
import path from 'path';
import protobuf from 'protobufjs';
import * as serializer from 'proto3-json-serializer';
async function loadAllProtobufSchemas(): Promise {
const protoDirPath = 'data/services/protos';
const protobufRoot = new protobuf.Root();
const protoFiles = fs.readdirSync(protoDirPath).filter((file) => file.endsWith('.proto'));
for (const protoFile of protoFiles) {
await protobufRoot.load(path.join(protoDirPath, protoFile));
}
return protobufRoot;
}
export async function createProtobufMessage(messageTypeName: string, messagePayload: any): Promise {
const protobufRoot = await loadAllProtobufSchemas();
const protobufMessageType = protobufRoot.lookupType(messageTypeName);
const protobufMessage = serializer.fromProto3JSON(protobufMessageType, messagePayload)!;
console.log('>>1', JSON.stringify(protobufMessage, null, 2));
const encodedMessage = protobufMessageType.encode(protobufMessage).finish();
return encodedMessage;
}
```
**3. Observe the logs after serialization:**
```
{
"hierarchyData": {}
}
```
**4. Expecting the logs after serialization:**
```
hierarchyData: {
articleHierarchies: [
{
"hierarchyId": "Z1",
"hierarchyNode": "1093895"
},
{
"hierarchyId": "Z1",
"hierarchyNode": "109389502"
},
]
```
### A clear and concise description of what the bug is, and what you expected to happen.
**Bug Description:** The proto3-json-serializer is not properly serializing repeated fields in arrays. When attempting to serialize a message with repeated fields, the resulting JSON output is missing the array data.
**Expected Behavior:** The serialized JSON should include the articleHierarchies array with its elements, as specified in the JsonPayload.
**Additional Information:** Please investigate why the repeated fields in arrays are not being serialized correctly and provide a fix or workaround.
### A clear and concise description WHY you expect this behavior, i.e., was it a recent change, there is documentation that points to this behavior, etc. **
The proto3-json-serializer library is designed to serialize and deserialize protobuf.js objects according to the proto3 JSON specification. According to the documentation (https://cloud.google.com/nodejs/docs/reference/proto3-json-serializer/latest/overview), the library should handle repeated fields in arrays correctly.
_Please find the example proto definitions in the comments section below._
Contributor guide
Assessment
This issue has not been assessed yet.