diff --git a/Makefile.am b/Makefile.am index 07968e941c57..4e5c164ca6e0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1217,6 +1217,7 @@ js_EXTRA_DIST= \ js/test_bootstrap.js \ js/testbinary.proto \ js/testempty.proto \ + js/testlargenumbers.proto js/experimental/runtime/testing/jasmine_protobuf.js \ js/experimental/runtime/testing/ensure_custom_equality_test.js \ js/experimental/runtime/testing/binary/test_message.js \ diff --git a/js/gulpfile.js b/js/gulpfile.js index 1f0946caf09c..a81c01117907 100644 --- a/js/gulpfile.js +++ b/js/gulpfile.js @@ -37,6 +37,7 @@ var group1Protos = [ 'testbinary.proto', 'testempty.proto', 'test.proto', + 'testlargenumbers.proto', ]; var group2Protos = [ diff --git a/js/message.js b/js/message.js index 5c081129a9ac..c1736b3f11c6 100644 --- a/js/message.js +++ b/js/message.js @@ -1112,8 +1112,11 @@ jspb.Message.setFieldIgnoringDefault_ = function( goog.asserts.assertInstanceof(msg, jspb.Message); if (value !== defaultValue) { jspb.Message.setField(msg, fieldNumber, value); - } else { + } else if (fieldNumber < msg.pivot_) { msg.array[jspb.Message.getIndex_(msg, fieldNumber)] = null; + } else { + jspb.Message.maybeInitEmptyExtensionObject_(msg); + delete msg.extensionObject_[fieldNumber]; } return msg; }; diff --git a/js/message_test.js b/js/message_test.js index e038f65c519e..3cac4c668d9d 100644 --- a/js/message_test.js +++ b/js/message_test.js @@ -118,6 +118,8 @@ goog.require('proto.jspb.test.ExtensionMessage'); goog.require('proto.jspb.test.TestExtensionsMessage'); goog.require('proto.jspb.test.TestAllowAliasEnum'); +// CommonJS-LoadFromFile: testlargenumbers_pb proto.jspb.test +goog.require('proto.jspb.test.MessageWithLargeFieldNumbers'); describe('Message test suite', function() { var stubs = new goog.testing.PropertyReplacer(); @@ -1075,4 +1077,36 @@ describe('Message test suite', function() { assertEquals(12, package2Message.getA()); }); + + it('testMessageWithLargeFieldNumbers', function() { + var message = new proto.jspb.test.MessageWithLargeFieldNumbers; + + message.setAString('string'); + assertEquals('string', message.getAString()); + + message.setAString(''); + assertEquals('', message.getAString()); + + message.setAString('new string'); + assertEquals('new string', message.getAString()); + + message.setABoolean(true); + assertEquals(true, message.getABoolean()); + + message.setABoolean(false); + assertEquals(false, message.getABoolean()); + + message.setABoolean(true); + assertEquals(true, message.getABoolean()); + + message.setAInt(42); + assertEquals(42, message.getAInt()); + + message.setAInt(0); + assertEquals(0, message.getAInt()); + + message.setAInt(42); + assertEquals(42, message.getAInt()); + }); + }); diff --git a/js/testlargenumbers.proto b/js/testlargenumbers.proto new file mode 100644 index 000000000000..47aef35798a5 --- /dev/null +++ b/js/testlargenumbers.proto @@ -0,0 +1,40 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package jspb.test; + + +message MessageWithLargeFieldNumbers { + string a_string = 1; + bool a_boolean = 900; + int32 a_int = 5000; +}