浏览代码

Fix js message pivot selection (#6813)

* fix javascript setFieldIgnoringDefault_ logic

* remove package-lock.json

* fix build script to include new UT asset file

Co-authored-by: Daniel Kurka <kurka.daniel@gmail.com>
Artem Kustikov 5 年之前
父节点
当前提交
743a4322ba
共有 5 个文件被更改,包括 80 次插入1 次删除
  1. 1 0
      Makefile.am
  2. 1 0
      js/gulpfile.js
  3. 4 1
      js/message.js
  4. 34 0
      js/message_test.js
  5. 40 0
      js/testlargenumbers.proto

+ 1 - 0
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               \

+ 1 - 0
js/gulpfile.js

@@ -37,6 +37,7 @@ var group1Protos = [
   'testbinary.proto',
   'testempty.proto',
   'test.proto',
+  'testlargenumbers.proto',
 ];
 
 var group2Protos = [

+ 4 - 1
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;
 };

+ 34 - 0
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());
+  });
+
 });

+ 40 - 0
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;
+}