Skip to content

Commit

Permalink
Merge pull request #7246 from ObsidianMinor/csharp/issue7241
Browse files Browse the repository at this point in the history
Correctly set ExtensionRegistry when parsing with MessageParser, but using an already existing CodedInputStream
  • Loading branch information
jtattermusch committed Oct 13, 2021
2 parents 21873ae + d36d84c commit 3c8c0ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions csharp/src/Google.Protobuf.Test/GeneratedMessageTest.Proto2.cs
Expand Up @@ -380,5 +380,18 @@ public void RoundTrip_NestedExtensionGroup()
TestGroupExtension.Parser.WithExtensionRegistry(new ExtensionRegistry() { TestNestedExtension.Extensions.OptionalGroupExtension }),
message);
}

[Test]
public void RoundTrip_ParseUsingCodedInput()
{
var message = new TestAllExtensions();
message.SetExtension(UnittestExtensions.OptionalBoolExtension, true);
byte[] bytes = message.ToByteArray();
using (CodedInputStream input = new CodedInputStream(bytes))
{
var parsed = TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { UnittestExtensions.OptionalBoolExtension }).ParseFrom(input);
Assert.AreEqual(message, parsed);
}
}
}
}
3 changes: 3 additions & 0 deletions csharp/src/Google.Protobuf/MessageParser.cs
Expand Up @@ -187,14 +187,17 @@ public IMessage ParseJson(string json)
internal void MergeFrom(IMessage message, CodedInputStream codedInput)
{
bool originalDiscard = codedInput.DiscardUnknownFields;
ExtensionRegistry originalRegistry = codedInput.ExtensionRegistry;
try
{
codedInput.DiscardUnknownFields = DiscardUnknownFields;
codedInput.ExtensionRegistry = Extensions;
message.MergeFrom(codedInput);
}
finally
{
codedInput.DiscardUnknownFields = originalDiscard;
codedInput.ExtensionRegistry = originalRegistry;
}
}

Expand Down

0 comments on commit 3c8c0ae

Please sign in to comment.