ruby-grape / ruby-grape/grape-entity

ArgumentError - because of circular references?

Open
#125 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

Hey,

I've added Russian Doll caching to an existing endpoint, and I'm getting the strange error below.

This is the code I have:

category = Category.find_by_id(params[:category_id])

ordered_sub_items = Rails.cache.fetch("#{category.cache_key}") do
  active_props = category.active_props

  category.ordered_sub_items.map do |p|
    Rails.cache.fetch("#{p.cache_key}") do 
      category_meta = { position: p.list_pos(category.id), group_id: p.list_group_id(category.id) }

      prop_values = Rails.cache.fetch("#{p.cache_key}/props") do
        active_props.map do |c|
          # data manipulation here..
        end
      end

      OpenStruct.new({
        product: p,
        category_meta: category_meta,
        prop_values: prop_values
      })
    end
  end
end

present ordered_sub_items, with: API::V1::Entities::categoryItem

Error:

ArgumentError - ArgumentError:
  grape-entity (0.4.4) lib/grape_entity/entity.rb:557:in `delegate_attribute'
  grape-entity (0.4.4) lib/grape_entity/entity.rb:543:in `value_for'
  grape-entity (0.4.4) lib/grape_entity/entity.rb:460:in `block in serializable_hash'
  grape-entity (0.4.4) lib/grape_entity/entity.rb:458:in `serializable_hash'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:55:in `block in as_json'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:77:in `check_for_circular_references'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:54:in `as_json'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:215:in `block in as_json'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:215:in `as_json'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:47:in `block in encode'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:77:in `check_for_circular_references'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:46:in `encode'
  activesupport (3.2.21) lib/active_support/json/encoding.rb:31:in `encode'
  activesupport (3.2.21) lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
  grape (0.7.0) lib/grape/formatter/json.rb:6:in `call'
  grape (0.7.0) lib/grape/middleware/formatter.rb:33:in `block in after'
  grape (0.7.0) lib/grape/middleware/formatter.rb:32:in `after'
  grape (0.7.0) lib/grape/middleware/base.rb:25:in `call!'
  grape (0.7.0) lib/grape/middleware/base.rb:18:in `call'
  newrelic_rpm (3.10.0.279) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  grape (0.7.0) lib/grape/middleware/base.rb:24:in `call!'
  grape (0.7.0) lib/grape/middleware/base.rb:18:in `call'
  newrelic_rpm (3.10.0.279) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  grape (0.7.0) lib/grape/middleware/error.rb:26:in `block in call!'
  grape (0.7.0) lib/grape/middleware/error.rb:25:in `call!'
  grape (0.7.0) lib/grape/middleware/base.rb:18:in `call'
  newrelic_rpm (3.10.0.279) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  rack (1.4.5) lib/rack/head.rb:9:in `call'
  newrelic_rpm (3.10.0.279) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  rack (1.4.5) lib/rack/builder.rb:134:in `call'
  grape (0.7.0) lib/grape/endpoint.rb:157:in `call!'
  grape (0.7.0) lib/grape/endpoint.rb:145:in `call'
  rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
  rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
  rack-mount (0.8.3) lib/rack/mount/code_generation.rb:103:in `optimized_each'
  rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
  rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
  grape (0.7.0) lib/grape/api.rb:537:in `call'
  grape (0.7.0) lib/grape/api.rb:44:in `call!'
  grape (0.7.0) lib/grape/api.rb:40:in `call'

Any ideas what I'm doing wrong?

Thanks

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at grape_entity/entity.rb around delegate_attribute and value_for, then trace the endpoint's present call and ActiveSupport's JSON circular-reference checks shown in the stack trace. Reproduce the endpoint with the provided caching structure and identify which returned value causes the serialization failure; done requires a confirmed cause and a documented, reproducible fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.