Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveStorage::Attachment - Unhandled case where named_variants can be nil #51882

Closed
CKolkey opened this issue May 22, 2024 · 0 comments · Fixed by #51931
Closed

ActiveStorage::Attachment - Unhandled case where named_variants can be nil #51882

CKolkey opened this issue May 22, 2024 · 0 comments · Fixed by #51931

Comments

@CKolkey
Copy link

CKolkey commented May 22, 2024

In ActiveStorage::Attachment, the private method named_variants can return nil, but callers of this method do not guard against a nil value being returned.

def named_variants
record.attachment_reflections[name]&.named_variants
end

preprocessed_variations = named_variants.filter_map { |_name, named_variant|

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  gem "rails"
  gem "sqlite3", "~> 1.4"
  gem "debug"
end

require "active_record/railtie"
require "active_storage/engine"
require "tmpdir"

class TestApp < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f

  config.root = __dir__
  config.hosts << "example.org"
  config.eager_load = false
  config.session_store :cookie_store, key: "cookie_store_key"
  config.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  config.active_storage.service = :local
  config.active_storage.service_configurations = {
    local: {
      root: Dir.tmpdir,
      service: "Disk",
    },
  }
end

ENV["DATABASE_URL"] = "sqlite3::memory:"

Rails.application.initialize!

require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s

ActiveRecord::Schema.define do
  CreateActiveStorageTables.new.change

  create_table :uploads, force: true do |t|
    t.bigint "blob_id", null: false
    t.index ["blob_id"], name: "index_uploads_on_blob_id"
  end
end

class Upload < ActiveRecord::Base
  belongs_to :blob, class_name: "ActiveStorage::Blob"
end

require "minitest/autorun"

class BugTest < Minitest::Test
  def test_no_named_variants
    blob = ActiveStorage::Blob.create(checksum: 0, byte_size: 0, filename: "example", content_type: "image/png")
    record = Upload.create(blob_id: blob.id)
    attachment = ActiveStorage::Attachment.create(blob:, record:, name: "attachment_example")

    assert_equal true, true
  end
end

Expected behavior

No exception should get raised if there are no named variants.

Actual behavior

eval error: undefined method 'each' for nil

System configuration

Rails version:
Rails 7.1.3.3

Ruby version:
ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin23]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant