Commit 25cf9e31 authored by kenton@google.com's avatar kenton@google.com

getExtension() on an empty repeated field should return an empty list, not a…

getExtension() on an empty repeated field should return an empty list, not a singular default value.
parent d6e84b3e
......@@ -352,7 +352,10 @@ public abstract class GeneratedMessage extends AbstractMessage {
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(
......
......@@ -83,7 +83,7 @@ public class LiteTest extends TestCase {
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()
......@@ -104,6 +104,8 @@ public class LiteTest extends TestCase {
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(
......
......@@ -1664,6 +1664,34 @@ class TestUtil {
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 ));
......
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