Skip to content

Commit 489f9b9

Browse files
fccedblocknumbata
authored
Update inspect method to handle nil serializable_hash (#384)
* Update inspect method to handle nil serializable_hash * Refactor inspect method to use object instead of hash * Add changelog and rspec * Update CHANGELOG.md * Fix rubocop offenses * Makes nil entity inspect consistent with the Ruby-style #<ClassName:object_id …> format --------- Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]> Co-authored-by: Andrei Subbota <[email protected]>
1 parent a68e17a commit 489f9b9

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
#### Fixes
88

9+
* [#388](https://github.com/ruby-grape/grape-entity/pull/388): Drop ruby-head from test matrix - [@numbata](https://github.com/numbata).
10+
* [#384](https://github.com/ruby-grape/grape-entity/pull/384): Fix `inspect` to correctly handle `nil` values - [@fcce](https://github.com/fcce).
911
* Your contribution here.
10-
* [#388](https://github.com/ruby-grape/grape-entity/pull/388): Drop ruby-head from test matrix to keep builds stable - [@numbata](https://github.com/numbata).
1112

1213
### ### 1.0.1 (2024-04-10)
1314

lib/grape_entity/entity.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,13 @@ def presented
472472

473473
# Prevent default serialization of :options or :delegator.
474474
def inspect
475-
fields = serializable_hash.map { |k, v| "#{k}=#{v}" }
476-
"#<#{self.class.name}:#{object_id} #{fields.join(' ')}>"
475+
object = serializable_hash
476+
if object.nil?
477+
"#<#{self.class.name}:#{object_id} nil>"
478+
else
479+
fields = object.map { |k, v| "#{k}=#{v}" }
480+
"#<#{self.class.name}:#{object_id} #{fields.join(' ')}>"
481+
end
477482
end
478483

479484
def initialize(object, options = {})

spec/grape_entity/entity_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,11 @@ class NoPathCharacterEntity < Grape::Entity
17781778
expect(data).to_not include '@options'
17791779
expect(data).to_not include '@delegator'
17801780
end
1781+
1782+
it 'returns a nil string when subject is nil' do
1783+
data = subject.class.new(nil).inspect
1784+
expect(data).to include 'nil'
1785+
end
17811786
end
17821787

17831788
describe '#value_for' do

0 commit comments

Comments
 (0)