diff --git a/lib/jira/base.rb b/lib/jira/base.rb index 8f98cf05..e63b204d 100644 --- a/lib/jira/base.rb +++ b/lib/jira/base.rb @@ -475,17 +475,7 @@ def maybe_nested_attribute(attribute_name, nested_under = nil) end def self.maybe_nested_attribute(attributes, attribute_name, nested_under = nil) - return attributes[attribute_name] if nested_under.nil? - if nested_under.instance_of? Array - final = nested_under.inject(attributes) do |parent, key| - break if parent.nil? - parent[key] - end - return nil if final.nil? - final[attribute_name] - else - return attributes[nested_under][attribute_name] - end + return attributes.dig(*[*nested_under, attribute_name]) end def url_with_query_params(url, query_params) diff --git a/spec/jira/base_spec.rb b/spec/jira/base_spec.rb index 26749bd5..83f38ced 100644 --- a/spec/jira/base_spec.rb +++ b/spec/jira/base_spec.rb @@ -494,6 +494,11 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc: end end + it 'returns an empty collection for empty nested has_many relationships' do + subject = JIRA::Resource::HasManyExample.new(client) + expect(subject.brunchmuffins.length).to eq(0) + end + it 'allows it to be deeply nested' do subject = JIRA::Resource::HasManyExample.new(client, attrs: { 'nested' => { 'breakfastscone' => { 'breakfastscones' => [{ 'id' => '123' }, { 'id' => '456' }] }