From b7500ec0d706a55b8fdb9543ed863f5798f8d0d8 Mon Sep 17 00:00:00 2001 From: Feng Ce Date: Sun, 28 Apr 2024 17:48:18 +0800 Subject: [PATCH 1/6] 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/6] 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/6] 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 From b1501cb749e9b158026ea54300307fead4c3f9d9 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Sun, 18 May 2025 08:54:25 -0400 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8135ea3..0228dda 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). +* [#384](https://github.com/ruby-grape/grape-entity/pull/384): Fix `inspect` to correctly handle `nil` values - [@fcce](https://github.com/fcce). ### ### 1.0.1 (2024-04-10) From ec7136a4eb4998167b63ca84ea0bd00b9f2a705d Mon Sep 17 00:00:00 2001 From: Andrei Subbota Date: Mon, 19 May 2025 22:38:03 +0200 Subject: [PATCH 5/6] Fix rubocop offenses --- bench/serializing.rb | 4 ++-- grape-entity.gemspec | 4 ++-- spec/grape_entity/entity_spec.rb | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/bench/serializing.rb b/bench/serializing.rb index 6078654..6ffa256 100644 --- a/bench/serializing.rb +++ b/bench/serializing.rb @@ -35,7 +35,7 @@ class Teacher < Models::Person attr_accessor :tenure def initialize(opts = {}) - super(opts) + super @tenure = opts[:tenure] end end @@ -44,7 +44,7 @@ class Student < Models::Person attr_reader :grade def initialize(opts = {}) - super(opts) + super @grade = opts[:grade] end end diff --git a/grape-entity.gemspec b/grape-entity.gemspec index f6fc2be..8d0ff7c 100644 --- a/grape-entity.gemspec +++ b/grape-entity.gemspec @@ -16,9 +16,9 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' - s.add_runtime_dependency 'activesupport', '>= 3.0.0' + s.add_dependency 'activesupport', '>= 3.0.0' # FIXME: remove dependecy - s.add_runtime_dependency 'multi_json', '>= 1.3.2' + s.add_dependency 'multi_json', '>= 1.3.2' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec}/*`.split("\n") diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index 6339ad4..796cf97 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -1774,7 +1774,6 @@ class NoPathCharacterEntity < Grape::Entity data = subject.class.new(nil).inspect expect(data).to include 'nil' end - end describe '#value_for' do From a8b592f2d8fb0049f222d7a85158e6862ff8ab2c Mon Sep 17 00:00:00 2001 From: Andrei Subbota Date: Thu, 22 May 2025 20:07:30 +0200 Subject: [PATCH 6/6] =?UTF-8?q?Makes=20nil=20entity=20inspect=20consistent?= =?UTF-8?q?=20with=20the=20Ruby-style=20#=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/grape_entity/entity.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index d38bf53..9f01031 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -474,7 +474,7 @@ def presented def inspect object = serializable_hash if object.nil? - "#<#{self.class.name}:#{object_id}> nil" + "#<#{self.class.name}:#{object_id} nil>" else fields = object.map { |k, v| "#{k}=#{v}" } "#<#{self.class.name}:#{object_id} #{fields.join(' ')}>"