From d1af029104e9b594a99209484914b822addb08fa Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Fri, 25 May 2018 14:12:43 -0700 Subject: 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. --- js/map.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'js/map.js') diff --git a/js/map.js b/js/map.js index 7b5b2c38..2fb14837 100644 --- a/js/map.js +++ b/js/map.js @@ -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); -- cgit v1.2.3