Skip to content

Commit 7288de2

Browse files
committed
fix(Deserializer): properly convert collection type when setting it on the model
1 parent 91b5d4b commit 7288de2

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

src/JsonApiDotNetCore/Extensions/TypeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public static Type GetElementType(this IEnumerable enumerable)
3535
/// <summary>
3636
/// Creates a List{TInterface} where TInterface is the generic for type specified by t
3737
/// </summary>
38-
public static IEnumerable<TInterface> GetEmptyCollection<TInterface>(this Type t)
38+
public static IEnumerable GetEmptyCollection(this Type t)
3939
{
4040
if (t == null) throw new ArgumentNullException(nameof(t));
4141

4242
var listType = typeof(List<>).MakeGenericType(t);
43-
var list = (IEnumerable<TInterface>)Activator.CreateInstance(listType);
43+
var list = (IEnumerable)Activator.CreateInstance(listType);
4444
return list;
4545
}
4646

src/JsonApiDotNetCore/Internal/TypeHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Reflection;
35

46
namespace JsonApiDotNetCore.Internal
57
{
68
public static class TypeHelper
79
{
10+
public static IList ConvertCollection(IEnumerable<object> collection, Type targetType)
11+
{
12+
var list = Activator.CreateInstance(typeof(List<>).MakeGenericType(targetType)) as IList;
13+
foreach(var item in collection)
14+
list.Add(ConvertType(item, targetType));
15+
return list;
16+
}
17+
818
public static object ConvertType(object value, Type type)
919
{
1020
if (value == null)

src/JsonApiDotNetCore/Models/HasManyAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public override void SetValue(object entity, object newValue)
1212
.GetType()
1313
.GetProperty(InternalRelationshipName);
1414

15-
propertyInfo.SetValue(entity, newValue);
15+
propertyInfo.SetValue(entity, newValue);
1616
}
1717
}
1818
}

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using JsonApiDotNetCore.Extensions;
56
using JsonApiDotNetCore.Internal;
67
using JsonApiDotNetCore.Internal.Generics;
78
using JsonApiDotNetCore.Models;
89
using JsonApiDotNetCore.Models.Operations;
910
using JsonApiDotNetCore.Services;
1011
using Newtonsoft.Json;
1112
using Newtonsoft.Json.Linq;
12-
using JsonApiDotNetCore.Extensions;
1313

1414
namespace JsonApiDotNetCore.Serialization
1515
{
@@ -246,16 +246,18 @@ private object SetHasManyRelationship(object entity,
246246

247247
if (data == null) return entity;
248248

249-
var resourceRelationships = attr.Type.GetEmptyCollection<IIdentifiable>();
250-
251249
var relationshipShells = relationshipData.ManyData.Select(r =>
252250
{
253251
var instance = attr.Type.New<IIdentifiable>();
254252
instance.StringId = r.Id;
255253
return instance;
256254
});
257255

258-
attr.SetValue(entity, relationshipShells);
256+
var convertedCollection = TypeHelper.ConvertCollection(relationshipShells, attr.Type);
257+
258+
// var convertedCollection = TypeHelper.ConvertCollection(relationshipShells, attr.Type);
259+
260+
attr.SetValue(entity, convertedCollection);
259261
}
260262

261263
return entity;

test/UnitTests/Extensions/TypeExtensions_Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using System.Collections.Generic;
2+
using JsonApiDotNetCore.Extensions;
13
using JsonApiDotNetCore.Models;
24
using Xunit;
3-
using JsonApiDotNetCore.Extensions;
4-
using System.Collections.Generic;
55

66
namespace UnitTests.Extensions
77
{
@@ -14,7 +14,7 @@ public void GetCollection_Creates_List_If_T_Implements_Interface()
1414
var type = typeof(Model);
1515

1616
// act
17-
var collection = type.GetEmptyCollection<IIdentifiable>();
17+
var collection = type.GetEmptyCollection();
1818

1919
// assert
2020
Assert.NotNull(collection);

0 commit comments

Comments
 (0)