Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [#333](https://github.com/codegram/hyperclient/pull/333): Updated danger-pr-comment to 0.2.0 and fixed the hash conversion assertion for Minitest 6.1 - [@dblock](https://github.com/dblock).
* [#334](https://github.com/codegram/hyperclient/pull/334): Added `Link#_query` to send query parameters that a link doesn't declare - [@dblock](https://github.com/dblock).
* [#336](https://github.com/codegram/hyperclient/pull/336): Fixed `SystemStackError` when iterating a link whose resource links back to itself under the same name, raising `Hyperclient::SelfReferencingLinkError` instead - [@dblock](https://github.com/dblock).
* Your contribution here.

### 2.1.0 (2026/10/14)
Expand Down
22 changes: 21 additions & 1 deletion lib/hyperclient/link.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require 'addressable'

module Hyperclient
# Public: Exception that is raised when iterating a link whose resource
# links back to itself under the same name, which would otherwise recurse
# forever.
class SelfReferencingLinkError < StandardError
def initialize(key, url)
super("The #{key} link at #{url} links back to itself, use _embedded to iterate over embedded items.")
end
end

# Internal: The Link is used to let a Resource interact with the API.
#
class Link
Expand Down Expand Up @@ -33,6 +42,8 @@ def each(&block)
current = self
while current
coll = current.respond_to?(@key) ? current.send(@key) : _resource
raise SelfReferencingLinkError.new(@key, current._url) if current.self_referencing?(coll)

coll.each(&block)
break unless current._links[:next]

Expand Down Expand Up @@ -172,6 +183,7 @@ def delegate_method(method, *args, &block)
return unless @key && _resource.respond_to?(@key)

@delegate ||= _resource.send(@key)
return if self_referencing?(@delegate)
return unless @delegate&.respond_to?(method.to_s)

@delegate.send(method, *args, &block)
Expand All @@ -180,13 +192,21 @@ def delegate_method(method, *args, &block)
# Internal: Accessory method to allow the link respond to the
# methods that will hit method_missing.
def respond_to_missing?(method, _include_private = false)
if @key && _resource.respond_to?(@key) && (delegate = _resource.send(@key)) && delegate.respond_to?(method.to_s)
if @key && _resource.respond_to?(@key) && (delegate = _resource.send(@key)) &&
!self_referencing?(delegate) && delegate.respond_to?(method.to_s)
true
else
_resource.respond_to?(method.to_s)
end
end

# Internal: Indicates if the given object is a Link to this same URL,
# following which would recurse forever.
def self_referencing?(other)
other.is_a?(Link) && other._url == _url
end
protected :self_referencing?

# Internal: avoid delegating to resource
#
# #to_ary is called for implicit array coercion (such as parallel assignment
Expand Down
41 changes: 41 additions & 0 deletions test/hyperclient/link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,47 @@ module Hyperclient
_(ids).must_equal [1, 2]
end

it 'raises a clear error when the resource links back to itself under the same name' do
resource = Resource.new({ '_links' => { 'sub_nodes' => { 'href' => '/sub_nodes' } } }, entry_point)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/sub_nodes') do
[200, {}, { '_links' => { 'sub_nodes' => { 'href' => 'http://api.example.org/sub_nodes' } },
'_embedded' => { 'nodes' => [{ 'name' => 'a' }] } }]
end
end

error = _(-> { resource._links.sub_nodes.map(&:name) }).must_raise Hyperclient::SelfReferencingLinkError
_(error.message).must_equal 'The sub_nodes link at http://api.example.org/sub_nodes links back to itself, ' \
'use _embedded to iterate over embedded items.'
_(resource._links.sub_nodes._embedded.nodes.map(&:name)).must_equal ['a']
end

it 'does not follow a link back to itself when checking respond_to? or delegating' do
resource = Resource.new({ '_links' => { 'sub_nodes' => { 'href' => '/sub_nodes' } } }, entry_point)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/sub_nodes') do
[200, {}, { 'name' => nil,
'_links' => { 'sub_nodes' => { 'href' => 'http://api.example.org/sub_nodes' } } }]
end
end

_(resource._links.sub_nodes.respond_to?(:unknown)).must_equal false
_(resource._links.sub_nodes.name).must_be_nil
end

it 'iterates a link that points to a different url with the same name' do
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/orders') { [200, {}, { '_links' => { 'orders' => { 'href' => '/all_orders' } } }] }
stub.get('http://api.example.org/all_orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] }
end

_(resource._links.orders.map(&:id)).must_equal [1]
end

it 'returns an Enumerator when called without a block' do
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)

Expand Down
Loading