From 209043b806fee8805be775147591237e3d7f2793 Mon Sep 17 00:00:00 2001 From: Brian Ko Date: Thu, 28 Mar 2019 23:23:35 -0700 Subject: [PATCH] Fix a bug with single item arrays Getting sent to has-one relationship authorizers --- lib/jsonapi/authorization/authorizing_processor.rb | 5 ++++- spec/requests/included_resources_spec.rb | 2 +- spec/requests/tricky_operations_spec.rb | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/jsonapi/authorization/authorizing_processor.rb b/lib/jsonapi/authorization/authorizing_processor.rb index 0a92d528..4728bb44 100644 --- a/lib/jsonapi/authorization/authorizing_processor.rb +++ b/lib/jsonapi/authorization/authorizing_processor.rb @@ -315,7 +315,7 @@ def related_models_with_context when Hash # polymorphic relationship resource_class = @resource_klass.resource_for(assoc_value[:type].to_s) resource_class.find_by_key(assoc_value[:id], context: context)._model - else + when Array resource_class = resource_class_for_relationship(assoc_name) resources = resource_class.find_by_keys(assoc_value, context: context) resources.map(&:_model).tap do |scoped_records| @@ -324,6 +324,9 @@ def related_models_with_context fail JSONAPI::Exceptions::RecordNotFound, related_ids end end + else + resource_class = resource_class_for_relationship(assoc_name) + resource_class.find_by_key(assoc_value, context: context)._model end { diff --git a/spec/requests/included_resources_spec.rb b/spec/requests/included_resources_spec.rb index c3d46440..fc949ad0 100644 --- a/spec/requests/included_resources_spec.rb +++ b/spec/requests/included_resources_spec.rb @@ -447,7 +447,7 @@ { relation_type: :to_one, relation_name: :author, - records: [existing_author] + records: existing_author }, { relation_type: :to_many, diff --git a/spec/requests/tricky_operations_spec.rb b/spec/requests/tricky_operations_spec.rb index b0d81012..0ef1cf0e 100644 --- a/spec/requests/tricky_operations_spec.rb +++ b/spec/requests/tricky_operations_spec.rb @@ -41,7 +41,7 @@ [{ relation_name: :article, relation_type: :to_one, - records: [article] + records: article }] end