Skip to content

De-Couple deserializer from GenericProcessorFactory #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(Deserializer): properly convert collection type when setting it o…
…n the model
  • Loading branch information
jaredcnance committed Jun 9, 2018
commit 31a4b76dfa07cca313f69c3aa97cc601498869a5
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public static Type GetElementType(this IEnumerable enumerable)
/// <summary>
/// Creates a List{TInterface} where TInterface is the generic for type specified by t
/// </summary>
public static IEnumerable<TInterface> GetEmptyCollection<TInterface>(this Type t)
public static IEnumerable GetEmptyCollection(this Type t)
{
if (t == null) throw new ArgumentNullException(nameof(t));

var listType = typeof(List<>).MakeGenericType(t);
var list = (IEnumerable<TInterface>)Activator.CreateInstance(listType);
var list = (IEnumerable)Activator.CreateInstance(listType);
return list;
}

Expand Down
8 changes: 8 additions & 0 deletions src/JsonApiDotNetCore/Internal/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ namespace JsonApiDotNetCore.Internal
{
public static class TypeHelper
{
public static IList ConvertCollection(IEnumerable<object> collection, Type targetType)
{
var list = Activator.CreateInstance(typeof(List<>).MakeGenericType(targetType)) as IList;
foreach(var item in collection)
list.Add(ConvertType(item, targetType));
return list;
}

public static object ConvertType(object value, Type type)
{
if (value == null)
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Models/HasManyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public override void SetValue(object entity, object newValue)
.GetType()
.GetProperty(InternalRelationshipName);

propertyInfo.SetValue(entity, newValue);
propertyInfo.SetValue(entity, newValue);
}
}
}
10 changes: 6 additions & 4 deletions src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Models.Operations;
using JsonApiDotNetCore.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using JsonApiDotNetCore.Extensions;

namespace JsonApiDotNetCore.Serialization
{
Expand Down Expand Up @@ -246,16 +246,18 @@ private object SetHasManyRelationship(object entity,

if (data == null) return entity;

var resourceRelationships = attr.Type.GetEmptyCollection<IIdentifiable>();

var relationshipShells = relationshipData.ManyData.Select(r =>
{
var instance = attr.Type.New<IIdentifiable>();
instance.StringId = r.Id;
return instance;
});

attr.SetValue(entity, relationshipShells);
var convertedCollection = TypeHelper.ConvertCollection(relationshipShells, attr.Type);

// var convertedCollection = TypeHelper.ConvertCollection(relationshipShells, attr.Type);

attr.SetValue(entity, convertedCollection);
}

return entity;
Expand Down
6 changes: 3 additions & 3 deletions test/UnitTests/Extensions/TypeExtensions_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Models;
using Xunit;
using JsonApiDotNetCore.Extensions;
using System.Collections.Generic;

namespace UnitTests.Extensions
{
Expand All @@ -14,7 +14,7 @@ public void GetCollection_Creates_List_If_T_Implements_Interface()
var type = typeof(Model);

// act
var collection = type.GetEmptyCollection<IIdentifiable>();
var collection = type.GetEmptyCollection();

// assert
Assert.NotNull(collection);
Expand Down