Skip to content

Revert dropping MultiJson to fix Time serialization regression#405

Merged
numbata merged 2 commits intomasterfrom
hotfix/revert-json-dump
Apr 17, 2026
Merged

Revert dropping MultiJson to fix Time serialization regression#405
numbata merged 2 commits intomasterfrom
hotfix/revert-json-dump

Conversation

@numbata
Copy link
Copy Markdown
Collaborator

@numbata numbata commented Apr 17, 2026

Summary

This reverts the behavioral changes from #385 ("Drop multijson dependency") which introduced a silent breaking change in 1.0.3: all Time fields in API responses changed format for applications using ActiveSupport.

  • Before (1.0.1): "2026-04-16T10:55:10.597Z" (ISO 8601)
  • After (1.0.3): "2026-04-16 10:55:10 UTC"

The goal is to restore the exact serialization behavior from 1.0.1 for a patch release, so affected consumers can upgrade immediately without changes on their side.

What went wrong

PR #385 replaced MultiJson.dump with JSON.dump (via a Grape::Entity::Json shim) in Entity#to_json. These two methods behave differently when ActiveSupport is loaded:

  • MultiJson.dump(hash) internally calls Hash#to_json, which ActiveSupport overrides to run as_json recursively — giving Time its ISO 8601 representation.
  • JSON.dump(hash) goes directly to Ruby's C extension JSON generator, bypassing ActiveSupport's override — so Time falls back to Time#to_s.

Since activesupport is a runtime dependency of grape-entity, every user was affected.

Additionally, the Grape::Entity::Json shim from #385 checked defined?(::MultiJson) at require time to decide which backend to use. For applications with gem "multi_json", require: false, MultiJson was never loaded at that point — so the constant always resolved to ::JSON, making the MultiJson detection dead code in practice (as noted by @stevenou in #403).

What this PR does

  • Restores require 'multi_json' and MultiJson.dump in Entity#to_json — the same code path that was in place at 1.0.1
  • Re-adds multi_json as a gemspec dependency
  • Removes the Grape::Entity::Json shim (lib/grape_entity/json.rb) and its spec, as they are no longer referenced
  • Adds regression tests that assert Time values serialize via as_json in both flat and nested exposures

A separate PR (#404) explores dropping MultiJson entirely without the regression by using Hash#to_json directly — that is a candidate for a future minor release.

Fixes #403

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
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 17, 2026

Danger Report

No issues found.

View run

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Reverts the MultiJson removal from #385 to restore the pre-1.0.3 Time serialization behavior (ISO 8601 via ActiveSupport’s as_json) and prevent silent breaking changes in API responses.

Changes:

  • Restored MultiJson.dump for Grape::Entity#to_json and reintroduced multi_json as a runtime dependency.
  • Removed the Grape::Entity::Json shim and its spec since it is no longer used.
  • Added regression specs to assert Time serialization via as_json for both flat and nested exposures.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
spec/grape_entity/json_spec.rb Removes spec for the deleted Grape::Entity::Json shim.
spec/grape_entity/entity_spec.rb Adds regression coverage for Time serialization (flat + nested).
lib/grape_entity/json.rb Deletes the no-longer-used JSON backend shim.
lib/grape_entity/entity.rb Restores MultiJson.dump in #to_json and requires multi_json.
grape-entity.gemspec Re-adds multi_json as a dependency to support restored behavior.
CHANGELOG.md Documents the regression fix/revert for the upcoming release.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@numbata numbata merged commit a4443d4 into master Apr 17, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dropping MultiJson in 1.0.3 broke Time serialization for apps using ActiveSupport

2 participants