Skip to content

Fix Resource#to_h returning nil instead of matching #to_hash - #319

Merged
dblock merged 1 commit into
codegram:masterfrom
dblock:dblock/fix-resource-to-h
Aug 26, 2026
Merged

Fix Resource#to_h returning nil instead of matching #to_hash#319
dblock merged 1 commit into
codegram:masterfrom
dblock:dblock/fix-resource-to-h

Conversation

@dblock

@dblock dblock commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Description

Closes #103.

resource.to_hash # => {"title" => "Order"}
resource.to_h    # => nil

Root cause

Resource#to_h/#to_hash are not explicitly defined; they were both routed through method_missing, which delegates to _attributes (a Collection, which does define both #to_h and #to_hash) — but only if the method isn't already defined on Array:

elsif !Array.method_defined?(method)
  %i[_attributes _embedded _links].each do |target|
    target = send(target)
    return target.send(method, *args, &block) if target.respond_to?(method.to_s)
  end
  super
end

Array defines #to_h but not #to_hash:

Array.method_defined?(:to_h)     # => true
Array.method_defined?(:to_hash)  # => false

So #to_hash correctly falls through the guard and delegates to _attributes.to_hash, but #to_h gets blocked by the guard, and method_missing's elsif branch simply does nothing in that case — returning nil instead of raising or delegating.

Fix

Explicitly define #to_h on Resource, delegating to _attributes.to_h, and alias #to_hash to it — removing the need to rely on the Array.method_defined? heuristic for this specific pair of methods.

Checklist

  • I have added tests to cover my changes.
  • I have added an entry to the changelog.

@github-actions

github-actions Bot commented Aug 26, 2026

Copy link
Copy Markdown

Danger Report

No issues found.

View run

@coveralls

coveralls commented Aug 26, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — dblock:dblock/fix-resource-to-h into codegram:master

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dblock
dblock force-pushed the dblock/fix-resource-to-h branch from 6692df4 to f47b4fe Compare August 26, 2026 21:48
@dblock
dblock merged commit 2333ddd into codegram:master Aug 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hyperclient::Resource#to_h != Hyperclient::Resource#to_hash

2 participants