diff --git a/java/core/src/main/java/com/google/protobuf/MessageSchema.java b/java/core/src/main/java/com/google/protobuf/MessageSchema.java index 084d687d1a..ae5dddc53b 100644 --- a/java/core/src/main/java/com/google/protobuf/MessageSchema.java +++ b/java/core/src/main/java/com/google/protobuf/MessageSchema.java @@ -5510,6 +5510,12 @@ final class MessageSchema implements Schema { getMessageFieldSchema(pos).makeImmutable(UNSAFE.getObject(message, offset)); } break; + case 60: // ONEOF_MESSAGE + case 68: // ONEOF_GROUP + if (isOneofPresent(message, numberAt(pos), pos)) { + getMessageFieldSchema(pos).makeImmutable(UNSAFE.getObject(message, offset)); + } + break; case 18: // DOUBLE_LIST: case 19: // FLOAT_LIST: case 20: // INT64_LIST: diff --git a/java/lite/src/test/java/com/google/protobuf/LiteTest.java b/java/lite/src/test/java/com/google/protobuf/LiteTest.java index a4f95a2666..a58ce95df0 100644 --- a/java/lite/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/lite/src/test/java/com/google/protobuf/LiteTest.java @@ -190,6 +190,18 @@ public class LiteTest { } } + @Test + public void testParsedOneofSubMessageIsImmutable() throws InvalidProtocolBufferException { + TestAllTypesLite message = + TestAllTypesLite.parseFrom( + TestAllTypesLite.newBuilder() + .setOneofNestedMessage(NestedMessage.newBuilder().addDd(1234).build()) + .build() + .toByteArray()); + IntArrayList subList = (IntArrayList) message.getOneofNestedMessage().getDdList(); + assertThat(subList.isModifiable()).isFalse(); + } + @Test public void testMemoization() throws Exception { GeneratedMessageLite message = TestUtilLite.getAllLiteExtensionsSet(); @@ -2365,8 +2377,7 @@ public class LiteTest { Foo fooWithOnlyValue = Foo.newBuilder().setValue(1).build(); Foo fooWithValueAndExtension = - fooWithOnlyValue - .toBuilder() + fooWithOnlyValue.toBuilder() .setValue(1) .setExtension(Bar.fooExt, Bar.newBuilder().setName("name").build()) .build(); @@ -2382,8 +2393,7 @@ public class LiteTest { Foo fooWithOnlyValue = Foo.newBuilder().setValue(1).build(); Foo fooWithValueAndExtension = - fooWithOnlyValue - .toBuilder() + fooWithOnlyValue.toBuilder() .setValue(1) .setExtension(Bar.fooExt, Bar.newBuilder().setName("name").build()) .build(); @@ -2515,9 +2525,9 @@ public class LiteTest { assertWithMessage("expected exception").fail(); } catch (InvalidProtocolBufferException expected) { assertThat( - TestAllExtensionsLite.newBuilder() - .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) - .build()) + TestAllExtensionsLite.newBuilder() + .setExtension(UnittestLite.optionalInt32ExtensionLite, 123) + .build()) .isEqualTo(expected.getUnfinishedMessage()); } } diff --git a/src/google/protobuf/unittest_lite.proto b/src/google/protobuf/unittest_lite.proto index 010d4a9c0b..e2730c6419 100644 --- a/src/google/protobuf/unittest_lite.proto +++ b/src/google/protobuf/unittest_lite.proto @@ -47,6 +47,7 @@ message TestAllTypesLite { message NestedMessage { optional int32 bb = 1; optional int64 cc = 2; + repeated int32 dd = 3 [packed = true]; } message NestedMessage2 {