fuwaneko / node-protobuf

Google Protocol Buffers wrapper for Node.js [UNMAINTAINED]
180 stars 42 forks source link

Parsing an empty repeated field should create an empty array #41

Closed elaberge closed 9 years ago

elaberge commented 9 years ago

A repeated field with no entry should produce an empty array. This is a regression introduced in 1.2.0, which was working correctly in 1.1.4.

Here is a test case against 1.2.1, which pass with 1.1.4.

diff --git a/test/test.js b/test/test.js
index 4c5a0ac..81c2dcf 100644
--- a/test/test.js
+++ b/test/test.js
@@ -8,6 +8,7 @@ describe("Basic", function() {
    var obj = {
        "name": "test",
        "n64": 123,
+       "repeated": [3,2,1]
    }

    describe("Serialize", function() {
@@ -82,6 +83,7 @@ describe("Basic", function() {
            var objWithNull = {
                "name": "test",
                "n64": 123,
+               "repeated": [3,2,1],
                "value": null
            }

@@ -96,6 +98,7 @@ describe("Basic", function() {
            var objWithNull = {
                "name": "test",
                "n64": 123,
+               "repeated": [3,2,1],
                "value": undefined
            }

@@ -105,6 +108,19 @@ describe("Basic", function() {
            delete objWithNull.value;
            assert.deepEqual(objWithNull, parsed)
        })
+
+       it("Should keep empty arrays", function() {
+           var objWithEmptyArray = {
+               "name": "test",
+               "n64": 123,
+               "repeated": []
+           }
+
+           var buffer = pb.serialize(objWithEmptyArray, "Test")
+           var parsed = pb.parse(buffer, "Test")
+
+           assert.deepEqual(objWithEmptyArray, parsed)
+       })
    })

    describe("Info", function() {
diff --git a/test/test.proto b/test/test.proto
index 128e171..f718c8e 100644
--- a/test/test.proto
+++ b/test/test.proto
@@ -8,6 +8,7 @@ message Test {
        SPECIFIC = 2;
    }
    optional Type type = 5 [default = DEFAULT];
+   repeated int32 repeated = 6;
 }

 message Data {