Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion AutoMapper.AspNetCore.OData.EFCore/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ public static Type GetUnderlyingElementType(this Type type)

if (genericTypeDefinition == typeof(IGrouping<,>))
return genericArguments[1];
else if (typeof(IDictionary<,>).IsAssignableFrom(genericTypeDefinition))
else if (IsGenericDictionaryType())
return typeof(KeyValuePair<,>).MakeGenericType(genericArguments[0], genericArguments[1]);
else if (genericArguments.Length == 1)
return genericArguments[0];
else
throw new ArgumentException(nameof(type));

bool IsGenericDictionaryType()
{
return (typeof(IDictionary<,>).IsAssignableFrom(genericTypeDefinition))
|| (type.GetInterface(typeof(System.Collections.IDictionary).FullName ?? "") != null && genericArguments.Length == 2);
}
}

public static Type GetUnderlyingElementType(this Expression expression)
Expand Down
54 changes: 53 additions & 1 deletion AutoMapper.OData.EFCore.Tests/AirVinylModel/VinylRecordModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,58 @@ public IDictionary<string, VinylLinkModel> Links {

return _links;
}
}
}

private Dictionary<string, VinylLinkModel> _moreLinks;
public Dictionary<string, VinylLinkModel> MoreLinks
{
get
{
if (_moreLinks is null)
{
_moreLinks = new Dictionary<string, VinylLinkModel>()
{
{ "buyingLink", new VinylLinkModel { Href = $"http://test/buy/{VinylRecordId}" } },
{ "reviewLink", new VinylLinkModel { Href = $"http://test/review/{VinylRecordId}" } }
};
}

return _moreLinks;
}
}

private SortedDictionary<string, VinylLinkModel> _extraLinks;
public SortedDictionary<string, VinylLinkModel> ExtraLinks
{
get
{
if (_extraLinks is null)
{
_extraLinks = new SortedDictionary<string, VinylLinkModel>()
{
{ "buyingLink", new VinylLinkModel { Href = $"http://test/buy/{VinylRecordId}" } },
{ "reviewLink", new VinylLinkModel { Href = $"http://test/review/{VinylRecordId}" } }
};
}

return _extraLinks;
}
}

private System.Collections.Concurrent.ConcurrentDictionary<string, VinylLinkModel> _additionalLinks;
public System.Collections.Concurrent.ConcurrentDictionary<string, VinylLinkModel> AdditionalLinks
{
get
{
if (_additionalLinks is null)
{
_additionalLinks = new System.Collections.Concurrent.ConcurrentDictionary<string, VinylLinkModel>();
_additionalLinks.TryAdd("buyingLink", new VinylLinkModel { Href = $"http://test/buy/{VinylRecordId}" });
_additionalLinks.TryAdd("reviewLink", new VinylLinkModel { Href = $"http://test/review/{VinylRecordId}" });
}

return _additionalLinks;
}
}
}
}
16 changes: 16 additions & 0 deletions AutoMapper.OData.EFCore.Tests/ExpansionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ void Test(ICollection<VinylRecordModel> collection)
}
}

[Fact]
public async Task ExpandingComplexTypesSupportsAllGenericDictionaries()
{
string query = "/vinylrecordmodel";
Test(await GetAsync<VinylRecordModel, VinylRecord>(query));

void Test(ICollection<VinylRecordModel> collection)
{
Assert.True(collection.Count > 0);
Assert.Contains(collection, vinyl => vinyl.Links.Count != 0);
Assert.Contains(collection, vinyl => vinyl.MoreLinks.Count != 0);
Assert.Contains(collection, vinyl => vinyl.ExtraLinks.Count != 0);
Assert.Contains(collection, vinyl => vinyl.AdditionalLinks.Count != 0);
}
}

[Fact]
public async Task GetRecordStoresExpandsComplexTypesByDefault()
{
Expand Down
3 changes: 3 additions & 0 deletions AutoMapper.OData.EFCore.Tests/Mappings/AirVinylMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public AirVinylMappings()
.ForAllMembers(o => o.ExplicitExpansion());
CreateMap<VinylRecord, VinylRecordModel>()
.ForMember(dest => dest.Links, o => o.Ignore())
.ForMember(dest => dest.MoreLinks, o => o.Ignore())
.ForMember(dest => dest.ExtraLinks, o => o.Ignore())
.ForMember(dest => dest.AdditionalLinks, o => o.Ignore())
.ForAllMembers(o => o.ExplicitExpansion());
}
}
Expand Down