From 1d0988b8efcb9eae937eb8438f6dd6b1ef66b6df Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 6 Jun 2017 09:10:27 -0400 Subject: [PATCH] ObjC: Preserve unknown fields in proto3 syntax files. As announced: https://groups.google.com/forum/#!topic/protobuf/VX5qEmTW3y0 The ObjC side of https://github.com/google/protobuf/issues/272 --- objectivec/GPBDescriptor_PackagePrivate.h | 4 --- objectivec/GPBMessage.m | 16 ++++------ .../Tests/GPBMessageTests+Serialization.m | 29 ------------------- 3 files changed, 5 insertions(+), 44 deletions(-) diff --git a/objectivec/GPBDescriptor_PackagePrivate.h b/objectivec/GPBDescriptor_PackagePrivate.h index 9173e7a2b..452b3f8e7 100644 --- a/objectivec/GPBDescriptor_PackagePrivate.h +++ b/objectivec/GPBDescriptor_PackagePrivate.h @@ -286,10 +286,6 @@ uint32_t GPBFieldTag(GPBFieldDescriptor *self); // would be the wire type for packed. uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self); -GPB_INLINE BOOL GPBPreserveUnknownFields(GPBFileSyntax syntax) { - return syntax != GPBFileSyntaxProto3; -} - GPB_INLINE BOOL GPBHasPreservingUnknownEnumSemantics(GPBFileSyntax syntax) { return syntax == GPBFileSyntaxProto3; } diff --git a/objectivec/GPBMessage.m b/objectivec/GPBMessage.m index 627a396e5..55c7b308e 100644 --- a/objectivec/GPBMessage.m +++ b/objectivec/GPBMessage.m @@ -2372,17 +2372,11 @@ static void MergeRepeatedNotPackedFieldFromCodedInputStream( // zero signals EOF / limit reached return; } else { - if (GPBPreserveUnknownFields(syntax)) { - if (![self parseUnknownField:input - extensionRegistry:extensionRegistry - tag:tag]) { - // it's an endgroup tag - return; - } - } else { - if (![input skipField:tag]) { - return; - } + if (![self parseUnknownField:input + extensionRegistry:extensionRegistry + tag:tag]) { + // it's an endgroup tag + return; } } } // if(!merged) diff --git a/objectivec/Tests/GPBMessageTests+Serialization.m b/objectivec/Tests/GPBMessageTests+Serialization.m index 2996f5ff6..4a4c54476 100644 --- a/objectivec/Tests/GPBMessageTests+Serialization.m +++ b/objectivec/Tests/GPBMessageTests+Serialization.m @@ -113,35 +113,6 @@ static NSData *DataFromCStr(const char *str) { [msg release]; } -- (void)testProto3DroppingUnknownFields { - DropUnknownsFooWithExtraFields *fooWithExtras = - [[DropUnknownsFooWithExtraFields alloc] init]; - - fooWithExtras.int32Value = 1; - fooWithExtras.enumValue = DropUnknownsFooWithExtraFields_NestedEnum_Baz; - fooWithExtras.extraInt32Value = 2; - - NSData *data = [fooWithExtras data]; - XCTAssertNotNil(data); - DropUnknownsFoo *foo = [DropUnknownsFoo parseFromData:data error:NULL]; - - XCTAssertEqual(foo.int32Value, 1); - XCTAssertEqual(foo.enumValue, DropUnknownsFoo_NestedEnum_Baz); - // Nothing should end up in the unknowns. - XCTAssertEqual([foo.unknownFields countOfFields], 0U); - - [fooWithExtras release]; - data = [foo data]; - fooWithExtras = - [DropUnknownsFooWithExtraFields parseFromData:data error:NULL]; - XCTAssertEqual(fooWithExtras.int32Value, 1); - XCTAssertEqual(fooWithExtras.enumValue, - DropUnknownsFooWithExtraFields_NestedEnum_Baz); - // And the extra value is gone (back to the default). - XCTAssertEqual(fooWithExtras.extraInt32Value, 0); - XCTAssertEqual([foo.unknownFields countOfFields], 0U); -} - - (void)testProto2UnknownEnumToUnknownField { Message3 *orig = [[Message3 alloc] init];