Skip to content

Commit 7481f95

Browse files
authored
Fixes Issue #242. Exception expanding dictionaries in complex types. (#243)
1 parent 3a314ae commit 7481f95

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

AutoMapper.AspNetCore.OData.EFCore/TypeExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,18 @@ public static Type GetUnderlyingElementType(this Type type)
117117

118118
if (genericTypeDefinition == typeof(IGrouping<,>))
119119
return genericArguments[1];
120-
else if (typeof(IDictionary<,>).IsAssignableFrom(genericTypeDefinition))
120+
else if (IsGenericDictionaryType())
121121
return typeof(KeyValuePair<,>).MakeGenericType(genericArguments[0], genericArguments[1]);
122122
else if (genericArguments.Length == 1)
123123
return genericArguments[0];
124124
else
125125
throw new ArgumentException(nameof(type));
126+
127+
bool IsGenericDictionaryType()
128+
{
129+
return (typeof(IDictionary<,>).IsAssignableFrom(genericTypeDefinition))
130+
|| (type.GetInterface(typeof(System.Collections.IDictionary).FullName ?? "") != null && genericArguments.Length == 2);
131+
}
126132
}
127133

128134
public static Type GetUnderlyingElementType(this Expression expression)

AutoMapper.OData.EFCore.Tests/AirVinylModel/VinylRecordModel.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,58 @@ public IDictionary<string, VinylLinkModel> Links {
4949

5050
return _links;
5151
}
52-
}
52+
}
53+
54+
private Dictionary<string, VinylLinkModel> _moreLinks;
55+
public Dictionary<string, VinylLinkModel> MoreLinks
56+
{
57+
get
58+
{
59+
if (_moreLinks is null)
60+
{
61+
_moreLinks = new Dictionary<string, VinylLinkModel>()
62+
{
63+
{ "buyingLink", new VinylLinkModel { Href = $"http://test/buy/{VinylRecordId}" } },
64+
{ "reviewLink", new VinylLinkModel { Href = $"http://test/review/{VinylRecordId}" } }
65+
};
66+
}
67+
68+
return _moreLinks;
69+
}
70+
}
71+
72+
private SortedDictionary<string, VinylLinkModel> _extraLinks;
73+
public SortedDictionary<string, VinylLinkModel> ExtraLinks
74+
{
75+
get
76+
{
77+
if (_extraLinks is null)
78+
{
79+
_extraLinks = new SortedDictionary<string, VinylLinkModel>()
80+
{
81+
{ "buyingLink", new VinylLinkModel { Href = $"http://test/buy/{VinylRecordId}" } },
82+
{ "reviewLink", new VinylLinkModel { Href = $"http://test/review/{VinylRecordId}" } }
83+
};
84+
}
85+
86+
return _extraLinks;
87+
}
88+
}
89+
90+
private System.Collections.Concurrent.ConcurrentDictionary<string, VinylLinkModel> _additionalLinks;
91+
public System.Collections.Concurrent.ConcurrentDictionary<string, VinylLinkModel> AdditionalLinks
92+
{
93+
get
94+
{
95+
if (_additionalLinks is null)
96+
{
97+
_additionalLinks = new System.Collections.Concurrent.ConcurrentDictionary<string, VinylLinkModel>();
98+
_additionalLinks.TryAdd("buyingLink", new VinylLinkModel { Href = $"http://test/buy/{VinylRecordId}" });
99+
_additionalLinks.TryAdd("reviewLink", new VinylLinkModel { Href = $"http://test/review/{VinylRecordId}" });
100+
}
101+
102+
return _additionalLinks;
103+
}
104+
}
53105
}
54106
}

AutoMapper.OData.EFCore.Tests/ExpansionTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ void Test(ICollection<VinylRecordModel> collection)
6565
}
6666
}
6767

68+
[Fact]
69+
public async Task ExpandingComplexTypesSupportsAllGenericDictionaries()
70+
{
71+
string query = "/vinylrecordmodel";
72+
Test(await GetAsync<VinylRecordModel, VinylRecord>(query));
73+
74+
void Test(ICollection<VinylRecordModel> collection)
75+
{
76+
Assert.True(collection.Count > 0);
77+
Assert.Contains(collection, vinyl => vinyl.Links.Count != 0);
78+
Assert.Contains(collection, vinyl => vinyl.MoreLinks.Count != 0);
79+
Assert.Contains(collection, vinyl => vinyl.ExtraLinks.Count != 0);
80+
Assert.Contains(collection, vinyl => vinyl.AdditionalLinks.Count != 0);
81+
}
82+
}
83+
6884
[Fact]
6985
public async Task GetRecordStoresExpandsComplexTypesByDefault()
7086
{

AutoMapper.OData.EFCore.Tests/Mappings/AirVinylMappings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public AirVinylMappings()
3131
.ForAllMembers(o => o.ExplicitExpansion());
3232
CreateMap<VinylRecord, VinylRecordModel>()
3333
.ForMember(dest => dest.Links, o => o.Ignore())
34+
.ForMember(dest => dest.MoreLinks, o => o.Ignore())
35+
.ForMember(dest => dest.ExtraLinks, o => o.Ignore())
36+
.ForMember(dest => dest.AdditionalLinks, o => o.Ignore())
3437
.ForAllMembers(o => o.ExplicitExpansion());
3538
}
3639
}

0 commit comments

Comments
 (0)