diff --git a/.travis.yml b/.travis.yml index c0b78f0fe..f21b7cf5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,8 @@ language: ruby sudo: false rvm: - - 1.9.3 - - 2.0.0 - - 2.1.7 - - 2.2.3 + - 2.2.5 + - 2.3.1 gemfile: - Gemfile.rails32 - Gemfile.rails40 diff --git a/lib/apipie/dsl_definition.rb b/lib/apipie/dsl_definition.rb index 0c35f2dbc..fc3369a96 100644 --- a/lib/apipie/dsl_definition.rb +++ b/lib/apipie/dsl_definition.rb @@ -26,7 +26,7 @@ def _apipie_dsl_data_init :errors => [], :params => [], :headers => [], - :resouce_id => nil, + :resource_id => nil, :short_description => nil, :description => nil, :examples => [], @@ -223,7 +223,7 @@ def _apipie_define_validators(description) if Apipie.configuration.validate_presence? method_params.each do |_, param| # check if required parameters are present - raise ParamMissing.new(param) if param.required && !params.has_key?(param.name) + raise ParamMissing.new(param, param) if param.required && !params.has_key?(param.name) end end @@ -239,7 +239,7 @@ def _apipie_define_validators(description) if Apipie.configuration.validate_key? params.reject{|k,_| %w[format controller action].include?(k.to_s) }.each_key do |param| # params allowed - raise UnknownParam.new(param) if method_params.select {|_,p| p.name.to_s == param.to_s}.empty? + raise UnknownParam.new(param, param) if method_params.select {|_,p| p.name.to_s == param.to_s}.empty? end end diff --git a/lib/apipie/errors.rb b/lib/apipie/errors.rb index 62472bb0a..c1d28e8bf 100644 --- a/lib/apipie/errors.rb +++ b/lib/apipie/errors.rb @@ -8,10 +8,15 @@ class ParamError < Error # abstract class DefinedParamError < ParamError - attr_accessor :param + attr_accessor :param, :param_description - def initialize(param) + def initialize(param, param_description) @param = param + @param_description = param_description + end + + def parameter_path + param_description.parents_path end end @@ -27,6 +32,10 @@ def to_s "Missing parameter #{@param.name}" end end + + def parameter_path + [param.name] + param_description.parents_path + end end class UnknownParam < DefinedParamError @@ -38,8 +47,8 @@ def to_s class ParamInvalid < DefinedParamError attr_accessor :value, :error - def initialize(param, value, error) - super param + def initialize(param, value, error, param_description) + super param, param_description @value = value @error = error end diff --git a/lib/apipie/param_description.rb b/lib/apipie/param_description.rb index 47ba6db03..3fd2534af 100644 --- a/lib/apipie/param_description.rb +++ b/lib/apipie/param_description.rb @@ -105,6 +105,19 @@ def parents_and_self ret end + # Get parameter parents names + def parents_path + names ||= [] + obj = self + + while !obj.nil? + names << obj.name + obj = obj.parent + end + + names + end + def to_json(lang = nil) hash = { :name => name.to_s, :full_name => full_name, diff --git a/lib/apipie/validator.rb b/lib/apipie/validator.rb index e4bfa0e65..14aaed361 100644 --- a/lib/apipie/validator.rb +++ b/lib/apipie/validator.rb @@ -48,7 +48,7 @@ def description end def error - ParamInvalid.new(param_name, @error_value, description) + ParamInvalid.new(param_name, @error_value, description, param_description) end def to_s @@ -263,7 +263,7 @@ def self.build(param_description, argument, options, proc) end def error - ParamInvalid.new(param_name, @error_value, @help) + ParamInvalid.new(param_name, @error_value, @help, param_description) end def description @@ -306,7 +306,7 @@ def validate(value) if @hash_params @hash_params.each do |k, p| if Apipie.configuration.validate_presence? - raise ParamMissing.new(p) if p.required && !value.has_key?(k) + raise ParamMissing.new(p, param_description) if p.required && !value.has_key?(k) end if Apipie.configuration.validate_value? p.validate(value[k]) if value.has_key?(k) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index b89f79229..6b41fe1b1 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -143,6 +143,13 @@ def reload_controllers assert_response :success end + it "should work with {} value for a required hash param" do + expect { + get :show, :id => '5', :session => "secret_hash", :hash_param => {:dummy_hash => {}} + }.to raise_error(Apipie::ParamMissing, 'Missing parameter dummy_2') + assert_response :success + end + it "should fail if required parameter is missing" do expect { get :show, :id => 5 }.to raise_error(Apipie::ParamMissing, /session_parameter_is_required/) end