@@ -170,25 +170,23 @@ def parse_fields(fields)
170170 end
171171 type_resource = Resource . resource_for ( @resource_klass . module_path + underscored_type . to_s )
172172 rescue NameError
173- @errors . concat ( JSONAPI ::Exceptions ::InvalidResource . new ( type ) . errors )
174- rescue JSONAPI ::Exceptions ::InvalidResource => e
175- @errors . concat ( e . errors )
173+ fail JSONAPI ::Exceptions ::InvalidResource . new ( type )
176174 end
177175
178176 if type_resource . nil?
179- @errors . concat ( JSONAPI ::Exceptions ::InvalidResource . new ( type ) . errors )
177+ fail JSONAPI ::Exceptions ::InvalidResource . new ( type )
180178 else
181179 unless values . nil?
182180 valid_fields = type_resource . fields . collect { |key | format_key ( key ) }
183181 values . each do |field |
184182 if valid_fields . include? ( field )
185183 extracted_fields [ type ] . push unformat_key ( field )
186184 else
187- @errors . concat ( JSONAPI ::Exceptions ::InvalidField . new ( type , field ) . errors )
185+ fail JSONAPI ::Exceptions ::InvalidField . new ( type , field )
188186 end
189187 end
190188 else
191- @errors . concat ( JSONAPI ::Exceptions ::InvalidField . new ( type , 'nil' ) . errors )
189+ fail JSONAPI ::Exceptions ::InvalidField . new ( type , 'nil' )
192190 end
193191 end
194192 end
@@ -205,16 +203,15 @@ def check_include(resource_klass, include_parts)
205203 check_include ( Resource . resource_for ( resource_klass . module_path + relationship . class_name . to_s . underscore ) , include_parts . last . partition ( '.' ) )
206204 end
207205 else
208- @errors . concat ( JSONAPI ::Exceptions ::InvalidInclude . new ( format_key ( resource_klass . _type ) ,
209- include_parts . first ) . errors )
206+ fail JSONAPI ::Exceptions ::InvalidInclude . new ( format_key ( resource_klass . _type ) , include_parts . first )
210207 end
211208 end
212209
213210 def parse_include_directives ( raw_include )
214211 return unless raw_include
215212
216213 unless JSONAPI . configuration . allow_include
217- fail JSONAPI ::Exceptions ::ParametersNotAllowed . new ( [ :include ] )
214+ fail JSONAPI ::Exceptions ::ParameterNotAllowed . new ( :include )
218215 end
219216
220217 included_resources = [ ]
@@ -226,19 +223,24 @@ def parse_include_directives(raw_include)
226223
227224 return if included_resources . empty?
228225
229- result = included_resources . compact . map do |included_resource |
230- check_include ( @resource_klass , included_resource . partition ( '.' ) )
231- unformat_key ( included_resource ) . to_s
232- end
226+ begin
227+ result = included_resources . compact . map do |included_resource |
228+ check_include ( @resource_klass , included_resource . partition ( '.' ) )
229+ unformat_key ( included_resource ) . to_s
230+ end
233231
234- @include_directives = JSONAPI ::IncludeDirectives . new ( @resource_klass , result )
232+ @include_directives = JSONAPI ::IncludeDirectives . new ( @resource_klass , result )
233+ rescue JSONAPI ::Exceptions ::InvalidInclude => e
234+ @errors . concat ( e . errors )
235+ @include_directives = { }
236+ end
235237 end
236238
237239 def parse_filters ( filters )
238240 return unless filters
239241
240242 unless JSONAPI . configuration . allow_filter
241- fail JSONAPI ::Exceptions ::ParametersNotAllowed . new ( [ :filter ] )
243+ fail JSONAPI ::Exceptions ::ParameterNotAllowed . new ( :filter )
242244 end
243245
244246 unless filters . class . method_defined? ( :each )
@@ -251,7 +253,7 @@ def parse_filters(filters)
251253 if @resource_klass . _allowed_filter? ( filter )
252254 @filters [ filter ] = value
253255 else
254- @errors . concat ( JSONAPI ::Exceptions ::FilterNotAllowed . new ( filter ) . errors )
256+ fail JSONAPI ::Exceptions ::FilterNotAllowed . new ( filter )
255257 end
256258 end
257259 end
@@ -267,7 +269,7 @@ def parse_sort_criteria(sort_criteria)
267269 return unless sort_criteria . present?
268270
269271 unless JSONAPI . configuration . allow_sort
270- fail JSONAPI ::Exceptions ::ParametersNotAllowed . new ( [ :sort ] )
272+ fail JSONAPI ::Exceptions ::ParameterNotAllowed . new ( :sort )
271273 end
272274
273275 sorts = [ ]
@@ -296,9 +298,8 @@ def check_sort_criteria(resource_klass, sort_criteria)
296298 sort_field = sort_criteria [ :field ]
297299 sortable_fields = resource_klass . sortable_fields ( context )
298300
299- unless sortable_fields . include? sort_field . to_sym
300- @errors . concat ( JSONAPI ::Exceptions ::InvalidSortCriteria
301- . new ( format_key ( resource_klass . _type ) , sort_field ) . errors )
301+ unless sortable_fields . include? sort_field . to_sym
302+ fail JSONAPI ::Exceptions ::InvalidSortCriteria . new ( format_key ( resource_klass . _type ) , sort_field )
302303 end
303304 end
304305
@@ -473,7 +474,7 @@ def parse_to_one_relationship(link_value, relationship)
473474
474475 unless links_object [ :id ] . nil?
475476 resource = self . resource_klass || Resource
476- relationship_resource = resource . resource_for ( unformat_key ( links_object [ :type ] ) . to_s )
477+ relationship_resource = resource . resource_for ( unformat_key ( relationship . options [ :class_name ] || links_object [ :type ] ) . to_s )
477478 relationship_id = relationship_resource . verify_key ( links_object [ :id ] , @context )
478479 if relationship . polymorphic?
479480 { id : relationship_id , type : unformat_key ( links_object [ :type ] . to_s ) }
@@ -528,45 +529,52 @@ def verify_permitted_params(params, allowed_fields)
528529 when 'relationships'
529530 value . keys . each do |links_key |
530531 unless formatted_allowed_fields . include? ( links_key . to_sym )
531- params_not_allowed . push ( links_key )
532- unless JSONAPI . configuration . raise_if_parameters_not_allowed
532+ if JSONAPI . configuration . raise_if_parameters_not_allowed
533+ fail JSONAPI ::Exceptions ::ParameterNotAllowed . new ( links_key )
534+ else
535+ params_not_allowed . push ( links_key )
533536 value . delete links_key
534537 end
535538 end
536539 end
537540 when 'attributes'
538541 value . each do |attr_key , attr_value |
539542 unless formatted_allowed_fields . include? ( attr_key . to_sym )
540- params_not_allowed . push ( attr_key )
541- unless JSONAPI . configuration . raise_if_parameters_not_allowed
543+ if JSONAPI . configuration . raise_if_parameters_not_allowed
544+ fail JSONAPI ::Exceptions ::ParameterNotAllowed . new ( attr_key )
545+ else
546+ params_not_allowed . push ( attr_key )
542547 value . delete attr_key
543548 end
544549 end
545550 end
546551 when 'type'
547552 when 'id'
548553 unless formatted_allowed_fields . include? ( :id )
549- params_not_allowed . push ( :id )
550- unless JSONAPI . configuration . raise_if_parameters_not_allowed
554+ if JSONAPI . configuration . raise_if_parameters_not_allowed
555+ fail JSONAPI ::Exceptions ::ParameterNotAllowed . new ( :id )
556+ else
557+ params_not_allowed . push ( :id )
551558 params . delete :id
552559 end
553560 end
554561 else
555- params_not_allowed . push ( key )
562+ if JSONAPI . configuration . raise_if_parameters_not_allowed
563+ fail JSONAPI ::Exceptions ::ParameterNotAllowed . new ( key )
564+ else
565+ params_not_allowed . push ( key )
566+ params . delete key
567+ end
556568 end
557569 end
558570
559571 if params_not_allowed . length > 0
560- if JSONAPI . configuration . raise_if_parameters_not_allowed
561- fail JSONAPI ::Exceptions ::ParametersNotAllowed . new ( params_not_allowed )
562- else
563- params_not_allowed_warnings = params_not_allowed . map do |key |
564- JSONAPI ::Warning . new ( code : JSONAPI ::PARAM_NOT_ALLOWED ,
565- title : 'Param not allowed' ,
566- detail : "#{ key } is not allowed." )
567- end
568- self . warnings . concat ( params_not_allowed_warnings )
572+ params_not_allowed_warnings = params_not_allowed . map do |param |
573+ JSONAPI ::Warning . new ( code : JSONAPI ::PARAM_NOT_ALLOWED ,
574+ title : 'Param not allowed' ,
575+ detail : "#{ param } is not allowed." )
569576 end
577+ self . warnings . concat ( params_not_allowed_warnings )
570578 end
571579 end
572580
0 commit comments