Commit 0fad0e13 authored by Adam Cozzette's avatar Adam Cozzette Committed by GitHub

Merge pull request #2794 from acozzette/jspb-extensions

JS: ensure that extension values are serialized even if they're falsy
parents 4842363e 651ba62a
...@@ -283,8 +283,7 @@ function checkAllFields(original, copy) { ...@@ -283,8 +283,7 @@ function checkAllFields(original, copy) {
* @param {!proto.jspb.test.TestExtendable} msg * @param {!proto.jspb.test.TestExtendable} msg
*/ */
function checkExtensions(msg) { function checkExtensions(msg) {
assertEquals(-42, assertEquals(0, msg.getExtension(proto.jspb.test.extendOptionalInt32));
msg.getExtension(proto.jspb.test.extendOptionalInt32));
assertEquals(-0x7fffffff00000000, assertEquals(-0x7fffffff00000000,
msg.getExtension(proto.jspb.test.extendOptionalInt64)); msg.getExtension(proto.jspb.test.extendOptionalInt64));
assertEquals(0x80000000, assertEquals(0x80000000,
...@@ -512,8 +511,7 @@ describe('protoBinaryTest', function() { ...@@ -512,8 +511,7 @@ describe('protoBinaryTest', function() {
* @param {proto.jspb.test.TestExtendable} msg * @param {proto.jspb.test.TestExtendable} msg
*/ */
function fillExtensions(msg) { function fillExtensions(msg) {
msg.setExtension( msg.setExtension(proto.jspb.test.extendOptionalInt32, 0);
proto.jspb.test.extendOptionalInt32, -42);
msg.setExtension( msg.setExtension(
proto.jspb.test.extendOptionalInt64, -0x7fffffff00000000); proto.jspb.test.extendOptionalInt64, -0x7fffffff00000000);
msg.setExtension( msg.setExtension(
......
...@@ -497,7 +497,7 @@ jspb.Message.toObjectExtension = function(proto, obj, extensions, ...@@ -497,7 +497,7 @@ jspb.Message.toObjectExtension = function(proto, obj, extensions,
for (var fieldNumber in extensions) { for (var fieldNumber in extensions) {
var fieldInfo = extensions[fieldNumber]; var fieldInfo = extensions[fieldNumber];
var value = getExtensionFn.call(proto, fieldInfo); var value = getExtensionFn.call(proto, fieldInfo);
if (value) { if (goog.isDefAndNotNull(value)) {
for (var name in fieldInfo.fieldName) { for (var name in fieldInfo.fieldName) {
if (fieldInfo.fieldName.hasOwnProperty(name)) { if (fieldInfo.fieldName.hasOwnProperty(name)) {
break; // the compiled field name break; // the compiled field name
...@@ -541,7 +541,7 @@ jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, ...@@ -541,7 +541,7 @@ jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions,
'without binary serialization support'); 'without binary serialization support');
} }
var value = getExtensionFn.call(proto, fieldInfo); var value = getExtensionFn.call(proto, fieldInfo);
if (value) { if (goog.isDefAndNotNull(value)) {
if (fieldInfo.isMessageType()) { if (fieldInfo.isMessageType()) {
// If the message type of the extension was generated without binary // If the message type of the extension was generated without binary
// support, there may not be a binary message serializer function, and // support, there may not be a binary message serializer function, and
......
...@@ -693,10 +693,11 @@ describe('Message test suite', function() { ...@@ -693,10 +693,11 @@ describe('Message test suite', function() {
}); });
it('testToObject_hasExtensionField', function() { it('testToObject_hasExtensionField', function() {
var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]); var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1'], 102: ''}]);
var obj = data.toObject(); var obj = data.toObject();
assertEquals('str1', obj.str1); assertEquals('str1', obj.str1);
assertEquals('ext1', obj.extField.ext1); assertEquals('ext1', obj.extField.ext1);
assertEquals('', obj.str);
}); });
it('testGetExtension', function() { it('testGetExtension', function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment