ccocchi / rabl-rails

Rails 4.2+ templating system with JSON, XML and Plist support.
MIT License
209 stars 51 forks source link

Object root not included in 0.4.2, but was in 0.4.1 #82

Closed javierjulio closed 6 years ago

javierjulio commented 8 years ago

When upgrading rabl-rails from 0.4.1 to 0.4.2 we noticed that in our staging environment the object root is no longer included but it was in 0.4.1. We use latest responders gem with rabl-rails and are on Rails 4.2.6. Here's a sample RABL file from our API that demonstrates the issue:

object :@user

extends 'api/v1/users/user'

node(:total_credits) do |user|
  user.get_credits[:total]
end

child(:credit_cards) do
  extends 'api/v1/credit_cards/credit_card'
end

child(:bank_account) do
  extends 'api/v1/bank_accounts/bank_account'
end

So instead of getting something like the following with 0.4.1:

{
  "user": {
    "id": 1,
    "name": "Javier Julio"
  }
}

We are now getting the following with 0.4.2 which is missing the root key

{
  "id": 1,
  "name": "Javier Julio"
}

We don't have any configuration for rabl-rails for staging or anywhere for that matter (no initializer, just using defaults) so I wonder if it has to do with how templates get compiled that is causing this bug?

ccocchi commented 8 years ago

Hello Javier,

I can't reproduce this bug in a fresh rails app using 4.2.6 using no initializer or custom configuration, which is consistent with changes made between the two versions.

The template i'm using is

object :@post
extends 'posts/post'

with the extended one being

attribute :title
node(:template) { 'show' }

And the response with 0.4.1 and 0.4.2 is the same:

{
  "post": {
    "title": "New post",
    "template": "show"
  }
}

Did you make changes with the JSON engine your using to generate the result (which may have a different behavior with json root values) ?

javierjulio commented 8 years ago

Thanks for looking into it and replying. No changes since this is working as expected on 0.4.1. I'm not sure what could be different in 0.4.2 that is causing the issue. My guess was perhaps doing several extends and other stuff probably removed the root key. We are using the latest stable versions of responders and oj too. We don't have anything enabled in config/initializers/wrap_parameters.rb which is all just commented out. I'm at a loss as to why we'd not be having the root key returned. I'm trying to test again to see what could be causing it. If you have any ideas please let me know. Has me a bit stumped as to why it could be happening as we don't have any special config even anything that would be related.

ccocchi commented 8 years ago

What is the content of your api/v1/users/user template?

javierjulio commented 8 years ago

@ccocchi it looks something like this, note it does another extends at the bottom:

attributes  :id,
            :name,
            :email,
            :gender,
            :birthdate

node(:modifiable_at_timestamp) do |user|
  user.modifiable_at_timestamp.to_i
end

node(:middle_name) do # deprecated
  nil
end

node(:zip_code) do # deprecated
  nil
end

node(:has_facebook_invites) do |user|
  !user.facebook_invites_batches.empty?
end

extends 'api/v1/shared/base'

And then the base.rabl content is below.. another extends:

extends 'api/v1/shared/timestamps'
javierjulio commented 8 years ago

Sorry should have been complete and pasted the timestamps.rabl contents too so for completeness its:

node(:created_at_timestamp) do |model|
  model.created_at.to_i
end

node(:updated_at_timestamp) do |model|
  model.updated_at.to_i
end
ccocchi commented 8 years ago

I tried to use the same template hierarchy with multiple extends, but I still have the root user in the JSON.

Did you try to force the configuration config.include_json_root = true or to explicitly add the root in the template, i.e. object :@user, root: :user ?

javierjulio commented 6 years ago

@ccocchi I've upgraded rabl-rails to the latest version yesterday as we've since moved on to a whole new deployment setup (we are on GKE now) for our staging and production environments. I'm not noticing this issue anymore which is a relief. Thanks again for looking into this! ❤️