Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#### Fixes

* Your contribution here.

* [#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)

Expand Down
4 changes: 2 additions & 2 deletions bench/serializing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Teacher < Models::Person
attr_accessor :tenure

def initialize(opts = {})
super(opts)
super
@tenure = opts[:tenure]
end
end
Expand All @@ -44,7 +44,7 @@ class Student < Models::Person
attr_reader :grade

def initialize(opts = {})
super(opts)
super
@grade = opts[:grade]
end
end
Expand Down
2 changes: 1 addition & 1 deletion grape-entity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
Expand Down
9 changes: 7 additions & 2 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down
5 changes: 5 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down