liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Liquid rendering is unexpectedly changing the "context" attribute on non-Drop classes

Open lavaturtle opened this issue 10 months ago • 0 comments

Background: I have a Rails app with several classes that are used in Liquid rendering like this:

class Thingy < ApplicationRecord
  def to_liquid
    LiquidDrops::Thingy.new(record: self)
  end
end

module LiquidDrops
  class Thingy < Liquid::Drop
    attr_accessor :record

    def name
      record.name
    end
  end
end

so I can then write {{ my_thingy.name }}, render it with a context like { 'my_thingy' => some_thingy }, and get the right output.

Since upgrading from liquid 5.4.0 to 5.5.0, I've noticed a problem with one of my models that happens to have an attribute named context. Whenever I render some liquid involving that model, it updates the value of context to a Liquid::Context (or a string like "#<Liquid::Context:0x000073767f8c1540>"), even if I'm not doing {{ my_thingy.context }}. This has caused problems where I end up with incorrect values of context in the database.

I suspect this is an unintended side effect of https://github.com/Shopify/liquid/commit/a5e5fab82a8f52f7045c329c15c3663ed25820a0. Would it be possible to limit that change to only set variable.context if variable is a Liquid::Drop, not some arbitrary other type of record?

Thanks for your time!

lavaturtle avatar Apr 15 '24 20:04 lavaturtle