-
Notifications
You must be signed in to change notification settings - Fork 11
Description
Please make sure you have searched for information in the following guides.
- Search the issues already opened: https://github.com/GoogleCloudPlatform/google-cloud-node/issues
- Search StackOverflow: http://stackoverflow.com/questions/tagged/google-cloud-platform+node.js
- Check our Troubleshooting guide: https://github.com/googleapis/google-cloud-node/blob/main/docs/troubleshooting.md
- Check our FAQ: https://github.com/googleapis/google-cloud-node/blob/main/docs/faq.md
- Check our libraries HOW-TO: https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md
- Check out our authentication guide: https://github.com/googleapis/google-auth-library-nodejs
- 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<protobuf.Root> {
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<Uint8Array> {
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.