@@ -357,19 +357,19 @@ def creatable_fields
357357 end
358358 # :nocov:
359359
360- def parse_add_operation ( data )
361- Array . wrap ( data ) . each do |params |
362- verify_type ( params [ :type ] )
360+ def parse_add_operation ( params )
361+ fail JSONAPI ::Exceptions ::InvalidDataFormat unless params . respond_to? ( :each_pair )
363362
364- data = parse_params ( params , creatable_fields )
365- @operations . push JSONAPI ::Operation . new ( :create_resource ,
366- @resource_klass ,
367- context : @context ,
368- data : data ,
369- fields : @fields ,
370- include_directives : @include_directives
371- )
372- end
363+ verify_type ( params [ :type ] )
364+
365+ data = parse_params ( params , creatable_fields )
366+ @operations . push JSONAPI ::Operation . new ( :create_resource ,
367+ @resource_klass ,
368+ context : @context ,
369+ data : data ,
370+ fields : @fields ,
371+ include_directives : @include_directives
372+ )
373373 rescue JSONAPI ::Exceptions ::Error => e
374374 @errors . concat ( e . errors )
375375 end
@@ -619,6 +619,8 @@ def parse_update_relationship_operation(verified_params, relationship, parent_ke
619619 end
620620
621621 def parse_single_replace_operation ( data , keys , id_key_presence_check_required : true )
622+ fail JSONAPI ::Exceptions ::InvalidDataFormat unless data . respond_to? ( :each_pair )
623+
622624 fail JSONAPI ::Exceptions ::MissingKey . new if data [ :id ] . nil?
623625
624626 key = data [ :id ] . to_s
@@ -641,31 +643,16 @@ def parse_single_replace_operation(data, keys, id_key_presence_check_required: t
641643 end
642644
643645 def parse_replace_operation ( data , keys )
644- if data . is_a? ( Array )
645- fail JSONAPI ::Exceptions ::CountMismatch if keys . count != data . count
646-
647- data . each do |object_params |
648- parse_single_replace_operation ( object_params , keys )
649- end
650- else
651- parse_single_replace_operation ( data , [ keys ] ,
652- id_key_presence_check_required : keys . present? )
653- end
654-
646+ parse_single_replace_operation ( data , [ keys ] , id_key_presence_check_required : keys . present? )
655647 rescue JSONAPI ::Exceptions ::Error => e
656648 @errors . concat ( e . errors )
657649 end
658650
659651 def parse_remove_operation ( params )
660- keys = parse_key_array ( params . require ( :id ) )
661-
662- keys . each do |key |
663- @operations . push JSONAPI ::Operation . new ( :remove_resource ,
664- @resource_klass ,
665- context : @context ,
666- resource_id : key
667- )
668- end
652+ @operations . push JSONAPI ::Operation . new ( :remove_resource ,
653+ @resource_klass ,
654+ context : @context ,
655+ resource_id : @resource_klass . verify_key ( params . require ( :id ) , context ) )
669656 rescue JSONAPI ::Exceptions ::Error => e
670657 @errors . concat ( e . errors )
671658 end
@@ -678,25 +665,15 @@ def parse_remove_relationship_operation(params, relationship, parent_key)
678665 )
679666
680667 if relationship . is_a? ( JSONAPI ::Relationship ::ToMany )
668+ operation_args = operation_base_args . dup
681669 keys = params [ :to_many ] . values [ 0 ]
682- keys . each do |key |
683- operation_args = operation_base_args . dup
684- operation_args [ 1 ] = operation_args [ 1 ] . merge ( associated_key : key )
685- @operations . push JSONAPI ::Operation . new ( :remove_to_many_relationship ,
686- *operation_args
687- )
688- end
670+ operation_args [ 1 ] = operation_args [ 1 ] . merge ( associated_keys : keys )
671+ @operations . push JSONAPI ::Operation . new ( :remove_to_many_relationships , *operation_args )
689672 else
690- @operations . push JSONAPI ::Operation . new ( :remove_to_one_relationship ,
691- *operation_base_args
692- )
673+ @operations . push JSONAPI ::Operation . new ( :remove_to_one_relationship , *operation_base_args )
693674 end
694675 end
695676
696- def parse_key_array ( raw )
697- @resource_klass . verify_keys ( raw . split ( /,/ ) , context )
698- end
699-
700677 def format_key ( key )
701678 @key_formatter . format ( key )
702679 end
0 commit comments