5
5
using JsonApiDotNetCore . Configuration ;
6
6
using JsonApiDotNetCore . Internal ;
7
7
using JsonApiDotNetCore . Internal . Contracts ;
8
+ using JsonApiDotNetCore . Internal . Query ;
8
9
using JsonApiDotNetCore . Managers . Contracts ;
9
10
using JsonApiDotNetCore . Services ;
10
11
using Microsoft . AspNetCore . Http ;
@@ -16,14 +17,13 @@ namespace JsonApiDotNetCore.Middleware
16
17
/// <summary>
17
18
/// Can be overwritten to help you out during testing
18
19
///
19
- /// This sets all necessary paaramters relating to the HttpContext for JADNC
20
+ /// This sets all necessary parameters relating to the HttpContext for JADNC
20
21
/// </summary>
21
22
public class RequestMiddleware
22
23
{
23
24
private readonly RequestDelegate _next ;
24
25
private IResourceGraph _resourceGraph ;
25
26
private HttpContext _httpContext ;
26
- private IJsonApiContext _jsonApiContext ;
27
27
private IRequestManager _requestManager ;
28
28
private IPageManager _pageManager ;
29
29
private IQueryParser _queryParser ;
@@ -40,10 +40,10 @@ public async Task Invoke(HttpContext httpContext,
40
40
IRequestManager requestManager ,
41
41
IPageManager pageManager ,
42
42
IQueryParser queryParser ,
43
- IJsonApiOptions options )
43
+ IJsonApiOptions options
44
+ )
44
45
{
45
46
_httpContext = httpContext ;
46
- _jsonApiContext = jsonApiContext ;
47
47
_resourceGraph = resourceGraph ;
48
48
_requestManager = requestManager ;
49
49
_pageManager = pageManager ;
@@ -58,100 +58,10 @@ public async Task Invoke(HttpContext httpContext,
58
58
// since the JsonApiContext is using field initializers
59
59
// Need to work on finding a better solution.
60
60
jsonApiContext . BeginOperation ( ) ;
61
- ContextEntity contextEntityCurrent = GetCurrentEntity ( ) ;
62
- // the contextEntity is null eg when we're using a non-JsonApiDotNetCore route.
63
- if ( contextEntityCurrent != null )
64
- {
65
- requestManager . SetContextEntity ( contextEntityCurrent ) ;
66
- // TODO: this does not need to be reset every request: we shouldn't need to rely on an external request to figure out the basepath of current application
67
- requestManager . BasePath = GetBasePath ( contextEntityCurrent . EntityName ) ;
68
- //Handle all querySet
69
- HandleUriParameters ( ) ;
70
- requestManager . IsRelationshipPath = PathIsRelationship ( ) ;
71
- // BACKWARD COMPATIBILITY for v4 will be removed in v5
72
- jsonApiContext . RequestManager = requestManager ;
73
- jsonApiContext . PageManager = new PageManager ( new LinkBuilder ( options , requestManager ) , options , requestManager ) ;
74
- }
75
61
76
62
await _next ( httpContext ) ;
77
63
}
78
64
}
79
- /// <summary>
80
- /// Parses the uri
81
- /// </summary>
82
- /// <param name="context"></param>
83
- /// <param name="requestManager"></param>
84
- protected void HandleUriParameters ( )
85
- {
86
- if ( _httpContext . Request . Query . Count > 0 )
87
- {
88
- //requestManager.FullQuerySet = context.Request.Query;
89
- var querySet = _queryParser . Parse ( _httpContext . Request . Query ) ;
90
- _requestManager . QuerySet = querySet ; //this shouldn't be exposed
91
- _pageManager . PageSize = querySet . PageQuery . PageSize ?? _pageManager . PageSize ;
92
- _pageManager . CurrentPage = querySet . PageQuery . PageOffset ?? _pageManager . CurrentPage ;
93
- _requestManager . IncludedRelationships = _requestManager . QuerySet . IncludedRelationships ;
94
- }
95
- }
96
-
97
- protected bool PathIsRelationship ( )
98
- {
99
- var actionName = ( string ) _httpContext . GetRouteData ( ) . Values [ "action" ] ;
100
- return actionName . ToLower ( ) . Contains ( "relationships" ) ;
101
- }
102
- private string GetBasePath ( string entityName )
103
- {
104
- var r = _httpContext . Request ;
105
- if ( _options . RelativeLinks )
106
- {
107
- return GetNamespaceFromPath ( r . Path , entityName ) ;
108
- }
109
- else
110
- {
111
- return $ "{ r . Scheme } ://{ r . Host } { GetNamespaceFromPath ( r . Path , entityName ) } ";
112
- }
113
- }
114
- internal static string GetNamespaceFromPath ( string path , string entityName )
115
- {
116
- var entityNameSpan = entityName . AsSpan ( ) ;
117
- var pathSpan = path . AsSpan ( ) ;
118
- const char delimiter = '/' ;
119
- for ( var i = 0 ; i < pathSpan . Length ; i ++ )
120
- {
121
- if ( pathSpan [ i ] . Equals ( delimiter ) )
122
- {
123
- var nextPosition = i + 1 ;
124
- if ( pathSpan . Length > i + entityNameSpan . Length )
125
- {
126
- var possiblePathSegment = pathSpan . Slice ( nextPosition , entityNameSpan . Length ) ;
127
- if ( entityNameSpan . SequenceEqual ( possiblePathSegment ) )
128
- {
129
- // check to see if it's the last position in the string
130
- // or if the next character is a /
131
- var lastCharacterPosition = nextPosition + entityNameSpan . Length ;
132
-
133
- if ( lastCharacterPosition == pathSpan . Length || pathSpan . Length >= lastCharacterPosition + 2 && pathSpan [ lastCharacterPosition ] . Equals ( delimiter ) )
134
- {
135
- return pathSpan . Slice ( 0 , i ) . ToString ( ) ;
136
- }
137
- }
138
- }
139
- }
140
- }
141
-
142
- return string . Empty ;
143
- }
144
- /// <summary>
145
- /// Gets the current entity that we need for serialization and deserialization.
146
- /// </summary>
147
- /// <param name="context"></param>
148
- /// <param name="resourceGraph"></param>
149
- /// <returns></returns>
150
- private ContextEntity GetCurrentEntity ( )
151
- {
152
- var controllerName = ( string ) _httpContext . GetRouteData ( ) . Values [ "controller" ] ;
153
- return _resourceGraph . GetEntityFromControllerName ( controllerName ) ;
154
- }
155
65
156
66
private bool IsValid ( )
157
67
{
0 commit comments