getExtension() on an empty repeated field should return an empty list, not a singular default value.
This commit is contained in:
parent
d6e84b3ed8
commit
25cf9e3153
3 changed files with 35 additions and 2 deletions
|
@ -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 ));
|
||||
|
|
Loading…
Add table
Reference in a new issue