Skip to content

Commit 898dd4a

Browse files
jtorvaldMichaël Catanzariti
authored andcommitted
Fix NullReference in Dictionary + tests Issue #31 (#33)
1 parent 00dacf7 commit 898dd4a

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Dahomey.Cbor.Tests/Issues/Issue0031.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ public class ObjectWithNullLists
1515
public List<string> Items { get; set; }
1616
}
1717

18-
[Fact]
18+
public class ObjectWithNullDictionary
19+
{
20+
public Dictionary<string, string> Items { get; set; }
21+
}
22+
23+
[Fact]
1924
public void TestNullReferenceList()
2025
{
2126

@@ -33,7 +38,7 @@ public void TestNullReferenceList()
3338

3439
}
3540

36-
[Fact]
41+
[Fact]
3742
public void TestNullReferenceArray()
3843
{
3944

@@ -50,5 +55,22 @@ public void TestNullReferenceArray()
5055
Assert.Equal("A1654279746573F6", hexResult);
5156
}
5257

53-
}
58+
59+
[Fact]
60+
public void TestNullReferenceDictionary()
61+
{
62+
var obj = new ObjectWithNullDictionary();
63+
64+
string hexResult = null;
65+
66+
var ex = Record.Exception(() => {
67+
hexResult = Helper.Write(obj);
68+
});
69+
70+
Assert.NotEmpty(hexResult);
71+
Assert.Null(ex);
72+
Assert.Equal("A1654974656D73F6", hexResult);
73+
74+
}
75+
}
5476
}

src/Dahomey.Cbor/Serialization/Converters/AbstractDictionaryConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public override TC Read(ref CborReader reader)
4545

4646
public override void Write(ref CborWriter writer, TC value)
4747
{
48+
if (value == null)
49+
{
50+
writer.WriteNull();
51+
return;
52+
}
4853
WriterContext context = new WriterContext
4954
{
5055
count = value.Count,

0 commit comments

Comments
 (0)