Skip to content

Commit 374a021

Browse files
committed
Fix Time serialization regression introduced by dropping MultiJson
JSON.dump bypasses ActiveSupport's Hash#to_json override, causing Time values to serialize via Time#to_s ("2026-04-16 10:55:10 UTC") instead of ActiveSupport's as_json ("2026-04-16T10:55:10.597Z"). Replace Json.dump with Hash#to_json which properly delegates through ActiveSupport and remove the now-unused Grape::Entity::Json constant. Fixes #403
1 parent b32714d commit 374a021

4 files changed

Lines changed: 18 additions & 17 deletions

File tree

lib/grape_entity/entity.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'grape_entity/json'
4-
53
module Grape
64
# An Entity is a lightweight structure that allows you to easily
75
# represent data from your application in a consistent and abstracted
@@ -594,7 +592,7 @@ def is_defined_in_entity?(attribute)
594592

595593
def to_json(options = {})
596594
options = options.to_h if options&.respond_to?(:to_h)
597-
Grape::Entity::Json.dump(serializable_hash(options))
595+
serializable_hash(options).to_json
598596
end
599597

600598
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)