Commit 881db010 authored by Jon Skeet's avatar Jon Skeet

Fix initialization check when an optional field has required subfields

parent 0dabc77f
...@@ -270,5 +270,21 @@ namespace Google.ProtocolBuffers { ...@@ -270,5 +270,21 @@ namespace Google.ProtocolBuffers {
Assert.IsFalse(TestAllExtensions.DefaultInstance.HasExtension(MultiFileProto.ExtensionWithOuter)); Assert.IsFalse(TestAllExtensions.DefaultInstance.HasExtension(MultiFileProto.ExtensionWithOuter));
} }
[Test]
public void OptionalFieldWithRequiredSubfieldsOptimizedForSize() {
TestOptionalOptimizedForSize message = TestOptionalOptimizedForSize.DefaultInstance;
Assert.IsTrue(message.IsInitialized);
message = TestOptionalOptimizedForSize.CreateBuilder().SetO(
TestRequiredOptimizedForSize.CreateBuilder().BuildPartial()
).BuildPartial();
Assert.IsFalse(message.IsInitialized);
message = TestOptionalOptimizedForSize.CreateBuilder().SetO(
TestRequiredOptimizedForSize.CreateBuilder().SetX(5).BuildPartial()
).BuildPartial();
Assert.IsTrue(message.IsInitialized);
}
} }
} }
...@@ -92,7 +92,7 @@ namespace Google.ProtocolBuffers { ...@@ -92,7 +92,7 @@ namespace Google.ProtocolBuffers {
} }
} }
} else { } else {
if (!((IMessage) this[field]).IsInitialized) { if (HasField(field) && !((IMessage) this[field]).IsInitialized) {
return false; return false;
} }
} }
......
...@@ -94,7 +94,7 @@ public abstract class GeneratedMessage extends AbstractMessage { ...@@ -94,7 +94,7 @@ public abstract class GeneratedMessage extends AbstractMessage {
} }
} }
} else { } else {
if (!((Message) getField(field)).isInitialized()) { if (hasField(field) && !((Message) getField(field)).isInitialized()) {
return false; return false;
} }
} }
......
...@@ -16,19 +16,21 @@ ...@@ -16,19 +16,21 @@
package com.google.protobuf; package com.google.protobuf;
import java.util.Arrays;
import junit.framework.TestCase;
import protobuf_unittest.EnumWithNoOuter;
import protobuf_unittest.MessageWithNoOuter;
import protobuf_unittest.MultipleFilesTestProto;
import protobuf_unittest.ServiceWithNoOuter;
import protobuf_unittest.UnittestProto; import protobuf_unittest.UnittestProto;
import protobuf_unittest.UnittestProto.ForeignMessage; import protobuf_unittest.UnittestOptimizeFor.TestOptionalOptimizedForSize;
import protobuf_unittest.UnittestOptimizeFor.TestRequiredOptimizedForSize;
import protobuf_unittest.UnittestProto.ForeignEnum; import protobuf_unittest.UnittestProto.ForeignEnum;
import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.ForeignMessage;
import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllExtensions;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; import protobuf_unittest.UnittestProto.TestExtremeDefaultValues;
import protobuf_unittest.MultipleFilesTestProto;
import protobuf_unittest.MessageWithNoOuter;
import protobuf_unittest.EnumWithNoOuter;
import protobuf_unittest.ServiceWithNoOuter;
import junit.framework.TestCase;
import java.util.Arrays;
/** /**
* Unit test for generated messages and generated code. See also * Unit test for generated messages and generated code. See also
...@@ -93,7 +95,7 @@ public class GeneratedMessageTest extends TestCase { ...@@ -93,7 +95,7 @@ public class GeneratedMessageTest extends TestCase {
// TODO(ngd): Upgrade to using real #equals method once implemented // TODO(ngd): Upgrade to using real #equals method once implemented
assertEquals(expectedMessage.toString(), message.toString()); assertEquals(expectedMessage.toString(), message.toString());
} }
public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception { public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception {
TestAllTypes message = TestAllTypes.newBuilder() TestAllTypes message = TestAllTypes.newBuilder()
// Pass builder for foreign message instance. // Pass builder for foreign message instance.
...@@ -243,4 +245,19 @@ public class GeneratedMessageTest extends TestCase { ...@@ -243,4 +245,19 @@ public class GeneratedMessageTest extends TestCase {
TestAllExtensions.getDefaultInstance().hasExtension( TestAllExtensions.getDefaultInstance().hasExtension(
MultipleFilesTestProto.extensionWithOuter)); MultipleFilesTestProto.extensionWithOuter));
} }
public void testOptionalFieldWithRequiredSubfieldsOptimizedForSize() throws Exception {
TestOptionalOptimizedForSize message = TestOptionalOptimizedForSize.getDefaultInstance();
assertTrue(message.isInitialized());
message = TestOptionalOptimizedForSize.newBuilder().setO(
TestRequiredOptimizedForSize.newBuilder().buildPartial()
).buildPartial();
assertFalse(message.isInitialized());
message = TestOptionalOptimizedForSize.newBuilder().setO(
TestRequiredOptimizedForSize.newBuilder().setX(5).buildPartial()
).buildPartial();
assertTrue(message.isInitialized());
}
} }
...@@ -39,3 +39,11 @@ message TestOptimizedForSize { ...@@ -39,3 +39,11 @@ message TestOptimizedForSize {
optional int32 test_extension = 1234; optional int32 test_extension = 1234;
} }
} }
message TestRequiredOptimizedForSize {
required int32 x = 1;
}
message TestOptionalOptimizedForSize {
optional TestRequiredOptimizedForSize o = 1;
}
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