diff --git a/CHANGELOG.md b/CHANGELOG.md index 5600f30..5112928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,9 @@ #### Fixes +* [#388](https://github.com/ruby-grape/grape-entity/pull/388): Drop ruby-head from test matrix - [@numbata](https://github.com/numbata). +* [#384](https://github.com/ruby-grape/grape-entity/pull/384): Fix `inspect` to correctly handle `nil` values - [@fcce](https://github.com/fcce). * Your contribution here. -* [#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). ### ### 1.0.1 (2024-04-10) diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 796720a..9f01031 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -472,8 +472,13 @@ def presented # Prevent default serialization of :options or :delegator. def inspect - fields = serializable_hash.map { |k, v| "#{k}=#{v}" } - "#<#{self.class.name}:#{object_id} #{fields.join(' ')}>" + object = serializable_hash + if object.nil? + "#<#{self.class.name}:#{object_id} nil>" + else + fields = object.map { |k, v| "#{k}=#{v}" } + "#<#{self.class.name}:#{object_id} #{fields.join(' ')}>" + end end def initialize(object, options = {}) diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index 4b03770..42fe0f1 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -1778,6 +1778,11 @@ class NoPathCharacterEntity < Grape::Entity expect(data).to_not include '@options' expect(data).to_not include '@delegator' end + + it 'returns a nil string when subject is nil' do + data = subject.class.new(nil).inspect + expect(data).to include 'nil' + end end describe '#value_for' do