Skip to content

Commit

Permalink
Fix a bug with single item arrays (#124)
Browse files Browse the repository at this point in the history
Getting sent to has-one relationship authorizers
  • Loading branch information
brianswko authored and valscion committed Mar 29, 2019
1 parent 5e0a441 commit 3f76cfa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/jsonapi/authorization/authorizing_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

{
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/included_resources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@
{
relation_type: :to_one,
relation_name: :author,
records: [existing_author]
records: existing_author
},
{
relation_type: :to_many,
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/tricky_operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
[{
relation_name: :article,
relation_type: :to_one,
records: [article]
records: article
}]
end

Expand Down

0 comments on commit 3f76cfa

Please sign in to comment.