Skip to content

Commit

Permalink
Fixed: Empty inner messages are always present on the wire + test cas…
Browse files Browse the repository at this point in the history
…e + removed now unused Writer#ldelim parameter, see #585
  • Loading branch information
dcodeIO committed Dec 27, 2016
1 parent 61fd385 commit 95ed6e9
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 75 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# [6.3.1](https://github.com/dcodeIO/protobuf.js/releases/tag/6.3.1)

# [6.3.0](https://github.com/dcodeIO/protobuf.js/releases/tag/6.3.0)

## Breaking
Expand Down
40 changes: 23 additions & 17 deletions dist/noparse/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/noparse/protobuf.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/noparse/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/noparse/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/noparse/protobuf.min.js.map

Large diffs are not rendered by default.

40 changes: 23 additions & 17 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protobufjs",
"version": "6.3.0",
"version": "6.3.1",
"description": "Protocol Buffers for JavaScript (& TypeScript).",
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
"license": "BSD-3-Clause",
Expand Down
14 changes: 6 additions & 8 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ var Enum = require("./enum"),

var safeProp = util.safeProp;

function genEncodeType(gen, field, fieldIndex, ref, alwaysRequired) {
if (field.resolvedType.group)
return gen("types[%d].encode(%s,w.uint32(%d)).uint32(%d)", fieldIndex, ref, (field.id << 3 | 3) >>> 0, (field.id << 3 | 4) >>> 0);
return alwaysRequired || field.required
? gen("types[%d].encode(%s,w.uint32(%d).fork()).ldelim()", fieldIndex, ref, (field.id << 3 | 2) >>> 0)
: gen("types[%d].encode(%s,w.fork()).len&&w.ldelim(%d)||w.reset()", fieldIndex, ref, field.id);
function genEncodeType(gen, field, fieldIndex, ref) {
return field.resolvedType.group
? gen("types[%d].encode(%s,w.uint32(%d)).uint32(%d)", fieldIndex, ref, (field.id << 3 | 3) >>> 0, (field.id << 3 | 4) >>> 0)
: gen("types[%d].encode(%s,w.uint32(%d).fork()).ldelim()", fieldIndex, ref, (field.id << 3 | 2) >>> 0);
}

/**
Expand Down Expand Up @@ -69,7 +67,7 @@ function encoder(mtype) {
("if(%s)", ref)
("for(var i=0;i<%s.length;++i)", ref);
if (wireType === undefined)
genEncodeType(gen, field, i, ref + "[i]", true);
genEncodeType(gen, field, i, ref + "[i]");
else gen
("w.uint32(%d).%s(%s[i])", (field.id << 3 | wireType) >>> 0, type, ref);

Expand All @@ -88,7 +86,7 @@ function encoder(mtype) {
}

if (wireType === undefined)
genEncodeType(gen, field, i, ref, true);
genEncodeType(gen, field, i, ref);
else gen
("w.uint32(%d).%s(%s)", (field.id << 3 | wireType) >>> 0, type, ref);

Expand Down
13 changes: 12 additions & 1 deletion src/oneof.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ OneOfPrototype.toJSON = function toJSON() {
* @ignore
*/
function addFieldsToParent(oneof) {
if (oneof.parent)
if (oneof.parent) {
oneof._fieldsArray.forEach(function(field) {
if (!field.parent)
oneof.parent.add(field);
});
}
}

/**
Expand Down Expand Up @@ -160,6 +161,16 @@ OneOfPrototype.remove = function remove(field) {
*/
OneOfPrototype.onAdd = function onAdd(parent) {
ReflectionObject.prototype.onAdd.call(this, parent);
var self = this;
// Collect present fields
this.oneof.forEach(function(fieldName) {
var field = parent.get(fieldName);
if (field && !field.partOf) {
field.partOf = self;
self._fieldsArray.push(field);
}
});
// Add not yet present fields
addFieldsToParent(this);
};

Expand Down
9 changes: 3 additions & 6 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,17 +513,14 @@ WriterPrototype.reset = function reset() {

/**
* Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
* @param {number} [id] Id with wire type 2 to prepend as a tag where applicable
* @returns {Writer} `this`
*/
WriterPrototype.ldelim = function ldelim(id) {
WriterPrototype.ldelim = function ldelim() {
var head = this.head,
tail = this.tail,
len = this.len;
this.reset();
if (typeof id === "number")
this.uint32((id << 3 | 2) >>> 0);
this.uint32(len);
this.reset()
.uint32(len);
this.tail.next = head.next; // skip noop
this.tail = tail;
this.len += len;
Expand Down
36 changes: 36 additions & 0 deletions tests/empty-inner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var tape = require("tape");

var protobuf = require("..");

tape.test("empty inner message fields", function(test) {
var root = protobuf.Root.fromJSON({
nested: {
Inner: {
fields: {
}
},
Outer: {
oneofs: {
child: {
oneof: ["inner"]
}
},
fields: {
inner: {
id: 1,
type: "Inner"
}
}
}
}
});
var Outer = root.lookup("Outer");
var msg = Outer.from({
inner: {}
});
var buf = Outer.encode(msg).finish();
test.equal(buf.length, 2, "should always be present on the wire");
test.equal(buf[0], 1 << 3 | 2, "should write id 1, wireType 2");
test.equal(buf[1], 0, "should write a length of 0");
test.end();
});

0 comments on commit 95ed6e9

Please sign in to comment.