Permalink
Browse files

Fixed: Exclude any fields part of some oneof when populating defaults…

… in toObject, see #710
  • Loading branch information...
dcodeIO committed Mar 21, 2017
1 parent 320dea5 commit bc76ad732fc0689cb0a2aeeb91b06ec5331d7972
Showing with 10 additions and 12 deletions.
  1. +2 −2 README.md
  2. +8 −10 src/converter.js
@@ -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.
@@ -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 {
@@ -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){");
@@ -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);

0 comments on commit bc76ad7

Please sign in to comment.