From bc76ad732fc0689cb0a2aeeb91b06ec5331d7972 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Tue, 21 Mar 2017 22:23:31 +0100 Subject: [PATCH] Fixed: Exclude any fields part of some oneof when populating defaults in toObject, see #710 --- README.md | 4 ++-- src/converter.js | 18 ++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bb4618a80..ec26e3c9f 100644 --- a/README.md +++ b/README.md @@ -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*
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*
- 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 { diff --git a/src/converter.js b/src/converter.js index c0c90e6e6..684781c0b 100644 --- a/src/converter.js +++ b/src/converter.js @@ -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);