Skip to content

Commit a4443d4

Browse files
authored
Revert dropping MultiJson to fix Time serialization regression (#405)
JSON.dump bypasses ActiveSupport's Hash#to_json override, causing Time values to serialize as "2026-04-16 10:55:10 UTC" instead of ISO 8601 format. Restore MultiJson.dump to match 1.0.1 behavior. Fixes #403
1 parent b32714d commit a4443d4

File tree

6 files changed

+21
-16
lines changed

6 files changed

+21
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#405](https://github.com/ruby-grape/grape-entity/pull/405): Fix `Time` serialization regression by reverting #385 and restoring `MultiJson.dump` - [@numbata](https://github.com/numbata).
1011
* [#402](https://github.com/ruby-grape/grape-entity/pull/402): Remove `Json::ParseError` assignment - [@olivier-thatch](https://github.com/olivier-thatch).
1112

1213
### 1.0.3 (2026-04-15)

grape-entity.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
2525
s.required_ruby_version = '>= 3.0'
2626

2727
s.add_dependency 'activesupport', '>= 3.0.0'
28+
s.add_dependency 'multi_json', '>= 1.0'
2829

2930
s.files = Dir['lib/**/*.rb', 'CHANGELOG.md', 'LICENSE', 'README.md']
3031
s.require_paths = ['lib']

lib/grape_entity/entity.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require 'grape_entity/json'
3+
require 'multi_json'
44

55
module Grape
66
# An Entity is a lightweight structure that allows you to easily
@@ -594,7 +594,7 @@ def is_defined_in_entity?(attribute)
594594

595595
def to_json(options = {})
596596
options = options.to_h if options&.respond_to?(:to_h)
597-
Grape::Entity::Json.dump(serializable_hash(options))
597+
MultiJson.dump(serializable_hash(options))
598598
end
599599

600600
def to_xml(options = {})

lib/grape_entity/json.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

spec/grape_entity/entity_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,23 @@ class NoPathCharacterEntity < Grape::Entity
18801880
it 'returns a json' do
18811881
expect(fresh_class.new(model).to_json).to eq(JSON.generate(attributes.slice(:name)))
18821882
end
1883+
1884+
it 'serializes Time values via as_json' do
1885+
fresh_class.expose :birthday
1886+
entity = fresh_class.new(model)
1887+
json = entity.to_json
1888+
parsed = JSON.parse(json)
1889+
expect(parsed['birthday']).to eq(attributes[:birthday].as_json)
1890+
end
1891+
1892+
it 'serializes Time values in nested exposures via as_json' do
1893+
fresh_class.expose :details do
1894+
fresh_class.expose :birthday
1895+
end
1896+
entity = fresh_class.new(model)
1897+
parsed = JSON.parse(entity.to_json)
1898+
expect(parsed['details']['birthday']).to eq(attributes[:birthday].as_json)
1899+
end
18831900
end
18841901

18851902
describe '#inspect' do

spec/grape_entity/json_spec.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)