rails / jbuilder

Jbuilder: generate JSON objects with a Builder-style DSL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ActiveRecord objects/collection rendering as a string, instead of json

bborn opened this issue · comments

Upgrading from 2.8.0 to 2.9.0 when I ran into this. In 2.8.0:

json.calendar @event.calendar

renders

{
  "calendar": {
    "id": 1,
    "name": "Foo"
  }
}

But in 2.8.0 (with no other changes), I get:

{
  "calendar": "#<Calendar:0x007f89f4c034a8>"
}

And I'm seeing a similar thing when trying to render a collection of ActiveRecord objects as json.

Am I doing something wrong here, or did this break with the version change?

I'm experiencing what I think is the same issue with dates:

json.extract! @project, :created_at

json.created_at_2 @project.created_at.as_json

yields:

{  
  "created_at": "2019-04-15 08:50:12 -0700",
  "created_at_2": "2019-04-15T08:50:12.906-07:00"
}

I would expect these to be the same but it seems that to_s is being used instead of as_json with extract!

What versions of Ruby and Rails are you using?

I’m on Ruby 2.5.5, Rails 6.0.0.rc1

Seeing some issues with dates/times as well:

> Time.zone
=> #<ActiveSupport::TimeZone:0x00007fd58888a6e8
 @name="Auckland",
 @tzinfo=#<TZInfo::DataTimezone: Pacific/Auckland>,
 @utc_offset=nil>
> time = Time.zone.now
=> Tue, 14 May 2019 14:24:43 NZST +12:00
> JSON.dump(time: time)
=> "{\"time\":\"2019-05-14 14:24:43 +1200\"}"
> MultiJson.dump(time: time)
=> "{\"time\":\"2019-05-14 14:24:43\"}"

Note the difference in the serialization format.

Ruby 2.6.2, Rails 5.2.3

Can you please share which adapter MultiJson reports from a console?

MultiJson.adapter

@georgeclaghorn

> MultiJson.adapter
=> MultiJson::Adapters::JsonGem

Same ☝

commented

The same issue

MultiJson.adapter
=> MultiJson::Adapters::JsonGem
Ruby 2.5.5 & Rails 5.2.3

Me:

Ruby 2.4.0 & Rails 2.5.3

> MultiJson.adapter
=> MultiJson::Adapters::Oj

Is anyone willing to test against e2e8623 before I release 2.9.1?

@georgeclaghorn works for me with e2e8623 👍

e2e8623 works for us as well.

 MultiJson.adapter
# => MultiJson::Adapters::JsonGem
  • Ruby 2.6.2p47

  • Rails 5.2.3

@georgeclaghorn that commit sha1 fixes the tests that started failing when we updated to 2.9.0 from 2.8.0

diff --git a/Gemfile b/Gemfile
index 25f9d2de..4f4abc77 100644
--- a/Gemfile
+++ b/Gemfile
@@ -2,6 +2,10 @@ source 'https://rubygems.org'

 gemspec

+
+gem "jbuilder", :github => "rails/jbuilder", :ref => "e2e8623b08078ad6a2323ce8ecaf642b7afe"
+

Ruby 2.4.5, rails 5.0.7.2

commented

Confirm that e2e8623 works for me

Thanks for testing. I just released v2.9.1 with that change.

@georgeclaghorn Many thanks for resolving the issue so quickly. Sorry I couldn’t help testing, in my timezone it was night time.

Agreed, thanks very much!

Hi, I am migrating from ruby 2.7.4 to 3.0.2 and I encounter a similar problem (Rails 6.0.3.4).

  • Ruby 2.7.4 - json.createdAt something.created_at yields "2021-11-22T16:35:25+08:00"
  • Ruby 3.0.2 - json.createdAt something.created_at yields "2021-11-22 16:35:25 +0800"

A quick workaround for this is to use something.created_at.iso8601 but this doesnt seem to be the right way as I need to add it everywhere.