Giter Club home page Giter Club logo

Comments (11)

amrnt avatar amrnt commented on May 12, 2024

I got over this issue by using

expose :preferences do |model, options|
  if model.preference
    # Hash
  end
end

instead of expose :preference, :as => :preferences, :using => PreferenceEntity

But I think its still an issue!

from grape.

justfalter avatar justfalter commented on May 12, 2024

I believe I've run into this issue, myself. I can't seem to find a way leverage :using in the way that the documentation seems to intend its usage: nesting entities.

Grape::Entity implements #as_json, but when JSON encoders like OkJson go to encode the nested Entity objects, it tries to call #to_json (which does not exist). Depending on the JSON encoder, they may chose to encode an object that lacks #to_json, however OkJson throws an exception if it does not.

I've effectively done the following in my code in order to encode the entities in a consistent manner.

expose :preferences do |model, options|
  if model.preference
    model.preference.map { |pref| PreferenceEntity.new(pref).serializable_hash }
  end
end

Alternatively, doing something like the following also works, but I suspect that it is a bit inappropriate.

class PreferenceEntity < Grape::Entity
   alias :to_json :as_json
   ...
end

I haven't looked into it more deeply, but I may see if I can't figure out a patch and send a pull request to fix it.

from grape.

256dpi avatar 256dpi commented on May 12, 2024

For me the :using property is also not working. I think its because of the missing to_json function or something like that:

[
  {
    ...
    "slots": [
      "#<Harbour::API::V1::SlotEntity:0x007fcb33d243c0>",
      "#<Harbour::API::V1::SlotEntity:0x007fcb33d24230>",
      "#<Harbour::API::V1::SlotEntity:0x007fcb33d240f0>",
      "#<Harbour::API::V1::SlotEntity:0x007fcb33d23fb0>",
      "#<Harbour::API::V1::SlotEntity:0x007fcb33d23dd0>"
    ]
  }
]

To fix the nesting problem i'm using currently the following method:

expose :slots do |station,options|
  station.slots.map{ |slot| SlotEntity.new(slot).serializable_hash }
end

from grape.

chrisbloom7 avatar chrisbloom7 commented on May 12, 2024

Similar fix for a has_one relationship:

expose :address do |model, options|
  if model.address
    API::Entities::V1::Location.new(model.address).serializable_hash
  end
end

Trying it the recommended way resulted in the following error:

undefined method `represent' for #<Class:0x00000129261f90>

/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/entity.rb:348:in `value_for'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/entity.rb:317:in `block in serializable_hash'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/entity.rb:315:in `each'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/entity.rb:315:in `inject'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/entity.rb:315:in `serializable_hash'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:122:in `serialize'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:134:in `encode_json'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/formatter.rb:79:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/formatter.rb:79:in `block in after'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/formatter.rb:78:in `collect'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/formatter.rb:78:in `after'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:27:in `call!'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:20:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:26:in `call!'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:20:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:26:in `call!'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:20:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/error.rb:44:in `block in call!'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/error.rb:43:in `catch'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/error.rb:43:in `call!'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/middleware/base.rb:20:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/endpoint.rb:113:in `call!'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/endpoint.rb:103:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:152:in `block in call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:96:in `block in recognize'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:96:in `optimized_each'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:95:in `recognize'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:141:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/api.rb:425:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/api.rb:55:in `call!'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/grape-0.2.2/lib/grape/api.rb:51:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:600:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/warden-1.2.1/lib/warden/manager.rb:34:in `catch'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/warden-1.2.1/lib/warden/manager.rb:34:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/etag.rb:23:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/conditionalget.rb:25:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/head.rb:14:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/flash.rb:242:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/cookies.rb:339:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/query_cache.rb:64:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `_run__644697953361164916__call__1047081581030344329__callbacks'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/reloader.rb:65:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/rack/logger.rb:26:in `call_app'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/rack/logger.rb:16:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/request_id.rb:22:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/runtime.rb:17:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/lock.rb:15:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/actionpack-3.2.8/lib/action_dispatch/middleware/static.rb:62:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/engine.rb:479:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/application.rb:223:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/rack/log_tailer.rb:17:in `call'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/thin-1.5.0/lib/thin/connection.rb:81:in `block in pre_process'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/thin-1.5.0/lib/thin/connection.rb:79:in `catch'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/thin-1.5.0/lib/thin/connection.rb:79:in `pre_process'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/thin-1.5.0/lib/thin/connection.rb:54:in `process'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/thin-1.5.0/lib/thin/connection.rb:39:in `receive_data'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/thin-1.5.0/lib/thin/backends/base.rb:63:in `start'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/thin-1.5.0/lib/thin/server.rb:159:in `start'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/server.rb:70:in `start'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:55:in `block in <top (required)>'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:50:in `tap'
/Users/chrisbloom7/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'

from grape.

lucassus avatar lucassus commented on May 12, 2024

I have the same issue. The strangest thing is that it works on my laptop but breaks on travis-ci (https://travis-ci.org/lucassus/mongo_browser/jobs/3839962) and on my other machine.
Here is code for my API: https://github.com/lucassus/mongo_browser/blob/master/lib/mongo_browser/api.rb
And here is my workaround for the problem: lucassus/mongo_browser@47de199

On both machines I have the same ruby's configuration: ruby-1.9.3-p327-perf with separate rvm gemsets for my project.

from grape.

dblock avatar dblock commented on May 12, 2024

This may be fixed on HEAD. We now fully reply on the formatter, so if you use format :json it will call to_json where possible. I'd appreciate if those on this thread could try again and see if there're still problems.

from grape.

dblock avatar dblock commented on May 12, 2024

@agileanimal Does this bug belong in grape-entity?

from grape.

idyll avatar idyll commented on May 12, 2024

What's the best way to move it over and keep the thread?

from grape.

mtjhax avatar mtjhax commented on May 12, 2024

I was not able to reproduce the original error from @amrnt in Rails 3.2.11 and Grape 0.2.6. I notice the original error backtrace referenced Rails 3.1.1 and Grape 0.2.2. Unclear if I am attempting to reproduce the problem improperly or it was fixed per @dblock.

from grape.

dblock avatar dblock commented on May 12, 2024

@amrnt Do you have any updates to this? Is it a problem still? Can I close this?

from grape.

dblock avatar dblock commented on May 12, 2024

I am closing this.

from grape.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.