Skip to content

Commit bc76ad7

Browse files
committed
Fixed: Exclude any fields part of some oneof when populating defaults in toObject, see #710
1 parent 320dea5 commit bc76ad7

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ In case of doubt you can just use the full library.
8686
Usage
8787
-----
8888

89-
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.
89+
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.
9090

9191
* **Message.verify**(message: *Object*): *?string*<br />
9292
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
109109
additionally prepends the length of the message as a varint.
110110

111111
* **Message.decode**(reader: *Reader|Uint8Array*): *Message*<br />
112-
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.
112+
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.
113113

114114
```js
115115
try {

src/converter.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,13 @@ converter.toObject = function toObject(mtype) {
203203

204204
var repeatedFields = [],
205205
mapFields = [],
206-
otherFields = [],
206+
normalFields = [],
207207
i = 0;
208208
for (; i < fields.length; ++i)
209-
if (fields[i].resolve().repeated)
210-
repeatedFields.push(fields[i]);
211-
else if (fields[i].map)
212-
mapFields.push(fields[i]);
213-
else
214-
otherFields.push(fields[i]);
209+
if (!fields[i].partOf)
210+
( fields[i].resolve().repeated ? repeatedFields
211+
: fields[i].map ? mapFields
212+
: normalFields).push(fields[i]);
215213

216214
if (repeatedFields.length) { gen
217215
("if(o.arrays||o.defaults){");
@@ -229,10 +227,10 @@ converter.toObject = function toObject(mtype) {
229227
("}");
230228
}
231229

232-
if (otherFields.length) { gen
230+
if (normalFields.length) { gen
233231
("if(o.defaults){");
234-
for (i = 0, field; i < otherFields.length; ++i) {
235-
var field = otherFields[i],
232+
for (i = 0, field; i < normalFields.length; ++i) {
233+
var field = normalFields[i],
236234
prop = util.safeProp(field.name);
237235
if (field.resolvedType instanceof Enum) gen
238236
("d%s=o.enums===String?%j:%j", prop, field.resolvedType.valuesById[field.typeDefault], field.typeDefault);

0 commit comments

Comments
 (0)