Skip to content

Commit

Permalink
fix(DasherizedRoutingConvention): Check for abstract subtype
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMattner committed Apr 12, 2017
1 parent 8a07949 commit 581f4bd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Controllers/JsonApiControllerMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace JsonApiDotNetCore.Controllers
{
public class JsonApiControllerMixin : Controller
public abstract class JsonApiControllerMixin : Controller
{
public JsonApiControllerMixin()
protected JsonApiControllerMixin()
{ }

protected IActionResult UnprocessableEntity()
Expand Down
7 changes: 2 additions & 5 deletions src/JsonApiDotNetCore/Internal/DasherizedRoutingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ public void Apply(ApplicationModel application)
}
}

private bool IsJsonApiController(ControllerModel controller)
private bool IsJsonApiController(ControllerModel controller)
{
var controllerBaseType = controller.ControllerType.BaseType;
if(!controllerBaseType.IsConstructedGenericType) return false;
var genericTypeDefinition = controllerBaseType.GetGenericTypeDefinition();
return (genericTypeDefinition == typeof(JsonApiController<,>) || genericTypeDefinition == typeof(JsonApiController<>));
return controller.ControllerType.IsSubclassOf(typeof(JsonApiControllerMixin));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Data;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
{
public abstract class AbstractTodoItemsController<T> : JsonApiController<T> where T : class, IIdentifiable<int>
{
protected AbstractTodoItemsController(
IJsonApiContext jsonApiContext,
IEntityRepository<T, int> entityRepository,
ILoggerFactory loggerFactory)
: base(jsonApiContext, entityRepository, loggerFactory)
{
}
}
public class TodoItemsTestController : AbstractTodoItemsController<TodoItem>
{
public TodoItemsTestController(
IJsonApiContext jsonApiContext,
IEntityRepository<TodoItem> entityRepository,
ILoggerFactory loggerFactory)
: base(jsonApiContext, entityRepository, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using DotNetCoreDocs.Models;
using JsonApiDotNetCore.Serialization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Xunit;
using JsonApiDotNetCoreExample;
using JsonApiDotNetCoreExample.Models;

namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
{
Expand All @@ -30,5 +33,25 @@ public async Task NonJsonApiControllers_DoNotUse_Dasherized_Routes()
// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public async Task InheritedJsonApiControllers_Uses_Dasherized_Routes()
{
// Arrange
var builder = new WebHostBuilder()
.UseStartup<Startup>();
var httpMethod = new HttpMethod("GET");
var route = "/api/v1/todo-items-test";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
}

0 comments on commit 581f4bd

Please sign in to comment.