Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace JsonApiDotNetCore.Middleware
public interface IControllerResourceMapping
{
/// <summary>
/// Get the associated resource type for the provided controller name.
/// Get the associated resource type for the provided controller type.
/// </summary>
Type GetResourceTypeForController(string controllerName);
Type GetResourceTypeForController(Type controllerType);
}
}
13 changes: 8 additions & 5 deletions src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
Expand Down Expand Up @@ -47,7 +48,7 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin

RouteValueDictionary routeValues = httpContext.GetRouteData().Values;

ResourceContext primaryResourceContext = CreatePrimaryResourceContext(routeValues, controllerResourceMapping, resourceContextProvider);
ResourceContext primaryResourceContext = CreatePrimaryResourceContext(httpContext, controllerResourceMapping, resourceContextProvider);

if (primaryResourceContext != null)
{
Expand Down Expand Up @@ -77,14 +78,16 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
await _next(httpContext);
}

private static ResourceContext CreatePrimaryResourceContext(RouteValueDictionary routeValues, IControllerResourceMapping controllerResourceMapping,
private static ResourceContext CreatePrimaryResourceContext(HttpContext httpContext, IControllerResourceMapping controllerResourceMapping,
IResourceContextProvider resourceContextProvider)
{
string controllerName = (string)routeValues["controller"];
Endpoint endpoint = httpContext.GetEndpoint();
var controllerActionDescriptor = endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>();

if (controllerName != null)
if (controllerActionDescriptor != null)
{
Type resourceType = controllerResourceMapping.GetResourceTypeForController(controllerName);
Type controllerType = controllerActionDescriptor.ControllerTypeInfo;
Type resourceType = controllerResourceMapping.GetResourceTypeForController(controllerType);

if (resourceType != null)
{
Expand Down
12 changes: 6 additions & 6 deletions src/JsonApiDotNetCore/Middleware/JsonApiRoutingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class JsonApiRoutingConvention : IJsonApiRoutingConvention
private readonly IJsonApiOptions _options;
private readonly IResourceContextProvider _resourceContextProvider;
private readonly HashSet<string> _registeredTemplates = new HashSet<string>();
private readonly Dictionary<string, ResourceContext> _registeredResources = new Dictionary<string, ResourceContext>();
private readonly Dictionary<Type, ResourceContext> _resourceContextPerControllerTypeMap = new Dictionary<Type, ResourceContext>();

public JsonApiRoutingConvention(IJsonApiOptions options, IResourceContextProvider resourceContextProvider)
{
Expand All @@ -46,11 +46,11 @@ public JsonApiRoutingConvention(IJsonApiOptions options, IResourceContextProvide
}

/// <inheritdoc />
public Type GetResourceTypeForController(string controllerName)
public Type GetResourceTypeForController(Type controllerType)
{
ArgumentGuard.NotNullNorEmpty(controllerName, nameof(controllerName));
ArgumentGuard.NotNull(controllerType, nameof(controllerType));

if (_registeredResources.TryGetValue(controllerName, out ResourceContext resourceContext))
if (_resourceContextPerControllerTypeMap.TryGetValue(controllerType, out ResourceContext resourceContext))
{
return resourceContext.ResourceType;
}
Expand All @@ -77,7 +77,7 @@ public void Apply(ApplicationModel application)

if (resourceContext != null)
{
_registeredResources.Add(controller.ControllerName, resourceContext);
_resourceContextPerControllerTypeMap.Add(controller.ControllerType, resourceContext);
}
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ private bool IsRoutingConventionEnabled(ControllerModel controller)
/// </summary>
private string TemplateFromResource(ControllerModel model)
{
if (_registeredResources.TryGetValue(model.ControllerName, out ResourceContext resourceContext))
if (_resourceContextPerControllerTypeMap.TryGetValue(model.ControllerType, out ResourceContext resourceContext))
{
string template = $"{_options.Namespace}/{resourceContext.PublicName}";

Expand Down
12 changes: 10 additions & 2 deletions test/UnitTests/Middleware/JsonApiMiddlewareTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Resources.Annotations;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Controllers;
using Moq;
using Moq.Language;
using Xunit;
Expand Down Expand Up @@ -96,7 +98,7 @@ private InvokeConfiguration GetConfiguration(string path, string resourceName =

const string forcedNamespace = "api/v1";
var mockMapping = new Mock<IControllerResourceMapping>();
mockMapping.Setup(mapping => mapping.GetResourceTypeForController(It.IsAny<string>())).Returns(typeof(string));
mockMapping.Setup(mapping => mapping.GetResourceTypeForController(It.IsAny<Type>())).Returns(typeof(string));

Mock<IJsonApiOptions> mockOptions = CreateMockOptions(forcedNamespace);
Mock<IResourceGraph> mockGraph = CreateMockResourceGraph(resourceName, relType != null);
Expand Down Expand Up @@ -151,7 +153,13 @@ private static DefaultHttpContext CreateHttpContext(string path, bool isRelation
}

context.Features.Set<IRouteValuesFeature>(feature);
context.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(), null));

var controllerActionDescriptor = new ControllerActionDescriptor
{
ControllerTypeInfo = (TypeInfo)typeof(object)
};

context.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(controllerActionDescriptor), null));
return context;
}

Expand Down
12 changes: 10 additions & 2 deletions test/UnitTests/Middleware/JsonApiRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCoreExample.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
Expand Down Expand Up @@ -46,7 +48,7 @@ public async Task Sets_request_properties_correctly(string requestMethod, string

var controllerResourceMappingMock = new Mock<IControllerResourceMapping>();

controllerResourceMappingMock.Setup(mapping => mapping.GetResourceTypeForController(It.IsAny<string>())).Returns(typeof(Article));
controllerResourceMappingMock.Setup(mapping => mapping.GetResourceTypeForController(It.IsAny<Type>())).Returns(typeof(Article));

var httpContext = new DefaultHttpContext();
SetupRoutes(httpContext, requestMethod, requestPath);
Expand Down Expand Up @@ -98,7 +100,13 @@ private static void SetupRoutes(HttpContext httpContext, string requestMethod, s
}

httpContext.Features.Set<IRouteValuesFeature>(feature);
httpContext.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(), null));

var controllerActionDescriptor = new ControllerActionDescriptor
{
ControllerTypeInfo = (TypeInfo)typeof(object)
};

httpContext.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(controllerActionDescriptor), null));
}
}
}