diff options
author | Adam Cozzette <acozzette@google.com> | 2018-05-25 14:12:43 -0700 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2018-05-25 14:44:22 -0700 |
commit | d1af029104e9b594a99209484914b822addb08fa (patch) | |
tree | ea2c0cfe72f25c79e6bb0e0d7f8f4da320cc3c4a /js/map.js | |
parent | 6f723a6624de0e0f9b1ae16ae002e678613e07b8 (diff) | |
download | protobuf-d1af029104e9b594a99209484914b822addb08fa.tar.gz protobuf-d1af029104e9b594a99209484914b822addb08fa.tar.bz2 protobuf-d1af029104e9b594a99209484914b822addb08fa.zip |
Fixed JS parsing of unspecified map keys
We need to use a default of 0 when parsing unspecified map keys, instead
of failing an assertion.
This change was written by Michael Aaron (michaelaaron@google.com) but I
am cherry-picking it directly instead of waiting for the next sync of
Google-internal changes.
Diffstat (limited to 'js/map.js')
-rw-r--r-- | js/map.js | 15 |
1 files changed, 11 insertions, 4 deletions
@@ -443,7 +443,8 @@ jspb.Map.prototype.serializeBinary = function( /** * Read one key/value message from the given BinaryReader. Compatible as the * `reader` callback parameter to jspb.BinaryReader.readMessage, to be called - * when a key/value pair submessage is encountered. + * when a key/value pair submessage is encountered. If the Key is undefined, + * we should default it to 0. * @template K, V * @param {!jspb.Map} map * @param {!jspb.BinaryReader} reader @@ -457,12 +458,17 @@ jspb.Map.prototype.serializeBinary = function( * readMessage, in which case the second callback arg form is used. * * @param {?function(V,!jspb.BinaryReader)=} opt_valueReaderCallback - * The BinaryReader parsing callback for type V, if V is a message type. + * The BinaryReader parsing callback for type V, if V is a message type + * + * @param {K=} opt_defaultKey + * The default value for the type of map keys. Accepting map + * entries with unset keys is required for maps to be backwards compatible + * with the repeated message representation described here: goo.gl/zuoLAC * */ jspb.Map.deserializeBinary = function(map, reader, keyReaderFn, valueReaderFn, - opt_valueReaderCallback) { - var key = undefined; + opt_valueReaderCallback, opt_defaultKey) { + var key = opt_defaultKey; var value = undefined; while (reader.nextField()) { @@ -470,6 +476,7 @@ jspb.Map.deserializeBinary = function(map, reader, keyReaderFn, valueReaderFn, break; } var field = reader.getFieldNumber(); + if (field == 1) { // Key. key = keyReaderFn.call(reader); |