From 0e9dd4ca09cdd386ea0c86396aa542dad7238c1e Mon Sep 17 00:00:00 2001 From: Manuel Date: Thu, 30 May 2024 02:20:22 -0700 Subject: [PATCH] Handle groups in C# JsonParser (#16970) Fixes https://github.com/protocolbuffers/protobuf/issues/16968 Closes #16970 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16970 from CommonGuy:main b008e189348ea42659b52874a5aaad8857d696ab PiperOrigin-RevId: 638578696 --- csharp/src/Google.Protobuf.Test/JsonParserTest.cs | 10 ++++++++++ csharp/src/Google.Protobuf/JsonParser.cs | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs index ef6d63dbdc08e..3df906257e16d 100644 --- a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs @@ -995,6 +995,16 @@ public void Proto2_DefaultValuesPreserved() Assert.AreEqual(0, parsed.FieldName13); } + [Test] + public void Proto2_Group() + { + string json = "{ \"data\": { \"groupInt32\": 2, \"groupUint32\": 3 } }"; + var parsed = TestAllTypesProto2.Parser.ParseJson(json); + Assert.True(parsed.HasData); + Assert.AreEqual(2, parsed.Data.GroupInt32); + Assert.AreEqual(3, parsed.Data.GroupUint32); + } + [Test] [TestCase("5")] [TestCase("\"text\"")] diff --git a/csharp/src/Google.Protobuf/JsonParser.cs b/csharp/src/Google.Protobuf/JsonParser.cs index 1a0da4a39236f..fcf859daba096 100644 --- a/csharp/src/Google.Protobuf/JsonParser.cs +++ b/csharp/src/Google.Protobuf/JsonParser.cs @@ -330,7 +330,7 @@ private bool TryParseSingleValue(FieldDescriptor field, JsonTokenizer tokenizer, } var fieldType = field.FieldType; - if (fieldType == FieldType.Message) + if (fieldType == FieldType.Message || fieldType == FieldType.Group) { // Parse wrapper types as their constituent types. // TODO: What does this mean for null?