From 36cc4686dab13fe587395fe8a0e26c6dbeecbb4a Mon Sep 17 00:00:00 2001 From: Andrey Subbota Date: Fri, 17 Apr 2026 06:03:21 +0200 Subject: [PATCH 1/2] Revert dropping MultiJson in favor of JSON.dump (#385) PR #385 replaced MultiJson.dump with JSON.dump which bypasses ActiveSupport's Hash#to_json override, causing Time values to serialize as "2026-04-16 10:55:10 UTC" instead of ISO 8601. Restore direct multi_json dependency and MultiJson.dump call. Remove the intermediate Grape::Entity::Json constant introduced by #385 as it is no longer needed. Fixes #403 --- grape-entity.gemspec | 1 + lib/grape_entity/entity.rb | 4 ++-- lib/grape_entity/json.rb | 7 ------- spec/grape_entity/entity_spec.rb | 17 +++++++++++++++++ spec/grape_entity/json_spec.rb | 7 ------- 5 files changed, 20 insertions(+), 16 deletions(-) delete mode 100644 lib/grape_entity/json.rb delete mode 100644 spec/grape_entity/json_spec.rb diff --git a/grape-entity.gemspec b/grape-entity.gemspec index b6b882b..5b158df 100644 --- a/grape-entity.gemspec +++ b/grape-entity.gemspec @@ -25,6 +25,7 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.add_dependency 'activesupport', '>= 3.0.0' + s.add_dependency 'multi_json', '>= 1.0' s.files = Dir['lib/**/*.rb', 'CHANGELOG.md', 'LICENSE', 'README.md'] s.require_paths = ['lib'] diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 2951b0f..cd14033 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'grape_entity/json' +require 'multi_json' module Grape # An Entity is a lightweight structure that allows you to easily @@ -594,7 +594,7 @@ def is_defined_in_entity?(attribute) def to_json(options = {}) options = options.to_h if options&.respond_to?(:to_h) - Grape::Entity::Json.dump(serializable_hash(options)) + MultiJson.dump(serializable_hash(options)) end def to_xml(options = {}) diff --git a/lib/grape_entity/json.rb b/lib/grape_entity/json.rb deleted file mode 100644 index a57278f..0000000 --- a/lib/grape_entity/json.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -module Grape - class Entity - Json = defined?(::MultiJson) ? ::MultiJson : ::JSON - end -end diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index f024217..5bdd73e 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -1880,6 +1880,23 @@ class NoPathCharacterEntity < Grape::Entity it 'returns a json' do expect(fresh_class.new(model).to_json).to eq(JSON.generate(attributes.slice(:name))) end + + it 'serializes Time values via as_json' do + fresh_class.expose :birthday + entity = fresh_class.new(model) + json = entity.to_json + parsed = JSON.parse(json) + expect(parsed['birthday']).to eq(attributes[:birthday].as_json) + end + + it 'serializes Time values in nested exposures via as_json' do + fresh_class.expose :details do + fresh_class.expose :birthday + end + entity = fresh_class.new(model) + parsed = JSON.parse(entity.to_json) + expect(parsed['details']['birthday']).to eq(attributes[:birthday].as_json) + end end describe '#inspect' do diff --git a/spec/grape_entity/json_spec.rb b/spec/grape_entity/json_spec.rb deleted file mode 100644 index b57825f..0000000 --- a/spec/grape_entity/json_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -describe Grape::Entity::Json do - subject { described_class } - - it { is_expected.to eq(JSON) } -end From 1a88c6b2b617c54ac81254422ac69d0392d7414c Mon Sep 17 00:00:00 2001 From: Andrey Subbota Date: Fri, 17 Apr 2026 06:10:58 +0200 Subject: [PATCH 2/2] Add CHANGELOG entry for #405 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2847b16..adb7517 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ #### Fixes * Your contribution here. +* [#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). * [#402](https://github.com/ruby-grape/grape-entity/pull/402): Remove `Json::ParseError` assignment - [@olivier-thatch](https://github.com/olivier-thatch). ### 1.0.3 (2026-04-15)