Skip to content

fix: support for lazy loading proxies #793

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 4 commits into from
Jul 28, 2020
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
10 changes: 8 additions & 2 deletions src/JsonApiDotNetCore/Internal/ResourceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace JsonApiDotNetCore.Internal
public class ResourceGraph : IResourceGraph
{
private List<ResourceContext> Resources { get; }
private static readonly Type proxyTargetAccessorType = Type.GetType("Castle.DynamicProxy.IProxyTargetAccessor, Castle.Core");

public ResourceGraph(List<ResourceContext> resources)
{
Expand All @@ -26,7 +27,9 @@ public ResourceContext GetResourceContext(string resourceName)
=> Resources.SingleOrDefault(e => e.ResourceName == resourceName);
/// <inheritdoc />
public ResourceContext GetResourceContext(Type resourceType)
=> Resources.SingleOrDefault(e => e.ResourceType == resourceType);
=> IsLazyLoadingProxyForResourceType(resourceType) ?
Resources.SingleOrDefault(e => e.ResourceType == resourceType.BaseType) :
Resources.SingleOrDefault(e => e.ResourceType == resourceType);
/// <inheritdoc />
public ResourceContext GetResourceContext<TResource>() where TResource : class, IIdentifiable
=> GetResourceContext(typeof(TResource));
Expand Down Expand Up @@ -121,10 +124,13 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec
}

throw new ArgumentException(
$"The expression '{selector}' should select a single property or select multiple properties into an anonymous type. " +
$"The expression '{selector}' should select a single property or select multiple properties into an anonymous type. " +
$"For example: 'article => article.Title' or 'article => new {{ article.Title, article.PageCount }}'.");
}

private bool IsLazyLoadingProxyForResourceType(Type resourceType) =>
proxyTargetAccessorType?.IsAssignableFrom(resourceType) ?? false;

private static Expression RemoveConvert(Expression expression)
=> expression is UnaryExpression unaryExpression
&& unaryExpression.NodeType == ExpressionType.Convert
Expand Down
37 changes: 37 additions & 0 deletions test/UnitTests/Internal/ResourceGraphBuilder_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Castle.DynamicProxy;
using Xunit;

namespace UnitTests.Internal
Expand Down Expand Up @@ -41,12 +43,47 @@ public void Adding_DbContext_Members_That_Do_Not_Implement_IIdentifiable_Logs_Wa
Assert.Equal("Entity 'UnitTests.Internal.ResourceGraphBuilder_Tests+TestContext' does not implement 'IIdentifiable'.", loggerFactory.Logger.Messages[0].Text);
}

[Fact]
public void GetResourceContext_Yields_Right_Type_For_LazyLoadingProxy()
{
// Arrange
var resourceGraphBuilder = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance);
resourceGraphBuilder.AddResource<Bar>();
var resourceGraph = (ResourceGraph)resourceGraphBuilder.Build();
var proxyGenerator = new ProxyGenerator();

// Act
var proxy = proxyGenerator.CreateClassProxy<Bar>();
var result = resourceGraph.GetResourceContext(proxy.GetType());

// Assert
Assert.Equal(typeof(Bar), result.ResourceType);
}

[Fact]
public void GetResourceContext_Yields_Right_Type_For_Identifiable()
{
// Arrange
var resourceGraphBuilder = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance);
resourceGraphBuilder.AddResource<Bar>();
var resourceGraph = (ResourceGraph)resourceGraphBuilder.Build();

// Act
var result = resourceGraph.GetResourceContext(typeof(Bar));

// Assert
Assert.Equal(typeof(Bar), result.ResourceType);
}

private class Foo { }

private class TestContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
}

public class Bar : Identifiable { }

}

}
1 change: 1 addition & 0 deletions test/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="Bogus" Version="$(BogusVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="$(EFCoreVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="$(EFCoreVersion)" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitVersion)" />
Expand Down