66using JsonApiDotNetCore . Controllers ;
77using JsonApiDotNetCore . Extensions ;
88using JsonApiDotNetCore . Graph ;
9+ using JsonApiDotNetCore . Internal . Contracts ;
910using JsonApiDotNetCore . Models ;
1011using Microsoft . AspNetCore . Mvc ;
1112using Microsoft . AspNetCore . Mvc . ApplicationModels ;
@@ -37,12 +38,14 @@ public class DefaultRoutingConvention : IJsonApiRoutingConvention, IControllerRe
3738 {
3839 private readonly string _namespace ;
3940 private readonly IResourceNameFormatter _formatter ;
41+ private readonly IResourceGraph _resourceGraph ;
4042 private readonly HashSet < string > _registeredTemplates = new HashSet < string > ( ) ;
4143 private readonly Dictionary < string , Type > _registeredResources = new Dictionary < string , Type > ( ) ;
42- public DefaultRoutingConvention ( IJsonApiOptions options , IResourceNameFormatter formatter )
44+ public DefaultRoutingConvention ( IJsonApiOptions options , IResourceNameFormatter formatter , IResourceGraph resourceGraph )
4345 {
4446 _namespace = options . Namespace ;
4547 _formatter = formatter ;
48+ _resourceGraph = resourceGraph ;
4649 }
4750
4851 /// <inheritdoc/>
@@ -58,18 +61,43 @@ public void Apply(ApplicationModel application)
5861 foreach ( var controller in application . Controllers )
5962 {
6063 var resourceType = GetResourceTypeFromController ( controller . ControllerType ) ;
64+
6165 if ( resourceType != null )
66+ {
6267 _registeredResources . Add ( controller . ControllerName , resourceType ) ;
68+ }
6369
6470 if ( RoutingConventionDisabled ( controller ) == false )
71+ {
6572 continue ;
73+ }
74+ // if defined in resourcegraph, it should be used
75+ var contexts = _resourceGraph . GetResourceContexts ( ) ;
76+
77+ var foundResourceGraph = contexts . First ( c => c . ResourceType == resourceType ) ;
78+ string template = null ;
79+ if ( foundResourceGraph != null )
80+ {
81+ template = $ "{ _namespace } /{ foundResourceGraph . ResourceName } ";
82+ }
83+ else
84+ {
85+ template = TemplateFromResource ( controller ) ?? TemplateFromController ( controller ) ;
86+ }
6687
67- var template = TemplateFromResource ( controller ) ?? TemplateFromController ( controller ) ;
6888 if ( template == null )
89+ {
6990 throw new JsonApiSetupException ( $ "Controllers with overlapping route templates detected: { controller . ControllerType . FullName } ") ;
91+ }
7092
7193 controller . Selectors [ 0 ] . AttributeRouteModel = new AttributeRouteModel { Template = template } ;
7294 }
95+
96+ }
97+
98+ public void HandleControllers ( IList < ControllerModel > controllers )
99+ {
100+
73101 }
74102
75103 /// <summary>
@@ -128,7 +156,7 @@ private Type GetResourceTypeFromController(Type type)
128156 {
129157 var nextBaseType = currentBaseType . BaseType ;
130158
131- if ( ( nextBaseType == controllerBase || nextBaseType == jsonApiMixin ) && currentBaseType . IsGenericType )
159+ if ( ( nextBaseType == controllerBase || nextBaseType == jsonApiMixin ) && currentBaseType . IsGenericType )
132160 {
133161 var potentialResource = currentBaseType . GetGenericArguments ( ) . FirstOrDefault ( t => t . Inherits ( identifiable ) ) ;
134162 if ( potentialResource != null )
0 commit comments