Skip to content

Commit

Permalink
Fixed: Exclude any fields part of some oneof when populating defaults…
Browse files Browse the repository at this point in the history
… in toObject, see #710
  • Loading branch information
dcodeIO committed Mar 21, 2017
1 parent 320dea5 commit bc76ad7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ In case of doubt you can just use the full library.
Usage
-----

For [performance](#performance) reasons, protobuf.js provides multiple methods per message type with each of them doing just one thing. This avoids redundant assertions where messages are already known to be valid but also requires explicit verification where necessary. Note that `Message` refers to any message below.
For [performance](#performance) reasons, protobuf.js provides multiple methods per message type with each of them doing just one thing. This avoids redundant assertions where messages are already known to be valid but also requires explicit verification where necessary. Note that `Message` refers to any message type below.

* **Message.verify**(message: *Object*): *?string*<br />
explicitly performs verification prior to encoding / converting a plain object (i.e. where data comes from user input). Instead of throwing, it returns the error message as a string, if any.
Expand All @@ -109,7 +109,7 @@ For [performance](#performance) reasons, protobuf.js provides multiple methods p
additionally prepends the length of the message as a varint.

* **Message.decode**(reader: *Reader|Uint8Array*): *Message*<br />
is a message specific decoder expecting a valid buffer. If required fields are missing, it throws a `protobuf.util.ProtocolError` with an `instance` property set to the so far decoded message - otherwise an `Error`. The result is a runtime message.
is a message specific decoder expecting a valid buffer. If required fields are missing, it throws a `util.ProtocolError` with an `instance` property set to the so far decoded message - otherwise an `Error`. The result is a runtime message.

```js
try {
Expand Down
18 changes: 8 additions & 10 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,13 @@ converter.toObject = function toObject(mtype) {

var repeatedFields = [],
mapFields = [],
otherFields = [],
normalFields = [],
i = 0;
for (; i < fields.length; ++i)
if (fields[i].resolve().repeated)
repeatedFields.push(fields[i]);
else if (fields[i].map)
mapFields.push(fields[i]);
else
otherFields.push(fields[i]);
if (!fields[i].partOf)
( fields[i].resolve().repeated ? repeatedFields
: fields[i].map ? mapFields
: normalFields).push(fields[i]);

if (repeatedFields.length) { gen
("if(o.arrays||o.defaults){");
Expand All @@ -229,10 +227,10 @@ converter.toObject = function toObject(mtype) {
("}");
}

if (otherFields.length) { gen
if (normalFields.length) { gen
("if(o.defaults){");
for (i = 0, field; i < otherFields.length; ++i) {
var field = otherFields[i],
for (i = 0, field; i < normalFields.length; ++i) {
var field = normalFields[i],
prop = util.safeProp(field.name);
if (field.resolvedType instanceof Enum) gen
("d%s=o.enums===String?%j:%j", prop, field.resolvedType.valuesById[field.typeDefault], field.typeDefault);
Expand Down

0 comments on commit bc76ad7

Please sign in to comment.