Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last ifg #8

Open
wants to merge 9 commits into
base: last-official
Choose a base branch
from
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _apipie_dsl_data_init
:errors => [],
:params => [],
:headers => [],
:resouce_id => nil,
:resource_id => nil,
:short_description => nil,
:description => nil,
:examples => [],
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
17 changes: 13 additions & 4 deletions lib/apipie/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down