Skip to content

Commit 19b9918

Browse files
author
Bart Koelman
committed
More types cleanup
1 parent 1fb69ae commit 19b9918

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

src/JsonApiDotNetCore/Extensions/TypeExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ internal static class TypeExtensions
1616
/// ((IList)myList).CopyToList(targetType).
1717
/// </code>
1818
/// </summary>
19-
public static IList CopyToList(this IEnumerable copyFrom, Type elementType)
19+
public static IList CopyToList(this IEnumerable copyFrom, Type elementType, Converter<object, object> elementConverter = null)
2020
{
2121
Type collectionType = typeof(List<>).MakeGenericType(elementType);
22+
23+
if (elementConverter != null)
24+
{
25+
var converted = copyFrom.Cast<object>().Select(element => elementConverter(element));
26+
return (IList) CopyToTypedCollection(converted, collectionType);
27+
}
28+
2229
return (IList)CopyToTypedCollection(copyFrom, collectionType);
2330
}
2431

src/JsonApiDotNetCore/Internal/Generics/RepositoryRelationshipUpdateHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ private async Task UpdateOneToManyAsync(IIdentifiable parent, RelationshipAttrib
7878
}
7979
else
8080
{
81+
var idType = TypeHelper.GetIdType(relationship.RightType);
82+
var typedIds = relationshipIds.CopyToList(idType, stringId => TypeHelper.ConvertType(stringId, idType));
83+
8184
// [1, 2, 3]
82-
var target = Expression.Constant(TypeHelper.ConvertListType(relationshipIds,
83-
TypeHelper.GetIdType(relationship.RightType)));
85+
var target = Expression.Constant(typedIds);
8486
// (Person p) => ...
8587
ParameterExpression parameter = Expression.Parameter(typeof(TRelatedResource));
8688
// (Person p) => p.Id

src/JsonApiDotNetCore/Internal/TypeHelper.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,6 @@ public static PropertyInfo ParseNavigationExpression<TResource>(Expression<Func<
111111
return (PropertyInfo)exp.Member;
112112
}
113113

114-
/// <summary>
115-
/// Convert collection of query string params to Collection of concrete Type
116-
/// </summary>
117-
/// <param name="values">Collection like ["10","20","30"]</param>
118-
/// <param name="type">Non array type. For e.g. int</param>
119-
/// <returns>Collection of concrete type</returns>
120-
public static IList ConvertListType(IEnumerable<string> values, Type type)
121-
{
122-
var list = CreateListFor(type);
123-
foreach (var value in values)
124-
{
125-
list.Add(ConvertType(value, type));
126-
}
127-
128-
return list;
129-
}
130-
131114
/// <summary>
132115
/// Creates an instance of the specified generic type
133116
/// </summary>
@@ -191,8 +174,7 @@ public static object CreateInstanceOfOpenType(Type openType, Type parameter, boo
191174
/// <param name="type">The target type</param>
192175
public static IList CreateListFor(Type type)
193176
{
194-
IList list = (IList)CreateInstanceOfOpenType(typeof(List<>), type);
195-
return list;
177+
return (IList)CreateInstanceOfOpenType(typeof(List<>), type);
196178
}
197179

198180
/// <summary>

0 commit comments

Comments
 (0)