Commit 42470ba6 authored by Jan-Willem Maarse's avatar Jan-Willem Maarse Committed by Android Git Automerger

am 70eec2ca: am 58eb8e98: am d20f0a42: Merge "Fix NPE when clearing an extension in nano protos"

* commit '70eec2cabb5fa7bf8d7d492ec46905dae3df5a76':
  Fix NPE when clearing an extension in nano protos
parents 05b6a7be 00120917
...@@ -268,7 +268,7 @@ public class Extension<M extends ExtendableMessageNano<M>, T> { ...@@ -268,7 +268,7 @@ public class Extension<M extends ExtendableMessageNano<M>, T> {
// After deletion or no-op addition (due to 'value' being an array of empty or // After deletion or no-op addition (due to 'value' being an array of empty or
// null-only elements), unknownFields may be empty. Discard the ArrayList if so. // null-only elements), unknownFields may be empty. Discard the ArrayList if so.
return (unknownFields.size() == 0) ? null : unknownFields; return (unknownFields == null || unknownFields.isEmpty()) ? null : unknownFields;
} }
protected UnknownFieldData writeData(Object value) { protected UnknownFieldData writeData(Object value) {
......
...@@ -2910,6 +2910,20 @@ public class NanoTest extends TestCase { ...@@ -2910,6 +2910,20 @@ public class NanoTest extends TestCase {
assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum))); assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum)));
} }
public void testNullExtensions() throws Exception {
// Check that clearing the extension on an empty message is a no-op.
Extensions.ExtendableMessage message = new Extensions.ExtendableMessage();
message.setExtension(SingularExtensions.someMessage, null);
assertEquals(0, MessageNano.toByteArray(message).length);
// Check that the message is empty after setting and clearing an extension.
AnotherMessage another = new AnotherMessage();
message.setExtension(SingularExtensions.someMessage, another);
assertTrue(MessageNano.toByteArray(message).length > 0);
message.setExtension(SingularExtensions.someMessage, null);
assertEquals(0, MessageNano.toByteArray(message).length);
}
public void testUnknownFields() throws Exception { public void testUnknownFields() throws Exception {
// Check that we roundtrip (serialize and deserialize) unrecognized fields. // Check that we roundtrip (serialize and deserialize) unrecognized fields.
AnotherMessage message = new AnotherMessage(); AnotherMessage message = new AnotherMessage();
......
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