From 9e4f354f14775061ed098c896170d3a2d01a3895 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Mon, 4 Jan 2016 14:03:01 +0000 Subject: Prohibit null values in map fields On deserialization, missing values for message types are replaced with a "default" message. --- .../Google.Protobuf.Test/GeneratedMessageTest.cs | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs') diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs index 1163f524..cda7f885 100644 --- a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs +++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs @@ -221,7 +221,7 @@ namespace Google.Protobuf }, MapInt32ForeignMessage = { { 0, new ForeignMessage { C = 10 } }, - { 5, null }, + { 5, new ForeignMessage() }, }, MapInt32Enum = { { 1, MapEnum.MAP_ENUM_BAR }, @@ -268,6 +268,40 @@ namespace Google.Protobuf Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]); } + [Test] + public void MapWithOnlyKey_PrimitiveValue() + { + // Hand-craft the stream to contain a single entry with just a key. + var memoryStream = new MemoryStream(); + var output = new CodedOutputStream(memoryStream); + output.WriteTag(TestMap.MapInt32DoubleFieldNumber, WireFormat.WireType.LengthDelimited); + int key = 10; + output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key)); + output.WriteTag(1, WireFormat.WireType.Varint); + output.WriteInt32(key); + output.Flush(); + + var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); + Assert.AreEqual(0.0, parsed.MapInt32Double[key]); + } + + [Test] + public void MapWithOnlyKey_MessageValue() + { + // Hand-craft the stream to contain a single entry with just a key. + var memoryStream = new MemoryStream(); + var output = new CodedOutputStream(memoryStream); + output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited); + int key = 10; + output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key)); + output.WriteTag(1, WireFormat.WireType.Varint); + output.WriteInt32(key); + output.Flush(); + + var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); + Assert.AreEqual(new ForeignMessage(), parsed.MapInt32ForeignMessage[key]); + } + [Test] public void MapIgnoresExtraFieldsWithinEntryMessages() { -- cgit v1.2.3