grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

Error "Circular dependency detected while autoloading constant" using entities

Open volonterx opened this issue 10 years ago • 5 comments
trafficstars

app/api/mysite/entities/v1/order.rb

module Mysite
  module Entities
    class V1::Order < Mysite::Entities::Base
      expose :id
      expose :state
      expose :cost

      expose :supplier, using: "Mysite::Entities::V1::Supplier"
      expose :office, using: "Mysite::Entities::V1::TheOffice", as: :customer

      expose :order_product_memberships, using: "Mysite::Entities::V1::OrderProductMembership", as: :product

      with_options(format_with: :iso_timestamp) do
        expose :created_at
        expose :updated_at
      end
    end
  end
end

app/api/mysite/v1/order_api.rb

module Mysite
  class V1::OrderAPI < Mysite::API
    resource :orders do
      include Grape::Kaminari
      paginate per_page: 20

      desc "Return a list of orders"
      params do
        requires :api_key
        requires :company_role
        optional :last_time_seen, allow_blank: false
      end
      get do
        company = Company.find_by_api_key!(params[:api_key])
        orders = company.orders_as(params[:company_role]).updated_from(params[:last_time_seen])
        present :orders, paginate(orders), with: Mysite::Entities::V1::Order
      end
...

When I trying to get list of orders via api, I get:

RuntimeError (Circular dependency detected while autoloading constant Mysite::Entities::V1::Order):
  app/api/mysite/v1/order_api.rb:16:in `block (2 levels) in <class:OrderAPI>' 

(in "present :orders...")

Early I have similar problem with expose :office, using: Mysite::Entities::V1::TheOffice, as: :customer unless when I moved Mysite::Entities::V1::TheOffice in quotes

Now problem with with: Mysite::Entities::V1::Order I have tried many tricks and I think it's a bug.

ruby 2.1.1 rails 4.1.8 grape (0.13.0) grape-entity (0.4.5)

volonterx avatar Oct 06 '15 19:10 volonterx

Do you have any questions? I can conduct some experiments or anything else.

volonterx avatar Oct 07 '15 21:10 volonterx

Possibly, you could try to write a spec that reproduces this inside this project.

dblock avatar Oct 08 '15 11:10 dblock

I forgot the most important thing! This bug appears only in production environment. For development all is OK.

volonterx avatar Oct 08 '15 18:10 volonterx

Maybe try to build out a minimal project we can play with?

dblock avatar Oct 09 '15 11:10 dblock

Just a sidenote to and old issue: There's now an ability in grape-entity to define the class name as a string. This will help with circular dependencies:

expose :replies, using: "API::Entities::Status", as: :responses

pre avatar Apr 26 '17 06:04 pre