trailblazer/representable

Circular references when using explicit, standalone representers

sbwoodside opened this issue · 1 comments

Example:

class CompaniesRepresenter < Roar::Decorator
  include Roar::JSON::JSONAPI.resource :companies
  has_one :owner, class: User, decorator: UsersRepresenter
end

class UsersRepresenter < Roar::Decorator
  include Roar::JSON::JSONAPI.resource :users
  has_many :companies, extend: CompaniesRepresenter, class: Company
end

And then in the rails console: pp CompaniesRepresenter.new(Company.first).as_json and I get SystemStackError: stack level too deep and a stack trace with ... 9450 levels...

Is there a better way to handle this?

At the moment I'm using what I think is called an inline representer:

class CompanyRepresenter < Roar::Decorator
  include Roar::JSON
  include Roar::Hypermedia

  property :id
  property :name
  property :description

  property :owner, class: User do
    property :id
    property :name
  end

  link(:self) { "/#{represented.class}/#{represented.id}" }
end