procore-oss / blueprinter

Simple, Fast, and Declarative Serialization Library for Ruby
MIT License
1.14k stars 109 forks source link

[bug] `association` with `if: <Symbol>` finds a method from wrong blueprint #458

Closed kyori19 closed 2 months ago

kyori19 commented 3 months ago

Is there an existing issue for this?

Is this a regression?

Current Behavior

association :parent, blueprint: ParentBlueprint, if: :include_parent? in ChildBlueprint calls ParentBlueprint.include_parent?

Expected Behavior

association :parent, blueprint: ParentBlueprint, if: :include_parent? in ChildBlueprint should call ChildBlueprint.include_parent?

Steps To Reproduce

Use this script to reproduce the problem.

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/inline'
require 'json'

gemfile do
  source 'https://rubygems.org'

  # uncomment this to check previous behavior
  # gem 'blueprinter', '= 1.0.2'

  gem 'blueprinter', '= 1.1.0'
end

Parent = Struct.new(:name, keyword_init: true)
Child = Struct.new(:parent, :name, keyword_init: true)

class ParentBlueprint < Blueprinter::Base
  field :name
end

class ChildBlueprint < Blueprinter::Base
  def self.include_parent?(_, _, _)
    true
  end

  field :name
  association :parent, blueprint: ParentBlueprint, if: :include_parent?
end

parent = Parent.new(
  name: 'Parent',
)

child = Child.new(
  parent:,
  name: 'Child',
)

pp ChildBlueprint.render_as_hash(child)

Environment

- OS: Ubuntu 22.04
- Browser Name and version: N/A
- Ruby Version: ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux]

Anything else?

No response

lessthanjacob commented 2 months ago

Apologies for the delay here @kyori19!

I was able to replicate via tests (it looks like there was also a substantial gap in test coverage here), and will prioritize a fix shortly!