diff --git a/CHANGELOG.md b/CHANGELOG.md index be905aa..2d4e2c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index 3b7a40b..1952b02 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -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 @@ -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] @@ -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) @@ -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 diff --git a/test/hyperclient/link_test.rb b/test/hyperclient/link_test.rb index 6f88b18..97f2871 100644 --- a/test/hyperclient/link_test.rb +++ b/test/hyperclient/link_test.rb @@ -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)