Skip to content

Fixes Issue #242. Exception expanding dictionaries in complex types. #243

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 1 commit into from
May 31, 2025
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