Skip to content

Commit 3f76cfa

Browse files
brianswkovalscion
authored andcommitted
Fix a bug with single item arrays (#124)
Getting sent to has-one relationship authorizers
1 parent 5e0a441 commit 3f76cfa

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

lib/jsonapi/authorization/authorizing_processor.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def related_models_with_context
315315
when Hash # polymorphic relationship
316316
resource_class = @resource_klass.resource_for(assoc_value[:type].to_s)
317317
resource_class.find_by_key(assoc_value[:id], context: context)._model
318-
else
318+
when Array
319319
resource_class = resource_class_for_relationship(assoc_name)
320320
resources = resource_class.find_by_keys(assoc_value, context: context)
321321
resources.map(&:_model).tap do |scoped_records|
@@ -324,6 +324,9 @@ def related_models_with_context
324324
fail JSONAPI::Exceptions::RecordNotFound, related_ids
325325
end
326326
end
327+
else
328+
resource_class = resource_class_for_relationship(assoc_name)
329+
resource_class.find_by_key(assoc_value, context: context)._model
327330
end
328331

329332
{

spec/requests/included_resources_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@
447447
{
448448
relation_type: :to_one,
449449
relation_name: :author,
450-
records: [existing_author]
450+
records: existing_author
451451
},
452452
{
453453
relation_type: :to_many,

spec/requests/tricky_operations_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
[{
4242
relation_name: :article,
4343
relation_type: :to_one,
44-
records: [article]
44+
records: article
4545
}]
4646
end
4747

0 commit comments

Comments
 (0)