From b7500ec0d706a55b8fdb9543ed863f5798f8d0d8 Mon Sep 17 00:00:00 2001 From: Feng Ce Date: Sun, 28 Apr 2024 17:48:18 +0800 Subject: [PATCH 1/3] Update inspect method to handle nil serializable_hash --- lib/grape_entity/entity.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 2728b9f..f2f6f94 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(' ')}>" + hash = serializable_hash + if hash.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 = {}) From 7a44cb5189b2dacbe75d1cbea60573fed5e50bea Mon Sep 17 00:00:00 2001 From: Feng Ce Date: Mon, 29 Apr 2024 10:19:01 +0800 Subject: [PATCH 2/3] Refactor inspect method to use object instead of hash --- lib/grape_entity/entity.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index f2f6f94..4b53224 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -472,8 +472,8 @@ def presented # Prevent default serialization of :options or :delegator. def inspect - hash = serializable_hash - if hash.nil? + object = serializable_hash + if object.nil? "#<#{self.class.name}:#{object_id}> nil" else fields = object.map { |k, v| "#{k}=#{v}" } From a68b90d31ccc36b74b4a6445f4a22b81178d8937 Mon Sep 17 00:00:00 2001 From: Feng Ce Date: Mon, 6 May 2024 14:54:42 +0800 Subject: [PATCH 3/3] Add changelog and rspec --- CHANGELOG.md | 2 +- spec/grape_entity/entity_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d01b1..8135ea3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ #### Fixes * Your contribution here. - +* [#384](https://github.com/ruby-grape/grape-entity/pull/384): Fixed the `inspect` method to correctly handle nil values. - [@fcce](https://github.com/fcce). ### ### 1.0.1 (2024-04-10) diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index 466e4d7..6339ad4 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -1769,6 +1769,12 @@ 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