@@ -392,9 +392,7 @@ def parse_params(params, allowed_fields)
392392 when 'relationships'
393393 value . each do |link_key , link_value |
394394 param = unformat_key ( link_key )
395-
396395 association = @resource_klass . _association ( param )
397-
398396 if association . is_a? ( JSONAPI ::Association ::HasOne )
399397 if link_value . nil?
400398 linkage = nil
@@ -404,12 +402,17 @@ def parse_params(params, allowed_fields)
404402
405403 links_object = parse_has_one_links_object ( linkage )
406404 if !association . polymorphic? && links_object [ :type ] && ( links_object [ :type ] . to_s != association . type . to_s )
407- raise JSONAPI ::Exceptions ::TypeMismatch . new ( links_object [ :type ] )
405+ fail JSONAPI ::Exceptions ::TypeMismatch . new ( links_object [ :type ] )
408406 end
409407
410408 unless links_object [ :id ] . nil?
411409 association_resource = Resource . resource_for ( @resource_klass . module_path + unformat_key ( links_object [ :type ] ) . to_s )
412- checked_has_one_associations [ param ] = association_resource . verify_key ( links_object [ :id ] , @context )
410+ association_id = association_resource . verify_key ( links_object [ :id ] , @context )
411+ if association . polymorphic?
412+ checked_has_one_associations [ param ] = { id : association_id , type : unformat_key ( links_object [ :type ] . to_s ) }
413+ else
414+ checked_has_one_associations [ param ] = association_id
415+ end
413416 else
414417 checked_has_one_associations [ param ] = nil
415418 end
@@ -426,12 +429,12 @@ def parse_params(params, allowed_fields)
426429
427430 # Since we do not yet support polymorphic associations we will raise an error if the type does not match the
428431 # association's type.
429- # TODO : Support Polymorphic associations
432+ # ToDo : Support Polymorphic associations
430433
431434 if links_object . length == 0
432435 checked_has_many_associations [ param ] = [ ]
433436 else
434- if links_object . length > 1 || !links_object . key ?( unformat_key ( association . type ) . to_s )
437+ if links_object . length > 1 || !links_object . has_key ?( unformat_key ( association . type ) . to_s )
435438 fail JSONAPI ::Exceptions ::TypeMismatch . new ( links_object [ :type ] )
436439 end
437440
@@ -519,19 +522,19 @@ def parse_update_association_operation(data, association_type, parent_key)
519522 association = resource_klass . _association ( association_type )
520523 if association . is_a? ( JSONAPI ::Association ::HasOne )
521524 if association . polymorphic?
522- object_params = { relationships : { format_key ( association . name ) => { data : data } } }
525+ object_params = { relationships : { format_key ( association . name ) => { data : data } } }
523526 verified_param_set = parse_params ( object_params , updatable_fields )
524527
525528 @operations . push JSONAPI ::ReplacePolymorphicHasOneAssociationOperation . new (
526529 resource_klass ,
527530 context : @context ,
528531 resource_id : parent_key ,
529532 association_type : association_type ,
530- key_value : verified_param_set [ :has_one ] . values [ 0 ] ,
531- key_type : data [ ' type' ]
533+ key_value : verified_param_set [ :has_one ] . values [ 0 ] [ :id ] ,
534+ key_type : verified_param_set [ :has_one ] . values [ 0 ] [ : type]
532535 )
533536 else
534- object_params = { relationships : { format_key ( association . name ) => { data : data } } }
537+ object_params = { relationships : { format_key ( association . name ) => { data : data } } }
535538 verified_param_set = parse_params ( object_params , updatable_fields )
536539
537540 @operations . push JSONAPI ::ReplaceHasOneAssociationOperation . new (
@@ -542,7 +545,7 @@ def parse_update_association_operation(data, association_type, parent_key)
542545 key_value : verified_param_set [ :has_one ] . values [ 0 ]
543546 )
544547 end
545- else
548+ elsif association . is_a? ( JSONAPI :: Association :: HasMany )
546549 unless association . acts_as_set
547550 fail JSONAPI ::Exceptions ::HasManySetReplacementForbidden . new
548551 end
0 commit comments