Commits

Anonymous committed 4a9e31a

getExtension() on an empty repeated field should return an empty list, not a singular default value.

Comments (0)

Files changed (3)

java/src/main/java/com/google/protobuf/GeneratedMessage.java

       FieldDescriptor descriptor = extension.getDescriptor();
       final Object value = extensions.getField(descriptor);
       if (value == null) {
-        if (descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
+        if (descriptor.isRepeated()) {
+          return (Type) Collections.emptyList();
+        } else if (descriptor.getJavaType() ==
+                   FieldDescriptor.JavaType.MESSAGE) {
           return (Type) extension.getMessageDefaultInstance();
         } else {
           return (Type) extension.fromReflectionType(

java/src/test/java/com/google/protobuf/LiteTest.java

   public void testLiteExtensions() throws Exception {
     // TODO(kenton):  Unlike other features of the lite library, extensions are
     //   implemented completely differently from the regular library.  We
-    //   need to test them more thoroughly, once they are fully-implemented.
+    //   should probably test them more thoroughly.
 
     TestAllExtensionsLite message =
       TestAllExtensionsLite.newBuilder()
         UnittestLite.optionalInt32ExtensionLite));
     assertEquals(1, message2.getExtensionCount(
         UnittestLite.repeatedStringExtensionLite));
+    assertEquals(1, message2.getExtension(
+        UnittestLite.repeatedStringExtensionLite).size());
     assertEquals("hello", message2.getExtension(
         UnittestLite.repeatedStringExtensionLite, 0));
     assertEquals(TestAllTypesLite.NestedEnum.BAZ, message2.getExtension(

java/src/test/java/com/google/protobuf/TestUtil.java

     Assert.assertEquals(0, message.getExtensionCount(repeatedStringPieceExtension));
     Assert.assertEquals(0, message.getExtensionCount(repeatedCordExtension));
 
+    // Repeated fields are empty via getExtension().size().
+    Assert.assertEquals(0, message.getExtension(repeatedInt32Extension   ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedInt64Extension   ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedUint32Extension  ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedUint64Extension  ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedSint32Extension  ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedSint64Extension  ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedFixed32Extension ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedFixed64Extension ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedSfixed32Extension).size());
+    Assert.assertEquals(0, message.getExtension(repeatedSfixed64Extension).size());
+    Assert.assertEquals(0, message.getExtension(repeatedFloatExtension   ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedDoubleExtension  ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedBoolExtension    ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedStringExtension  ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedBytesExtension   ).size());
+
+    Assert.assertEquals(0, message.getExtension(repeatedGroupExtension         ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedNestedMessageExtension ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedForeignMessageExtension).size());
+    Assert.assertEquals(0, message.getExtension(repeatedImportMessageExtension ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedNestedEnumExtension    ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedForeignEnumExtension   ).size());
+    Assert.assertEquals(0, message.getExtension(repeatedImportEnumExtension    ).size());
+
+    Assert.assertEquals(0, message.getExtension(repeatedStringPieceExtension).size());
+    Assert.assertEquals(0, message.getExtension(repeatedCordExtension).size());
+
     // hasBlah() should also be false for all default fields.
     Assert.assertFalse(message.hasExtension(defaultInt32Extension   ));
     Assert.assertFalse(message.hasExtension(defaultInt64Extension   ));