Skip to content

Commit

Permalink
Merge pull request #8 from protocolbuffers/master
Browse files Browse the repository at this point in the history
Fix js message pivot selection (protocolbuffers#6813)
  • Loading branch information
sthagen authored Feb 27, 2020
2 parents f106ca2 + 743a432 commit f43ed72
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions js/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var group1Protos = [
'testbinary.proto',
'testempty.proto',
'test.proto',
'testlargenumbers.proto',
];

var group2Protos = [
Expand Down
5 changes: 4 additions & 1 deletion js/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
34 changes: 34 additions & 0 deletions js/message_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 changes: 40 additions & 0 deletions js/testlargenumbers.proto
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f43ed72

Please sign in to comment.