8
8
using JsonApiDotNetCore . Models ;
9
9
using Microsoft . AspNetCore . Http ;
10
10
11
- namespace JsonApiDotNetCore . Services {
12
- public interface IQueryParser {
11
+ namespace JsonApiDotNetCore . Services
12
+ {
13
+ public interface IQueryParser
14
+ {
13
15
QuerySet Parse ( IQueryCollection query ) ;
14
16
}
15
17
16
- public class QueryParser : IQueryParser {
18
+ public class QueryParser : IQueryParser
19
+ {
17
20
private readonly IControllerContext _controllerContext ;
18
21
private readonly JsonApiOptions _options ;
19
22
@@ -30,41 +33,49 @@ public class QueryParser : IQueryParser {
30
33
31
34
public QueryParser (
32
35
IControllerContext controllerContext ,
33
- JsonApiOptions options ) {
36
+ JsonApiOptions options )
37
+ {
34
38
_controllerContext = controllerContext ;
35
39
_options = options ;
36
40
}
37
41
38
- public virtual QuerySet Parse ( IQueryCollection query ) {
42
+ public virtual QuerySet Parse ( IQueryCollection query )
43
+ {
39
44
var querySet = new QuerySet ( ) ;
40
- var disabledQueries = _controllerContext . GetControllerAttribute < DisableQueryAttribute > ( ) ? . QueryParams ?? QueryParams . None ;
45
+ var disabledQueries = _controllerContext . GetControllerAttribute < DisableQueryAttribute > ( ) ? . QueryParams ?? QueryParams . None ;
41
46
42
- foreach ( var pair in query ) {
43
- if ( pair . Key . StartsWith ( FILTER ) ) {
47
+ foreach ( var pair in query )
48
+ {
49
+ if ( pair . Key . StartsWith ( FILTER ) )
50
+ {
44
51
if ( disabledQueries . HasFlag ( QueryParams . Filter ) == false )
45
52
querySet . Filters . AddRange ( ParseFilterQuery ( pair . Key , pair . Value ) ) ;
46
53
continue ;
47
54
}
48
55
49
- if ( pair . Key . StartsWith ( SORT ) ) {
56
+ if ( pair . Key . StartsWith ( SORT ) )
57
+ {
50
58
if ( disabledQueries . HasFlag ( QueryParams . Sort ) == false )
51
59
querySet . SortParameters = ParseSortParameters ( pair . Value ) ;
52
60
continue ;
53
61
}
54
62
55
- if ( pair . Key . StartsWith ( INCLUDE ) ) {
63
+ if ( pair . Key . StartsWith ( INCLUDE ) )
64
+ {
56
65
if ( disabledQueries . HasFlag ( QueryParams . Include ) == false )
57
66
querySet . IncludedRelationships = ParseIncludedRelationships ( pair . Value ) ;
58
67
continue ;
59
68
}
60
69
61
- if ( pair . Key . StartsWith ( PAGE ) ) {
70
+ if ( pair . Key . StartsWith ( PAGE ) )
71
+ {
62
72
if ( disabledQueries . HasFlag ( QueryParams . Page ) == false )
63
73
querySet . PageQuery = ParsePageQuery ( querySet . PageQuery , pair . Key , pair . Value ) ;
64
74
continue ;
65
75
}
66
76
67
- if ( pair . Key . StartsWith ( FIELDS ) ) {
77
+ if ( pair . Key . StartsWith ( FIELDS ) )
78
+ {
68
79
if ( disabledQueries . HasFlag ( QueryParams . Fields ) == false )
69
80
querySet . Fields = ParseFieldsQuery ( pair . Key , pair . Value ) ;
70
81
continue ;
@@ -77,23 +88,26 @@ public virtual QuerySet Parse(IQueryCollection query) {
77
88
return querySet ;
78
89
}
79
90
80
- protected virtual List < FilterQuery > ParseFilterQuery ( string key , string value ) {
91
+ protected virtual List < FilterQuery > ParseFilterQuery ( string key , string value )
92
+ {
81
93
// expected input = filter[id]=1
82
94
// expected input = filter[id]=eq:1
83
95
var queries = new List < FilterQuery > ( ) ;
84
96
85
- var propertyName = key . Split ( OPEN_BRACKET , CLOSE_BRACKET ) [ 1 ] ;
97
+ var propertyName = key . Split ( OPEN_BRACKET , CLOSE_BRACKET ) [ 1 ] ;
86
98
87
99
var values = value . Split ( COMMA ) ;
88
- foreach ( var val in values ) {
100
+ foreach ( var val in values )
101
+ {
89
102
( var operation , var filterValue ) = ParseFilterOperation ( val ) ;
90
103
queries . Add ( new FilterQuery ( propertyName , filterValue , operation ) ) ;
91
104
}
92
105
93
106
return queries ;
94
107
}
95
108
96
- protected virtual ( string operation , string value ) ParseFilterOperation ( string value ) {
109
+ protected virtual ( string operation , string value ) ParseFilterOperation ( string value )
110
+ {
97
111
if ( value . Length < 3 )
98
112
return ( string . Empty , value ) ;
99
113
@@ -159,7 +173,7 @@ protected virtual List<SortQuery> ParseSortParameters(string value)
159
173
160
174
var attribute = GetAttribute ( propertyName ) ;
161
175
162
- if ( attribute . IsSortable == false )
176
+ if ( attribute . IsSortable == false )
163
177
throw new JsonApiException ( 400 , $ "Sort is not allowed for attribute '{ attribute . PublicAttributeName } '.") ;
164
178
165
179
sortParameters . Add ( new SortQuery ( direction , attribute ) ) ;
@@ -168,7 +182,8 @@ protected virtual List<SortQuery> ParseSortParameters(string value)
168
182
return sortParameters ;
169
183
}
170
184
171
- protected virtual List < string > ParseIncludedRelationships ( string value ) {
185
+ protected virtual List < string > ParseIncludedRelationships ( string value )
186
+ {
172
187
const string NESTED_DELIMITER = "." ;
173
188
if ( value . Contains ( NESTED_DELIMITER ) )
174
189
throw new JsonApiException ( 400 , "Deeply nested relationships are not supported" ) ;
@@ -195,7 +210,7 @@ protected virtual List<string> ParseFieldsQuery(string key, string value)
195
210
{
196
211
var attr = _controllerContext . RequestEntity
197
212
. Attributes
198
- . SingleOrDefault ( a => string . Equals ( a . PublicAttributeName , field , StringComparison . OrdinalIgnoreCase ) ) ;
213
+ . SingleOrDefault ( a => a . Is ( field ) ) ;
199
214
200
215
if ( attr == null ) throw new JsonApiException ( 400 , $ "'{ _controllerContext . RequestEntity . EntityName } ' does not contain '{ field } '.") ;
201
216
@@ -213,9 +228,7 @@ protected virtual AttrAttribute GetAttribute(string propertyName)
213
228
return _controllerContext
214
229
. RequestEntity
215
230
. Attributes
216
- . Single ( attr =>
217
- string . Equals ( attr . PublicAttributeName , propertyName , StringComparison . OrdinalIgnoreCase )
218
- ) ;
231
+ . Single ( attr => attr . Is ( propertyName ) ) ;
219
232
}
220
233
catch ( InvalidOperationException e )
221
234
{
0 commit comments