Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ea9a17

Browse files
Merge pull request #377 from json-api-dotnet/feat/#241-2
fix(#241): TypeLocator bug -- add tests
2 parents d5ee796 + 0c27ed7 commit 5ea9a17

File tree

2 files changed

+117
-6
lines changed

2 files changed

+117
-6
lines changed

‎src/JsonApiDotNetCore/Graph/TypeLocator.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ private static Type[] GetAssemblyTypes(Assembly assembly)
4545
return types;
4646
}
4747

48-
4948
/// <summary>
5049
/// Get all implementations of <see cref="IIdentifiable"/>. in the assembly
5150
/// </summary>
@@ -80,15 +79,28 @@ public static List<ResourceDescriptor> GetIdentifableTypes(Assembly assembly)
8079
/// </example>
8180
public static (Type implementation, Type registrationInterface) GetGenericInterfaceImplementation(Assembly assembly, Type openGenericInterfaceType, params Type[] genericInterfaceArguments)
8281
{
82+
if(assembly == null) throw new ArgumentNullException(nameof(assembly));
83+
if(openGenericInterfaceType == null) throw new ArgumentNullException(nameof(openGenericInterfaceType));
84+
if(genericInterfaceArguments == null) throw new ArgumentNullException(nameof(genericInterfaceArguments));
85+
if(genericInterfaceArguments.Length == 0) throw new ArgumentException("No arguments supplied for the generic interface.", nameof(genericInterfaceArguments));
86+
if(openGenericInterfaceType.IsGenericType == false) throw new ArgumentException("Requested type is not a generic type.", nameof(openGenericInterfaceType));
87+
8388
foreach (var type in assembly.GetTypes())
8489
{
8590
var interfaces = type.GetInterfaces();
8691
foreach (var interfaceType in interfaces)
87-
if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == openGenericInterfaceType)
88-
return (
89-
type,
90-
interfaceType.MakeGenericType(genericInterfaceArguments)
91-
);
92+
{
93+
if (interfaceType.IsGenericType)
94+
{
95+
var genericTypeDefinition = interfaceType.GetGenericTypeDefinition();
96+
if(genericTypeDefinition == openGenericInterfaceType.GetGenericTypeDefinition()) {
97+
return (
98+
type,
99+
genericTypeDefinition.MakeGenericType(genericInterfaceArguments)
100+
);
101+
}
102+
}
103+
}
92104
}
93105

94106
return (null, null);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using JsonApiDotNetCore.Graph;
3+
using JsonApiDotNetCore.Models;
4+
using Xunit;
5+
6+
namespace UnitTests.Internal
7+
{
8+
public class TypeLocator_Tests
9+
{
10+
[Fact]
11+
public void GetGenericInterfaceImplementation_Gets_Implementation()
12+
{
13+
// arrange
14+
var assembly = GetType().Assembly;
15+
var openGeneric = typeof(IGenericInterface<>);
16+
var genericArg = typeof(int);
17+
18+
var expectedImplementation = typeof(Implementation);
19+
var expectedInterface = typeof(IGenericInterface<int>);
20+
21+
// act
22+
var result = TypeLocator.GetGenericInterfaceImplementation(
23+
assembly,
24+
openGeneric,
25+
genericArg
26+
);
27+
28+
// assert
29+
Assert.NotNull(result);
30+
Assert.Equal(expectedImplementation, result.implementation);
31+
Assert.Equal(expectedInterface, result.registrationInterface);
32+
}
33+
34+
[Fact]
35+
public void GetDerivedGenericTypes_Gets_Implementation()
36+
{
37+
// arrange
38+
var assembly = GetType().Assembly;
39+
var openGeneric = typeof(BaseType<>);
40+
var genericArg = typeof(int);
41+
42+
var expectedImplementation = typeof(DerivedType);
43+
44+
// act
45+
var results = TypeLocator.GetDerivedGenericTypes(
46+
assembly,
47+
openGeneric,
48+
genericArg
49+
);
50+
51+
// assert
52+
Assert.NotNull(results);
53+
var result = Assert.Single(results);
54+
Assert.Equal(expectedImplementation, result);
55+
}
56+
57+
[Fact]
58+
public void GetIdType_Correctly_Identifies_JsonApiResource()
59+
{
60+
// arrange
61+
var type = typeof(Model);
62+
var exextedIdType = typeof(int);
63+
64+
// act
65+
var result = TypeLocator.GetIdType(type);
66+
67+
// assert
68+
Assert.NotNull(result);
69+
Assert.True(result.isJsonApiResource);
70+
Assert.Equal(exextedIdType, result.idType);
71+
}
72+
73+
[Fact]
74+
public void GetIdType_Correctly_Identifies_NonJsonApiResource()
75+
{
76+
// arrange
77+
var type = typeof(DerivedType);
78+
Type exextedIdType = null;
79+
80+
// act
81+
var result = TypeLocator.GetIdType(type);
82+
83+
// assert
84+
Assert.NotNull(result);
85+
Assert.False(result.isJsonApiResource);
86+
Assert.Equal(exextedIdType, result.idType);
87+
}
88+
}
89+
90+
91+
public interface IGenericInterface<T> { }
92+
public class Implementation : IGenericInterface<int> { }
93+
94+
95+
public class BaseType<T> { }
96+
public class DerivedType : BaseType<int> { }
97+
98+
public class Model : Identifiable { }
99+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /