Skip to content

Commit 581f4bd

Browse files
committedApr 12, 2017
fix(DasherizedRoutingConvention): Check for abstract subtype
1 parent 8a07949 commit 581f4bd

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed
 

‎src/JsonApiDotNetCore/Controllers/JsonApiControllerMixin.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace JsonApiDotNetCore.Controllers
44
{
5-
public class JsonApiControllerMixin : Controller
5+
public abstract class JsonApiControllerMixin : Controller
66
{
7-
public JsonApiControllerMixin()
7+
protected JsonApiControllerMixin()
88
{ }
99

1010
protected IActionResult UnprocessableEntity()

‎src/JsonApiDotNetCore/Internal/DasherizedRoutingConvention.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ public void Apply(ApplicationModel application)
2929
}
3030
}
3131

32-
private bool IsJsonApiController(ControllerModel controller)
32+
private bool IsJsonApiController(ControllerModel controller)
3333
{
34-
var controllerBaseType = controller.ControllerType.BaseType;
35-
if(!controllerBaseType.IsConstructedGenericType) return false;
36-
var genericTypeDefinition = controllerBaseType.GetGenericTypeDefinition();
37-
return (genericTypeDefinition == typeof(JsonApiController<,>) || genericTypeDefinition == typeof(JsonApiController<>));
34+
return controller.ControllerType.IsSubclassOf(typeof(JsonApiControllerMixin));
3835
}
3936
}
4037
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using JsonApiDotNetCore.Controllers;
2+
using JsonApiDotNetCore.Data;
3+
using JsonApiDotNetCore.Models;
4+
using JsonApiDotNetCore.Services;
5+
using JsonApiDotNetCoreExample.Models;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace JsonApiDotNetCoreExample.Controllers
9+
{
10+
public abstract class AbstractTodoItemsController<T> : JsonApiController<T> where T : class, IIdentifiable<int>
11+
{
12+
protected AbstractTodoItemsController(
13+
IJsonApiContext jsonApiContext,
14+
IEntityRepository<T, int> entityRepository,
15+
ILoggerFactory loggerFactory)
16+
: base(jsonApiContext, entityRepository, loggerFactory)
17+
{
18+
}
19+
}
20+
public class TodoItemsTestController : AbstractTodoItemsController<TodoItem>
21+
{
22+
public TodoItemsTestController(
23+
IJsonApiContext jsonApiContext,
24+
IEntityRepository<TodoItem> entityRepository,
25+
ILoggerFactory loggerFactory)
26+
: base(jsonApiContext, entityRepository, loggerFactory)
27+
{ }
28+
}
29+
}

‎test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/CustomControllerTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Net;
22
using System.Net.Http;
33
using System.Threading.Tasks;
4+
using DotNetCoreDocs.Models;
5+
using JsonApiDotNetCore.Serialization;
46
using Microsoft.AspNetCore.Hosting;
57
using Microsoft.AspNetCore.TestHost;
68
using Xunit;
79
using JsonApiDotNetCoreExample;
10+
using JsonApiDotNetCoreExample.Models;
811

912
namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
1013
{
@@ -30,5 +33,25 @@ public async Task NonJsonApiControllers_DoNotUse_Dasherized_Routes()
3033
// assert
3134
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
3235
}
36+
37+
[Fact]
38+
public async Task InheritedJsonApiControllers_Uses_Dasherized_Routes()
39+
{
40+
// Arrange
41+
var builder = new WebHostBuilder()
42+
.UseStartup<Startup>();
43+
var httpMethod = new HttpMethod("GET");
44+
var route = "/api/v1/todo-items-test";
45+
46+
var server = new TestServer(builder);
47+
var client = server.CreateClient();
48+
var request = new HttpRequestMessage(httpMethod, route);
49+
50+
// act
51+
var response = await client.SendAsync(request);
52+
53+
// assert
54+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
55+
}
3356
}
3457
}

0 commit comments

Comments
 (0)
Please sign in to comment.