Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 57a26a9

Browse files
committed
fix null property of generic collection
1 parent 07cd8c2 commit 57a26a9

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/ServiceStack.Text/CollectionExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ public static class CollectionExtensions
77
{
88
public static ICollection<T> CreateAndPopulate<T>(Type ofCollectionType, T[] withItems)
99
{
10-
if (ofCollectionType == null) return new List<T>(withItems);
10+
if (withItems == null)
11+
return null;
12+
13+
if (ofCollectionType == null)
14+
return new List<T>(withItems);
1115

1216
var genericType = ofCollectionType.FirstGenericType();
1317
var genericTypeDefinition = genericType != null
1418
? genericType.GetGenericTypeDefinition()
1519
: null;
1620
#if !XBOX
17-
if (genericTypeDefinition == typeof(HashSet<T>))
21+
if (genericTypeDefinition == typeof(HashSet<>))
1822
return new HashSet<T>(withItems);
1923
#endif
20-
if (genericTypeDefinition == typeof(LinkedList<T>))
24+
if (genericTypeDefinition == typeof(LinkedList<>))
2125
return new LinkedList<T>(withItems);
2226

2327
var collection = (ICollection<T>)ofCollectionType.CreateInstance();

tests/ServiceStack.Text.Tests/JsonTests/BasicPropertiesTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Runtime.Serialization;
45
using System.Text;
56
using NUnit.Framework;
67

@@ -172,5 +173,21 @@ static string DictStr(IDictionary d)
172173
foreach (var key in d.Keys) { sb.AppendLine(key + " = " + d[key]); }
173174
return sb.ToString();
174175
}
176+
177+
public class ModelWithHashSet
178+
{
179+
public HashSet<string> Set { get; set; }
180+
}
181+
182+
[Test]
183+
public void Can_deserialize_null_Nested_HashSet()
184+
{
185+
JsConfig.ThrowOnDeserializationError = true;
186+
string json = @"{""set"":null}";
187+
var o = json.FromJson<ModelWithHashSet>();
188+
Assert.That(o.Set, Is.Null);
189+
190+
JsConfig.Reset();
191+
}
175192
}
176193
}

0 commit comments

Comments
 (0)