Skip to content

Commit 453d4fc

Browse files
committed
fix tests
1 parent 529a562 commit 453d4fc

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

src/JsonApiDotNetCore/Extensions/IServiceCollectionExtensions.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Reflection;
56
using JsonApiDotNetCore.Builders;
67
using JsonApiDotNetCore.Configuration;
78
using JsonApiDotNetCore.Data;
@@ -186,10 +187,12 @@ public static void SerializeAsJsonApi(this MvcOptions options, JsonApiOptions js
186187
}
187188

188189
/// <summary>
190+
/// Adds all required registrations for the service to the container
189191
/// </summary>
192+
/// <exception cref="JsonApiSetupException"/>
190193
public static IServiceCollection AddResourceService<T>(this IServiceCollection services)
191194
{
192-
var typeImplemenetsAnExpectedInterface = false;
195+
var typeImplementsAnExpectedInterface = false;
193196

194197
var serviceImplementationType = typeof(T);
195198

@@ -200,19 +203,25 @@ public static IServiceCollection AddResourceService<T>(this IServiceCollection s
200203
{
201204
foreach(var openGenericType in ServiceDiscoveryFacade.ServiceInterfaces)
202205
{
203-
var concreteGenericType = openGenericType.GetGenericArguments().Length == 1
206+
// A shorthand interface is one where the id type is ommitted
207+
// e.g. IResourceService<T> is the shorthand for IResourceService<T, TId>
208+
var isShorthandInterface = (openGenericType.GetTypeInfo().GenericTypeParameters.Length == 1);
209+
if(isShorthandInterface && resourceDescriptor.IdType != typeof(int))
210+
continue; // we can't create a shorthand for id types other than int
211+
212+
var concreteGenericType = isShorthandInterface
204213
? openGenericType.MakeGenericType(resourceDescriptor.ResourceType)
205214
: openGenericType.MakeGenericType(resourceDescriptor.ResourceType, resourceDescriptor.IdType);
206215

207216
if(concreteGenericType.IsAssignableFrom(serviceImplementationType)) {
208-
services.AddScoped(serviceImplementationType, serviceImplementationType);
209-
typeImplemenetsAnExpectedInterface = true;
217+
services.AddScoped(concreteGenericType, serviceImplementationType);
218+
typeImplementsAnExpectedInterface = true;
210219
}
211220
}
212221
}
213222

214-
if(typeImplemenetsAnExpectedInterface == false)
215-
throw new JsonApiSetupException($"{typeImplemenetsAnExpectedInterface} does not implement any of the expected JsonApiDotNetCore interfaces.");
223+
if(typeImplementsAnExpectedInterface == false)
224+
throw new JsonApiSetupException($"{serviceImplementationType} does not implement any of the expected JsonApiDotNetCore interfaces.");
216225

217226
return services;
218227
}
@@ -225,8 +234,8 @@ private static HashSet<ResourceDescriptor> GetResourceTypesFromServiceImplementa
225234
{
226235
if(i.IsGenericType)
227236
{
228-
var firstGenericArgument = i.GetGenericTypeDefinition().GetGenericArguments().FirstOrDefault();
229-
if(TypeLocator.TryGetResourceDescriptor(firstGenericArgument, out var resourceDescriptor) == false)
237+
var firstGenericArgument = i.GenericTypeArguments.FirstOrDefault();
238+
if(TypeLocator.TryGetResourceDescriptor(firstGenericArgument, out var resourceDescriptor) == true)
230239
{
231240
resourceDecriptors.Add(resourceDescriptor);
232241
}

src/JsonApiDotNetCore/Graph/ServiceDiscoveryFacade.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public class ServiceDiscoveryFacade
1717
{
1818
internal static HashSet<Type> ServiceInterfaces = new HashSet<Type> {
1919
typeof(IResourceService<>),
20-
typeof(IResourceService<,>),
20+
typeof(IResourceService<,>),
21+
typeof(IResourceCmdService<>),
22+
typeof(IResourceCmdService<,>),
23+
typeof(IResourceQueryService<>),
24+
typeof(IResourceQueryService<,>),
2125
typeof(ICreateService<>),
2226
typeof(ICreateService<,>),
2327
typeof(IGetAllService<>),
@@ -26,6 +30,8 @@ public class ServiceDiscoveryFacade
2630
typeof(IGetByIdService<,>),
2731
typeof(IGetRelationshipService<>),
2832
typeof(IGetRelationshipService<,>),
33+
typeof(IGetRelationshipsService<>),
34+
typeof(IGetRelationshipsService<,>),
2935
typeof(IUpdateService<>),
3036
typeof(IUpdateService<,>),
3137
typeof(IDeleteService<>),

test/UnitTests/Extensions/IServiceCollectionExtensionsTests.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public void AddResourceService_Registers_All_Shorthand_Service_Interfaces()
6767

6868
// assert
6969
var provider = services.BuildServiceProvider();
70-
Assert.IsType<IntResourceService>(typeof(IResourceService<IntResource>));
71-
Assert.IsType<IntResourceService>(typeof(IResourceCmdService<IntResource>));
72-
Assert.IsType<IntResourceService>(typeof(IResourceQueryService<IntResource>));
73-
Assert.IsType<IntResourceService>(typeof(IGetAllService<IntResource>));
74-
Assert.IsType<IntResourceService>(typeof(IGetByIdService<IntResource>));
75-
Assert.IsType<IntResourceService>(typeof(IGetRelationshipService<IntResource>));
76-
Assert.IsType<IntResourceService>(typeof(IGetRelationshipsService<IntResource>));
77-
Assert.IsType<IntResourceService>(typeof(ICreateService<IntResource>));
78-
Assert.IsType<IntResourceService>(typeof(IUpdateService<IntResource>));
79-
Assert.IsType<IntResourceService>(typeof(IDeleteService<IntResource>));
70+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IResourceService<IntResource>)));
71+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IResourceCmdService<IntResource>)));
72+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IResourceQueryService<IntResource>)));
73+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IGetAllService<IntResource>)));
74+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IGetByIdService<IntResource>)));
75+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IGetRelationshipService<IntResource>)));
76+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IGetRelationshipsService<IntResource>)));
77+
Assert.IsType<IntResourceService>(provider.GetService(typeof(ICreateService<IntResource>)));
78+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IUpdateService<IntResource>)));
79+
Assert.IsType<IntResourceService>(provider.GetService(typeof(IDeleteService<IntResource>)));
8080
}
8181

8282
[Fact]
@@ -90,16 +90,16 @@ public void AddResourceService_Registers_All_LongForm_Service_Interfaces()
9090

9191
// assert
9292
var provider = services.BuildServiceProvider();
93-
Assert.IsType<GuidResourceService>(typeof(IResourceService<GuidResource, Guid>));
94-
Assert.IsType<GuidResourceService>(typeof(IResourceCmdService<GuidResource, Guid>));
95-
Assert.IsType<GuidResourceService>(typeof(IResourceQueryService<GuidResource, Guid>));
96-
Assert.IsType<GuidResourceService>(typeof(IGetAllService<GuidResource, Guid>));
97-
Assert.IsType<GuidResourceService>(typeof(IGetByIdService<GuidResource, Guid>));
98-
Assert.IsType<GuidResourceService>(typeof(IGetRelationshipService<GuidResource, Guid>));
99-
Assert.IsType<GuidResourceService>(typeof(IGetRelationshipsService<GuidResource, Guid>));
100-
Assert.IsType<GuidResourceService>(typeof(ICreateService<GuidResource, Guid>));
101-
Assert.IsType<GuidResourceService>(typeof(IUpdateService<GuidResource, Guid>));
102-
Assert.IsType<GuidResourceService>(typeof(IDeleteService<GuidResource, Guid>));
93+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IResourceService<GuidResource, Guid>)));
94+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IResourceCmdService<GuidResource, Guid>)));
95+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IResourceQueryService<GuidResource, Guid>)));
96+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IGetAllService<GuidResource, Guid>)));
97+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IGetByIdService<GuidResource, Guid>)));
98+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IGetRelationshipService<GuidResource, Guid>)));
99+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IGetRelationshipsService<GuidResource, Guid>)));
100+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(ICreateService<GuidResource, Guid>)));
101+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IUpdateService<GuidResource, Guid>)));
102+
Assert.IsType<GuidResourceService>(provider.GetService(typeof(IDeleteService<GuidResource, Guid>)));
103103
}
104104

105105
[Fact]
@@ -109,7 +109,7 @@ public void AddResourceService_Throws_If_Type_Does_Not_Implement_Any_Interfaces(
109109
var services = new ServiceCollection();
110110

111111
// act, assert
112-
Assert.Throws<JsonApiException>(() => services.AddResourceService<int>());
112+
Assert.Throws<JsonApiSetupException>(() => services.AddResourceService<int>());
113113
}
114114

115115
private class IntResource : Identifiable { }

0 commit comments

Comments
 (0)