55using JsonApiDotNetCore . Services ;
66using Microsoft . AspNetCore . Http ;
77using JsonApiDotNetCore . Models ;
8+ using JsonApiDotNetCore . Controllers ;
89
910namespace JsonApiDotNetCore . Internal . Query
1011{
@@ -13,53 +14,57 @@ public class QuerySet
1314 private readonly IJsonApiContext _jsonApiContext ;
1415
1516 public QuerySet (
16- IJsonApiContext jsonApiContext ,
17+ IJsonApiContext jsonApiContext ,
1718 IQueryCollection query )
1819 {
1920 _jsonApiContext = jsonApiContext ;
20- PageQuery = new PageQuery ( ) ;
21- Filters = new List < FilterQuery > ( ) ;
22- Fields = new List < string > ( ) ;
2321 BuildQuerySet ( query ) ;
2422 }
2523
26- public List < FilterQuery > Filters { get ; set ; }
27- public PageQuery PageQuery { get ; set ; }
28- public List < SortQuery > SortParameters { get ; set ; }
29- public List < string > IncludedRelationships { get ; set ; }
30- public List < string > Fields { get ; set ; }
24+ public List < FilterQuery > Filters { get ; set ; } = new List < FilterQuery > ( ) ;
25+ public PageQuery PageQuery { get ; set ; } = new PageQuery ( ) ;
26+ public List < SortQuery > SortParameters { get ; set ; } = new List < SortQuery > ( ) ;
27+ public List < string > IncludedRelationships { get ; set ; } = new List < string > ( ) ;
28+ public List < string > Fields { get ; set ; } = new List < string > ( ) ;
3129
3230 private void BuildQuerySet ( IQueryCollection query )
3331 {
32+ var disabledQueries = _jsonApiContext . GetControllerAttribute < DisableQueryAttribute > ( ) ? . QueryParams ?? QueryParams . None ;
33+
3434 foreach ( var pair in query )
3535 {
3636 if ( pair . Key . StartsWith ( "filter" ) )
3737 {
38- Filters . AddRange ( ParseFilterQuery ( pair . Key , pair . Value ) ) ;
38+ if ( disabledQueries . HasFlag ( QueryParams . Filter ) == false )
39+ Filters . AddRange ( ParseFilterQuery ( pair . Key , pair . Value ) ) ;
3940 continue ;
4041 }
4142
4243 if ( pair . Key . StartsWith ( "sort" ) )
4344 {
44- SortParameters = ParseSortParameters ( pair . Value ) ;
45+ if ( disabledQueries . HasFlag ( QueryParams . Sort ) == false )
46+ SortParameters = ParseSortParameters ( pair . Value ) ;
4547 continue ;
4648 }
4749
4850 if ( pair . Key . StartsWith ( "include" ) )
4951 {
50- IncludedRelationships = ParseIncludedRelationships ( pair . Value ) ;
52+ if ( disabledQueries . HasFlag ( QueryParams . Include ) == false )
53+ IncludedRelationships = ParseIncludedRelationships ( pair . Value ) ;
5154 continue ;
5255 }
5356
5457 if ( pair . Key . StartsWith ( "page" ) )
5558 {
56- PageQuery = ParsePageQuery ( pair . Key , pair . Value ) ;
59+ if ( disabledQueries . HasFlag ( QueryParams . Page ) == false )
60+ PageQuery = ParsePageQuery ( pair . Key , pair . Value ) ;
5761 continue ;
5862 }
5963
6064 if ( pair . Key . StartsWith ( "fields" ) )
6165 {
62- Fields = ParseFieldsQuery ( pair . Key , pair . Value ) ;
66+ if ( disabledQueries . HasFlag ( QueryParams . Fields ) == false )
67+ Fields = ParseFieldsQuery ( pair . Key , pair . Value ) ;
6368 continue ;
6469 }
6570
@@ -74,9 +79,9 @@ private List<FilterQuery> ParseFilterQuery(string key, string value)
7479 var queries = new List < FilterQuery > ( ) ;
7580
7681 var propertyName = key . Split ( '[' , ']' ) [ 1 ] . ToProperCase ( ) ;
77-
82+
7883 var values = value . Split ( ',' ) ;
79- foreach ( var val in values )
84+ foreach ( var val in values )
8085 {
8186 ( var operation , var filterValue ) = ParseFilterOperation ( val ) ;
8287 queries . Add ( new FilterQuery ( propertyName , filterValue , operation ) ) ;
@@ -87,14 +92,14 @@ private List<FilterQuery> ParseFilterQuery(string key, string value)
8792
8893 private ( string operation , string value ) ParseFilterOperation ( string value )
8994 {
90- if ( value . Length < 3 )
95+ if ( value . Length < 3 )
9196 return ( string . Empty , value ) ;
92-
97+
9398 var operation = value . Split ( ':' ) ;
9499
95- if ( operation . Length == 1 )
100+ if ( operation . Length == 1 )
96101 return ( string . Empty , value ) ;
97-
102+
98103 // remove prefix from value
99104 var prefix = operation [ 0 ] ;
100105 value = operation [ 1 ] ;
@@ -109,7 +114,7 @@ private PageQuery ParsePageQuery(string key, string value)
109114 PageQuery = PageQuery ?? new PageQuery ( ) ;
110115
111116 var propertyName = key . Split ( '[' , ']' ) [ 1 ] ;
112-
117+
113118 if ( propertyName == "size" )
114119 PageQuery . PageSize = Convert . ToInt32 ( value ) ;
115120 else if ( propertyName == "number" )
@@ -142,7 +147,7 @@ private List<SortQuery> ParseSortParameters(string value)
142147
143148 private List < string > ParseIncludedRelationships ( string value )
144149 {
145- if ( value . Contains ( "." ) )
150+ if ( value . Contains ( "." ) )
146151 throw new JsonApiException ( 400 , "Deeply nested relationships are not supported" ) ;
147152
148153 return value
@@ -157,11 +162,11 @@ private List<string> ParseFieldsQuery(string key, string value)
157162
158163 var includedFields = new List < string > { "Id" } ;
159164
160- if ( typeName != _jsonApiContext . RequestEntity . EntityName )
165+ if ( typeName != _jsonApiContext . RequestEntity . EntityName )
161166 return includedFields ;
162167
163168 var fields = value . Split ( ',' ) ;
164- foreach ( var field in fields )
169+ foreach ( var field in fields )
165170 {
166171 var internalAttrName = _jsonApiContext . RequestEntity
167172 . Attributes
0 commit comments