- if hash[key].is_a? String
- hash[key] = SafeJSON.load(hash[key])
- if must_be_class and !hash[key].is_a? must_be_class
- raise TypeError.new("parameter #{key.to_s} must be a #{must_be_class.to_s}")
- end
+ return if hash[key].nil?
+
+ val = hash[key]
+ if val.is_a? ActionController::Parameters
+ val = val.to_unsafe_hash
+ elsif val.is_a? String
+ val = SafeJSON.load(val)
+ hash[key] = val
+ end
+ # When assigning a Hash to an ActionController::Parameters and then
+ # retrieve it, we get another ActionController::Parameters instead of
+ # a Hash. This doesn't happen with other types. This is why 'val' is
+ # being used to do type checking below.
+ if must_be_class and !val.is_a? must_be_class
+ raise TypeError.new("parameter #{key.to_s} must be a #{must_be_class.to_s}")