Amit Patel
Amit Patel

Reputation: 15985

ActiveJob::SerializationError: Unsupported argument type: Date/Class

I am getting a weird error while passing class and date to an ActiveJob with the Sidekiq adapter.

1] pry(main)> StripeTransactionsSyncJob.perform_later(Stripe::SyncCharges, nil, 3.days.ago.to_date)
ActiveJob::SerializationError: Unsupported argument type: Class
from /home/amit/.rvm/gems/ruby-2.5.8@immosite/gems/activejob-5.0.7.2/lib/active_job/arguments.rb:83:in `serialize_argument'
[2] pry(main)> StripeTransactionsSyncJob.perform_later('Stripe::SyncCharges', nil, 3.days.ago.to_date)
ActiveJob::SerializationError: Unsupported argument type: Date
from /home/amit/.rvm/gems/ruby-2.5.8@immosite/gems/activejob-5.0.7.2/lib/active_job/arguments.rb:83:in `serialize_argument'

As per the doc, ActiveJob should support both types of arguments out of the box. What is wrong here?

Upvotes: 0

Views: 3416

Answers (2)

Jignesh Gohel
Jignesh Gohel

Reputation: 6552

The guide you have referenced in your post refers to the v6.1.4 of Rails. See the version info on top-right corner on that page.

The guide for v5.0 doesn't explicitly specify about the arguments types supported. And looking at the source code (see below) for the version of Rails you are using i.e 5.0.7.2

  def serialize_argument(argument)
    case argument
    when *TYPE_WHITELIST
      argument
    when GlobalID::Identification
      convert_to_global_id_hash(argument)
    when Array
      argument.map { |arg| serialize_argument(arg) }
    when ActiveSupport::HashWithIndifferentAccess
      result = serialize_hash(argument)
      result[WITH_INDIFFERENT_ACCESS_KEY] = serialize_argument(true)
      result
    when Hash
      symbol_keys = argument.each_key.grep(Symbol).map(&:to_s)
      result = serialize_hash(argument)
      result[SYMBOL_KEYS_KEY] = symbol_keys
      result
    else
      raise SerializationError.new("Unsupported argument type: #{argument.class.name}")
    end
  end

your passed argument types Class and Date are not supported and hence you are getting SerializationError.

Note: Whenever referring to the API-docs or Guide I would recommend to view them for the specific version of Rails you are using.

Upvotes: 3

Amit Patel
Amit Patel

Reputation: 15985

The Class/Date/DateTime/Time etc were not supported in Rails 5.0. So I need to use String form of data being passed to the Job.

For reference, here is the method(simplified) that does deserialization

def serialize_argument(argument)
  case argument
  when *[ NilClass, String, Integer, Float, BigDecimal, TrueClass, FalseClass ]
    argument
  when GlobalID::Identification
    convert_to_global_id_hash(argument)
  when Array
    argument.map { |arg| serialize_argument(arg) }
  when ActiveSupport::HashWithIndifferentAccess
    result = serialize_hash(argument)
    result[WITH_INDIFFERENT_ACCESS_KEY] = serialize_argument(true)
    result
  when Hash
    symbol_keys = argument.each_key.grep(Symbol).map(&:to_s)
    result = serialize_hash(argument)
    result[SYMBOL_KEYS_KEY] = symbol_keys
    result
  else
    raise SerializationError.new("Unsupported argument type: #{argument.class.name}")
  end
end

Upvotes: 1

Related Questions