Skip to content

Bug: File caching issue - form reuses cached file after validation error #205

@Skumring

Description

@Skumring

Bug: File caching issue - form reuses cached file after validation error

Description

When submitting the import form after a validation error, the system reuses the previously cached file instead of showing the "no file" error when no file is selected. This causes confusion and potential unintended data imports.


Reproduction Script

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  gem 'rails', '~> 7.0.0'
  gem 'sqlite3', '~> 1.6.0'
  gem 'activeadmin', '~> 2.9'
  gem 'active_admin_import', '5.1.0'
  gem 'rchardet', '~> 1.8'
  gem 'rubyzip', '~> 2.3'
  gem 'activerecord-import', '~> 1.5'
end

require 'active_record'
require 'action_controller/railtie'
require 'active_admin'
require 'active_admin_import'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :authors, force: true do |t|
    t.string :name
    t.string :last_name
    t.timestamps
  end
end

class Author < ActiveRecord::Base
  validates_presence_of :name
end

# Test the caching bug
puts "\n=== Testing File Caching Issue ===\n"

# Create a model instance and simulate form submission
model = ActiveAdminImport::Model.new

# First submission with a file
file = double('file', present?: true, path: '/tmp/test.csv')
model.assign_attributes(file: file)
puts "First submission - file assigned: #{model.file.present?}"

# Simulate validation error by creating a new model without passing file
# This simulates what happens when form is resubmitted without file selection
model2 = ActiveAdminImport::Model.new
model2.assign_attributes({}) # Empty hash, no file key

puts "Second submission - file should be nil: #{model2.file.nil?}"
puts "BUG: File is still present!" unless model2.file.nil?

# The bug: assign_attributes doesn't reset file to nil when not in args
# This causes cached file from previous submission to persist

Steps to Reproduce (in real application)

  1. Visit /admin/authors/import
  2. Upload a CSV file with invalid data (e.g., missing required column name)
  3. Submit the form → validation error displayed: "can't write unknown attribute" or similar
  4. Submit the form again WITHOUT selecting any file

Expected Behavior

Error message should display: "Please, select file to import"


Actual Behavior

The system reuses the cached file from step 2, attempting to import it again instead of showing the "no file" error.


Root Cause

In model.rb:50, when assign_attributes is called without a :file key in the hash, it doesn't explicitly set file to nil:

def assign_attributes(args = {}, new_record = false)
  @attributes.merge!(args)  # file persists if not in args!
  @new_record = new_record
  # ...
end

When the form is resubmitted without a file, Rails doesn't include the file parameter in the params hash. The assign_attributes method then merges an empty hash with the existing attributes, leaving the previous file in place.

Environment

  • Ruby: 3.2.1
  • Rails: 7.0.0+ (also affects 5.2, 6.0, 6.1, 7.1)
  • ActiveAdmin: 2.9.0
  • active_admin_import: 5.1.0 (all versions prior to the fix are affected)
  • Database: SQLite3 1.6.0 (but affects all databases)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions