6
6
using JsonApiDotNetCore . Controllers ;
7
7
using JsonApiDotNetCore . Extensions ;
8
8
using JsonApiDotNetCore . Graph ;
9
+ using JsonApiDotNetCore . Internal . Contracts ;
9
10
using JsonApiDotNetCore . Models ;
10
11
using Microsoft . AspNetCore . Mvc ;
11
12
using Microsoft . AspNetCore . Mvc . ApplicationModels ;
@@ -37,12 +38,14 @@ public class DefaultRoutingConvention : IJsonApiRoutingConvention, IControllerRe
37
38
{
38
39
private readonly string _namespace ;
39
40
private readonly IResourceNameFormatter _formatter ;
41
+ private readonly IResourceGraph _resourceGraph ;
40
42
private readonly HashSet < string > _registeredTemplates = new HashSet < string > ( ) ;
41
43
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 )
43
45
{
44
46
_namespace = options . Namespace ;
45
47
_formatter = formatter ;
48
+ _resourceGraph = resourceGraph ;
46
49
}
47
50
48
51
/// <inheritdoc/>
@@ -58,18 +61,43 @@ public void Apply(ApplicationModel application)
58
61
foreach ( var controller in application . Controllers )
59
62
{
60
63
var resourceType = GetResourceTypeFromController ( controller . ControllerType ) ;
64
+
61
65
if ( resourceType != null )
66
+ {
62
67
_registeredResources . Add ( controller . ControllerName , resourceType ) ;
68
+ }
63
69
64
70
if ( RoutingConventionDisabled ( controller ) == false )
71
+ {
65
72
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
+ }
66
87
67
- var template = TemplateFromResource ( controller ) ?? TemplateFromController ( controller ) ;
68
88
if ( template == null )
89
+ {
69
90
throw new JsonApiSetupException ( $ "Controllers with overlapping route templates detected: { controller . ControllerType . FullName } ") ;
91
+ }
70
92
71
93
controller . Selectors [ 0 ] . AttributeRouteModel = new AttributeRouteModel { Template = template } ;
72
94
}
95
+
96
+ }
97
+
98
+ public void HandleControllers ( IList < ControllerModel > controllers )
99
+ {
100
+
73
101
}
74
102
75
103
/// <summary>
@@ -128,7 +156,7 @@ private Type GetResourceTypeFromController(Type type)
128
156
{
129
157
var nextBaseType = currentBaseType . BaseType ;
130
158
131
- if ( ( nextBaseType == controllerBase || nextBaseType == jsonApiMixin ) && currentBaseType . IsGenericType )
159
+ if ( ( nextBaseType == controllerBase || nextBaseType == jsonApiMixin ) && currentBaseType . IsGenericType )
132
160
{
133
161
var potentialResource = currentBaseType . GetGenericArguments ( ) . FirstOrDefault ( t => t . Inherits ( identifiable ) ) ;
134
162
if ( potentialResource != null )
0 commit comments