diff options
author | Ulas Kirazci <ulas@google.com> | 2013-10-04 19:56:49 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-10-04 19:56:49 +0000 |
commit | e7336cdddb79b2d7b6b0b2d758069973d2699245 (patch) | |
tree | 1bca00fbae2863ef4ddcca2c08585ebd259b598b /java | |
parent | 4b7983cb71bda102b5b5d3aa2fb6767d0df4f6d4 (diff) | |
parent | b3c24e0ed285b9457560040fe49cb4f9616e721b (diff) | |
download | protobuf-e7336cdddb79b2d7b6b0b2d758069973d2699245.tar.gz protobuf-e7336cdddb79b2d7b6b0b2d758069973d2699245.tar.bz2 protobuf-e7336cdddb79b2d7b6b0b2d758069973d2699245.zip |
Merge "Fix roundtrip failure with groups when unknown fields are enabled."
Diffstat (limited to 'java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/nano/WireFormatNano.java | 9 | ||||
-rw-r--r-- | java/src/test/java/com/google/protobuf/NanoTest.java | 17 |
2 files changed, 24 insertions, 2 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java b/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java index c901e59c..301ff1db 100644 --- a/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java +++ b/java/src/main/java/com/google/protobuf/nano/WireFormatNano.java @@ -119,6 +119,9 @@ public final class WireFormatNano { * <p>Generated messages will call this for unknown fields if the store_unknown_fields * option is on. * + * <p>Note that the tag might be a end-group tag (rather than the start of an unknown field) in + * which case we do not want to add an unknown field entry. + * * @param data a Collection in which to store the data. * @param input the input buffer. * @param tag the tag of the field. @@ -130,11 +133,13 @@ public final class WireFormatNano { final CodedInputByteBufferNano input, final int tag) throws IOException { int startPos = input.getPosition(); - boolean skip = input.skipField(tag); + if (!input.skipField(tag)) { + return false; // This wasn't an unknown field, it's an end-group tag. + } int endPos = input.getPosition(); byte[] bytes = input.getData(startPos, endPos - startPos); data.add(new UnknownFieldData(tag, bytes)); - return skip; + return true; } /** diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 84836195..ca0bcda4 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -35,6 +35,7 @@ import com.google.protobuf.nano.EnumClassNanoMultiple; import com.google.protobuf.nano.EnumClassNanos; import com.google.protobuf.nano.Extensions; import com.google.protobuf.nano.Extensions.AnotherMessage; +import com.google.protobuf.nano.Extensions.MessageWithGroup; import com.google.protobuf.nano.FileScopeEnumMultiple; import com.google.protobuf.nano.FileScopeEnumRefNano; import com.google.protobuf.nano.InternalNano; @@ -505,6 +506,22 @@ public class NanoTest extends TestCase { assertEquals(1, newMsg.optionalGroup.a); } + public void testNanoOptionalGroupWithUnknownFieldsEnabled() throws Exception { + MessageWithGroup msg = new MessageWithGroup(); + MessageWithGroup.Group grp = new MessageWithGroup.Group(); + grp.a = 1; + msg.group = grp; + byte [] serialized = MessageNano.toByteArray(msg); + + MessageWithGroup parsed = MessageWithGroup.parseFrom(serialized); + assertTrue(msg.group != null); + assertEquals(1, msg.group.a); + + byte [] serialized2 = MessageNano.toByteArray(parsed); + assertEquals(serialized2.length, serialized.length); + MessageWithGroup parsed2 = MessageWithGroup.parseFrom(serialized2); + } + public void testNanoOptionalNestedMessage() throws Exception { TestAllTypesNano msg = new TestAllTypesNano(); TestAllTypesNano.NestedMessage nestedMsg = new TestAllTypesNano.NestedMessage(); |