Skip to content

Commit 5954d85

Browse files
committed
fix: discriminate proxies in GetResourceContext
1 parent 9323342 commit 5954d85

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/JsonApiDotNetCore/Internal/ResourceGraph.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq.Expressions;
55
using JsonApiDotNetCore.Internal.Contracts;
66
using JsonApiDotNetCore.Models;
7+
using Castle.DynamicProxy;
78

89
namespace JsonApiDotNetCore.Internal
910
{
@@ -26,7 +27,9 @@ public ResourceContext GetResourceContext(string resourceName)
2627
=> Resources.SingleOrDefault(e => e.ResourceName == resourceName);
2728
/// <inheritdoc />
2829
public ResourceContext GetResourceContext(Type resourceType)
29-
=> Resources.SingleOrDefault(e => e.ResourceType == resourceType) ?? Resources.SingleOrDefault(e => e.ResourceType.IsAssignableFrom(resourceType));
30+
=> IsDynamicProxy(resourceType) ?
31+
Resources.SingleOrDefault(e => e.ResourceType == resourceType.BaseType) :
32+
Resources.SingleOrDefault(e => e.ResourceType == resourceType);
3033
/// <inheritdoc />
3134
public ResourceContext GetResourceContext<TResource>() where TResource : class, IIdentifiable
3235
=> GetResourceContext(typeof(TResource));
@@ -125,6 +128,11 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec
125128
$"For example: 'article => article.Title' or 'article => new {{ article.Title, article.PageCount }}'.");
126129
}
127130

131+
private bool IsDynamicProxy(Type resourceType)
132+
{
133+
return typeof(IProxyTargetAccessor).IsAssignableFrom(resourceType);
134+
}
135+
128136
private static Expression RemoveConvert(Expression expression)
129137
=> expression is UnaryExpression unaryExpression
130138
&& unaryExpression.NodeType == ExpressionType.Convert

src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
<ItemGroup>
2424
<PackageReference Include="Ben.Demystifier" Version="0.1.6" />
25+
<PackageReference Include="Castle.Core" Version="4.4.1" />
2526
<PackageReference Include="Humanizer" Version="2.7.9" />
2627
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="$(EFCoreVersion)" />
2728
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />

test/UnitTests/Internal/ResourceGraphBuilder_Tests.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
21
using JsonApiDotNetCore.Builders;
32
using JsonApiDotNetCore.Configuration;
43
using JsonApiDotNetCore.Internal;
54
using JsonApiDotNetCore.Models;
65
using Microsoft.EntityFrameworkCore;
76
using Microsoft.Extensions.Logging;
87
using Microsoft.Extensions.Logging.Abstractions;
8+
using Castle.DynamicProxy;
99
using Xunit;
1010

1111
namespace UnitTests.Internal
@@ -50,16 +50,18 @@ public void GetResourceContext_Yields_Right_Type_For_Proxy()
5050
var resourceGraphBuilder = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance);
5151
resourceGraphBuilder.AddResource<Bar>();
5252
var resourceGraph = (ResourceGraph)resourceGraphBuilder.Build();
53+
var proxyGenerator = new ProxyGenerator();
5354

5455
// Act
55-
var result = resourceGraph.GetResourceContext(typeof(DummyProxy));
56+
var proxy = proxyGenerator.CreateClassProxy<Bar>();
57+
var result = resourceGraph.GetResourceContext(proxy.GetType());
5658

5759
// Assert
5860
Assert.Equal(typeof(Bar), result.ResourceType);
5961
}
6062

6163
[Fact]
62-
public void GetResourceContext_Yields_Right_Type_For_IIdentifiable()
64+
public void GetResourceContext_Yields_Right_Type_For_Identifiable()
6365
{
6466
// Arrange
6567
var resourceGraphBuilder = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance);
@@ -80,10 +82,7 @@ private class TestContext : DbContext
8082
public DbSet<Foo> Foos { get; set; }
8183
}
8284

83-
private class Bar : Identifiable { }
84-
85-
// Used to simulate a lazy loading proxy
86-
private class DummyProxy : Bar { }
85+
public class Bar : Identifiable { }
8786

8887
}
8988

0 commit comments

Comments
 (0)