88using JsonApiDotNetCore . Models ;
99using Microsoft . AspNetCore . Http ;
1010
11- namespace JsonApiDotNetCore . Services {
12- public interface IQueryParser {
11+ namespace JsonApiDotNetCore . Services
12+ {
13+ public interface IQueryParser
14+ {
1315 QuerySet Parse ( IQueryCollection query ) ;
1416 }
1517
16- public class QueryParser : IQueryParser {
18+ public class QueryParser : IQueryParser
19+ {
1720 private readonly IControllerContext _controllerContext ;
1821 private readonly JsonApiOptions _options ;
1922
@@ -30,41 +33,49 @@ public class QueryParser : IQueryParser {
3033
3134 public QueryParser (
3235 IControllerContext controllerContext ,
33- JsonApiOptions options ) {
36+ JsonApiOptions options )
37+ {
3438 _controllerContext = controllerContext ;
3539 _options = options ;
3640 }
3741
38- public virtual QuerySet Parse ( IQueryCollection query ) {
42+ public virtual QuerySet Parse ( IQueryCollection query )
43+ {
3944 var querySet = new QuerySet ( ) ;
40- var disabledQueries = _controllerContext . GetControllerAttribute < DisableQueryAttribute > ( ) ? . QueryParams ?? QueryParams . None ;
45+ var disabledQueries = _controllerContext . GetControllerAttribute < DisableQueryAttribute > ( ) ? . QueryParams ?? QueryParams . None ;
4146
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+ {
4451 if ( disabledQueries . HasFlag ( QueryParams . Filter ) == false )
4552 querySet . Filters . AddRange ( ParseFilterQuery ( pair . Key , pair . Value ) ) ;
4653 continue ;
4754 }
4855
49- if ( pair . Key . StartsWith ( SORT ) ) {
56+ if ( pair . Key . StartsWith ( SORT ) )
57+ {
5058 if ( disabledQueries . HasFlag ( QueryParams . Sort ) == false )
5159 querySet . SortParameters = ParseSortParameters ( pair . Value ) ;
5260 continue ;
5361 }
5462
55- if ( pair . Key . StartsWith ( INCLUDE ) ) {
63+ if ( pair . Key . StartsWith ( INCLUDE ) )
64+ {
5665 if ( disabledQueries . HasFlag ( QueryParams . Include ) == false )
5766 querySet . IncludedRelationships = ParseIncludedRelationships ( pair . Value ) ;
5867 continue ;
5968 }
6069
61- if ( pair . Key . StartsWith ( PAGE ) ) {
70+ if ( pair . Key . StartsWith ( PAGE ) )
71+ {
6272 if ( disabledQueries . HasFlag ( QueryParams . Page ) == false )
6373 querySet . PageQuery = ParsePageQuery ( querySet . PageQuery , pair . Key , pair . Value ) ;
6474 continue ;
6575 }
6676
67- if ( pair . Key . StartsWith ( FIELDS ) ) {
77+ if ( pair . Key . StartsWith ( FIELDS ) )
78+ {
6879 if ( disabledQueries . HasFlag ( QueryParams . Fields ) == false )
6980 querySet . Fields = ParseFieldsQuery ( pair . Key , pair . Value ) ;
7081 continue ;
@@ -77,23 +88,26 @@ public virtual QuerySet Parse(IQueryCollection query) {
7788 return querySet ;
7889 }
7990
80- protected virtual List < FilterQuery > ParseFilterQuery ( string key , string value ) {
91+ protected virtual List < FilterQuery > ParseFilterQuery ( string key , string value )
92+ {
8193 // expected input = filter[id]=1
8294 // expected input = filter[id]=eq:1
8395 var queries = new List < FilterQuery > ( ) ;
8496
85- var propertyName = key . Split ( OPEN_BRACKET , CLOSE_BRACKET ) [ 1 ] ;
97+ var propertyName = key . Split ( OPEN_BRACKET , CLOSE_BRACKET ) [ 1 ] ;
8698
8799 var values = value . Split ( COMMA ) ;
88- foreach ( var val in values ) {
100+ foreach ( var val in values )
101+ {
89102 ( var operation , var filterValue ) = ParseFilterOperation ( val ) ;
90103 queries . Add ( new FilterQuery ( propertyName , filterValue , operation ) ) ;
91104 }
92105
93106 return queries ;
94107 }
95108
96- protected virtual ( string operation , string value ) ParseFilterOperation ( string value ) {
109+ protected virtual ( string operation , string value ) ParseFilterOperation ( string value )
110+ {
97111 if ( value . Length < 3 )
98112 return ( string . Empty , value ) ;
99113
@@ -159,7 +173,7 @@ protected virtual List<SortQuery> ParseSortParameters(string value)
159173
160174 var attribute = GetAttribute ( propertyName ) ;
161175
162- if ( attribute . IsSortable == false )
176+ if ( attribute . IsSortable == false )
163177 throw new JsonApiException ( 400 , $ "Sort is not allowed for attribute '{ attribute . PublicAttributeName } '.") ;
164178
165179 sortParameters . Add ( new SortQuery ( direction , attribute ) ) ;
@@ -168,7 +182,8 @@ protected virtual List<SortQuery> ParseSortParameters(string value)
168182 return sortParameters ;
169183 }
170184
171- protected virtual List < string > ParseIncludedRelationships ( string value ) {
185+ protected virtual List < string > ParseIncludedRelationships ( string value )
186+ {
172187 const string NESTED_DELIMITER = "." ;
173188 if ( value . Contains ( NESTED_DELIMITER ) )
174189 throw new JsonApiException ( 400 , "Deeply nested relationships are not supported" ) ;
@@ -195,7 +210,7 @@ protected virtual List<string> ParseFieldsQuery(string key, string value)
195210 {
196211 var attr = _controllerContext . RequestEntity
197212 . Attributes
198- . SingleOrDefault ( a => string . Equals ( a . PublicAttributeName , field , StringComparison . OrdinalIgnoreCase ) ) ;
213+ . SingleOrDefault ( a => a . Is ( field ) ) ;
199214
200215 if ( attr == null ) throw new JsonApiException ( 400 , $ "'{ _controllerContext . RequestEntity . EntityName } ' does not contain '{ field } '.") ;
201216
@@ -213,9 +228,7 @@ protected virtual AttrAttribute GetAttribute(string propertyName)
213228 return _controllerContext
214229 . RequestEntity
215230 . Attributes
216- . Single ( attr =>
217- string . Equals ( attr . PublicAttributeName , propertyName , StringComparison . OrdinalIgnoreCase )
218- ) ;
231+ . Single ( attr => attr . Is ( propertyName ) ) ;
219232 }
220233 catch ( InvalidOperationException e )
221234 {
0 commit comments