From b3f894c5282244b41221f98dfac5296cea5a4485 Mon Sep 17 00:00:00 2001 From: Sebastian Sogamoso Date: Mon, 11 Mar 2013 08:00:19 -0500 Subject: Change ActionController::Parameters#require behavior when value is empty When the value for the required key is empty an ActionController::ParameterMissing is raised which gets caught by ActionController::Base and turned into a 400 Bad Request reply with a message in the body saying the key is missing, which is misleading. With these changes, ActionController::EmptyParameter will be raised which ActionController::Base will catch and turn into a 400 Bad Request reply with a message in the body saying the key value is empty. --- .../action_controller/metal/strong_parameters.rb | 32 ++++++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_controller/metal') diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index acad8a0799..0748d75a69 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -9,8 +9,6 @@ module ActionController # params = ActionController::Parameters.new(a: {}) # params.fetch(:b) # # => ActionController::ParameterMissing: param not found: b - # params.require(:a) - # # => ActionController::ParameterMissing: param not found: a class ParameterMissing < KeyError attr_reader :param # :nodoc: @@ -20,6 +18,20 @@ module ActionController end end + # Raised when a required parameter value is empty. + # + # params = ActionController::Parameters.new(a: {}) + # params.require(:a) + # # => ActionController::EmptyParameter: value is empty for required key: a + class EmptyParameter < IndexError + attr_reader :param + + def initialize(param) + @param = param + super("value is empty for required key: #{param}") + end + end + # Raised when a supplied parameter is not expected. # # params = ActionController::Parameters.new(a: "123", b: "456") @@ -154,20 +166,22 @@ module ActionController self end - # Ensures that a parameter is present. If it's present, returns - # the parameter at the given +key+, otherwise raises an - # ActionController::ParameterMissing error. + # Ensures that a parameter is present. If it's present and not empty, + # returns the parameter at the given +key+, if it's empty raises + # an ActionController::EmptyParameter error, otherwise + # raises an ActionController::ParameterMissing error. # # ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person) # # => {"name"=>"Francesco"} # - # ActionController::Parameters.new(person: nil).require(:person) - # # => ActionController::ParameterMissing: param not found: person - # # ActionController::Parameters.new(person: {}).require(:person) + # # => ActionController::EmptyParameter: value is empty for required key: person + # + # ActionController::Parameters.new(name: {}).require(:person) # # => ActionController::ParameterMissing: param not found: person def require(key) - self[key].presence || raise(ParameterMissing.new(key)) + raise(ActionController::ParameterMissing.new(key)) unless self.key?(key) + self[key].presence || raise(ActionController::EmptyParameter.new(key)) end # Alias of #require. -- cgit v1.2.3