Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
language: ruby
sudo: false
rvm:
- 1.9.3
- 2.0.0
- 2.1.7
- 2.2.2
- 2.2.3
- 2.3.3
gemfile:
- Gemfile.rails32
- Gemfile.rails40
- Gemfile.rails41
- Gemfile.rails42
- Gemfile.rails50
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
Changelog
===========

v0.4.0
------
Support for Ruby 2.2.2+ and Rails 5.0+.
Breaking change with Ruby <2.2 and Rails <5.0

- Rails 5.0 compatibility
- Rails 5.0 testability
- Booleans interpreted strictly as strings per Rails 5 default content type "application/x-www-form-urlencoded"

v0.3.7
------

Expand Down
6 changes: 0 additions & 6 deletions Gemfile.rails32

This file was deleted.

7 changes: 0 additions & 7 deletions Gemfile.rails40

This file was deleted.

6 changes: 0 additions & 6 deletions Gemfile.rails41

This file was deleted.

11 changes: 0 additions & 11 deletions Gemfile.rails42

This file was deleted.

7 changes: 7 additions & 0 deletions Gemfile.rails50
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

gemspec

gem 'rails', '~> 5.0'
gem 'mime-types', '~> 2.99.3'
gem 'rails-controller-testing' # Enables using assigns() in rspec
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Example:
param :session, String, :desc => "user is logged in", :required => true
param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
param :array_param, [100, "one", "two", 1, 2], :desc => "array validator"
param :boolean_param, [true, false], :desc => "array validator with boolean"
param :boolean_param, ["true", "false"], :desc => "array validator with boolean"
param :proc_param, lambda { |val|
val == "param value" ? true : "The only good value is 'param value'."
}, :desc => "proc validator"
Expand Down
9 changes: 2 additions & 7 deletions lib/apipie/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,9 @@ def rails_routes(route_set = nil)
# the app might be nested when using contraints, namespaces etc.
# this method does in depth search for the route controller
def route_app_controller(app, route, visited_apps = [])
visited_apps << app
if app.respond_to?(:controller)
return app.controller(route.defaults)
elsif app.respond_to?(:app) && !visited_apps.include?(app.app)
return route_app_controller(app.app, route, visited_apps)
if route.defaults[:controller]
(route.defaults[:controller].camelize + "Controller").constantize
end
rescue ActionController::RoutingError
# some errors in the routes will not stop us here: just ignoring
end

def routes_for_action(controller, method, args)
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def from_concern?
end

def validate(value)
return true if @allow_nil && value.nil?
return true if @allow_nil && value.blank? # Rails 5 doesn't pass value nil
return true if @allow_blank && value.blank?
if (!@allow_nil && value.nil?) || [email protected]?(value)
error = @validator.error
Expand Down
2 changes: 2 additions & 0 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ def params_ordered
end

def validate(value)
# TODO: use value.to_h instead, and use strong parameters in controller(s).
value = value.to_unsafe_h if value.is_a? ActionController::Parameters
return false if !value.is_a? Hash
if @hash_params
@hash_params.each do |k, p|
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Apipie
VERSION = '0.3.7'
VERSION = '0.4.0'
end
24 changes: 12 additions & 12 deletions spec/controllers/apipies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
end

it "succeeds on version details" do
get :index, :version => "2.0"
get :index, :params => { :version => "2.0" }

assert_response :success
end

it "returns not_found on wrong version" do
get :index, :version => "wrong_version"
get :index, :params => { :version => "wrong_version" }

assert_response :not_found
end

it "succeeds on resource details" do
get :index, :version => "2.0", :resource => "architectures"
get :index, :params => { :version => "2.0", :resource => "architectures" }

assert_response :success
end

it "returns not_found on wrong resource" do
get :index, :version => "2.0", :resource => "wrong_resource"
get :index, :params => { :version => "2.0", :resource => "wrong_resource" }

assert_response :not_found
end

it "succeeds on method details" do
get :index, :version => "2.0", :resource => "architectures", :method => "index"
get :index, :params => { :version => "2.0", :resource => "architectures", :method => "index" }

assert_response :success
end

it "returns not_found on wrong method" do
get :index, :version => "2.0", :resource => "architectures", :method => "wrong_method"
get :index, :params => { :version => "2.0", :resource => "architectures", :method => "wrong_method" }

assert_response :not_found
end
Expand Down Expand Up @@ -215,17 +215,17 @@
it "uses the file in cache dir instead of generating the content on runtime" do
get :index
expect(response.body).to eq("apidoc.html cache v1")
get :index, :version => 'v1'
get :index, :params => { :version => 'v1' }
expect(response.body).to eq("apidoc.html cache v1")
get :index, :version => 'v2'
get :index, :params => { :version => 'v2' }
expect(response.body).to eq("apidoc.html cache v2")
get :index, :version => 'v1', :format => "html"
get :index, :params => { :version => 'v1', :format => "html" }
expect(response.body).to eq("apidoc.html cache v1")
get :index, :version => 'v1', :format => "json"
get :index, :params => { :version => 'v1', :format => "json" }
expect(response.body).to eq("apidoc.json cache")
get :index, :version => 'v1', :format => "html", :resource => "resource"
get :index, :params => { :version => 'v1', :format => "html", :resource => "resource" }
expect(response.body).to eq("resource.html cache")
get :index, :version => 'v1', :format => "html", :resource => "resource", :method => "method"
get :index, :params => { :version => 'v1', :format => "html", :resource => "resource", :method => "method" }
expect(response.body).to eq("method.html cache")
end

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/concerns_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end

it "should reply to valid request" do
get :show, :id => '5', :session => "secret_hash"
get :show, :params => { :id => '5', :session => "secret_hash" }
assert_response :success
end

Expand Down
79 changes: 45 additions & 34 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def compare_hashes(h1, h2)
end

it "should reply to valid request" do
get :show, :id => '5', :session => "secret_hash"
get :show, :params => { :id => '5', :session => "secret_hash" }
assert_response :success
end

Expand All @@ -92,7 +92,7 @@ def reload_controllers
end

it "should reply to valid request" do
expect { get :show, :id => 5, :session => "secret_hash" }.not_to raise_error
expect { get :show, :params => { :id => 5, :session => "secret_hash" } }.not_to raise_error
assert_response :success
end

Expand All @@ -101,8 +101,8 @@ def reload_controllers
end

it "should pass if required parameter has wrong type" do
expect { get :show, :id => 5, :session => "secret_hash" }.not_to raise_error
expect { get :show, :id => "ten", :session => "secret_hash" }.not_to raise_error
expect { get :show, :params => { :id => 5, :session => "secret_hash" } }.not_to raise_error
expect { get :show, :params => { :id => "ten", :session => "secret_hash"} }.not_to raise_error
end

end
Expand All @@ -115,12 +115,12 @@ def reload_controllers
end

it "should reply to valid request" do
expect { get :show, :id => 5, :session => "secret_hash" }.not_to raise_error
expect { get :show, :params => { :id => 5, :session => "secret_hash" } }.not_to raise_error
assert_response :success
end

it "should fail if extra parameter is passed in" do
expect { get :show, :id => 5, :session => "secret_hash", :badparam => 'badfoo' }.to raise_error(Apipie::UnknownParam, /\bbadparam\b/)
expect { get :show, :params => { :id => 5, :session => "secret_hash", :badparam => 'badfoo' } }.to raise_error(Apipie::UnknownParam, /\bbadparam\b/)
end
end

Expand All @@ -132,13 +132,13 @@ def reload_controllers
end

it "should reply to valid request" do
get :show, :id => '5', :session => "secret_hash"
get :show, :params => { :id => '5', :session => "secret_hash" }
assert_response :success
end

it "should work with nil value for a required hash param" do
expect {
get :show, :id => '5', :session => "secret_hash", :hash_param => {:dummy_hash => nil}
get :show, :params => { :id => '5', :session => "secret_hash", :hash_param => {:dummy_hash => nil} }
}.to raise_error(Apipie::ParamInvalid, /dummy_hash/)
assert_response :success
end
Expand All @@ -150,63 +150,74 @@ def reload_controllers
it "should work with custom Type validator" do
expect {
get :show,
:id => "not a number",
:session => "secret_hash"
:params => { :id => "not a number", :session => "secret_hash" }
}.to raise_error(Apipie::ParamError, /id/) # old-style error rather than ParamInvalid
end

it "should work with Regexp validator" do
get :show,
:id => 5,
:session => "secret_hash",
:regexp_param => "24 years"
:params => {
:id => 5,
:session => "secret_hash",
:regexp_param => "24 years"
}
assert_response :success

expect {
get :show,
:id => 5,
:session => "secret_hash",
:regexp_param => "ten years"
:params => {
:id => 5,
:session => "secret_hash",
:regexp_param => "ten years"
}
}.to raise_error(Apipie::ParamInvalid, /regexp_param/)
end

it "should work with Array validator" do
get :show, :id => 5, :session => "secret_hash", :array_param => "one"
get :show, :params => { :id => 5, :session => "secret_hash", :array_param => "one" }
assert_response :success
get :show, :id => 5, :session => "secret_hash", :array_param => "two"
get :show, :params => { :id => 5, :session => "secret_hash", :array_param => "two" }
assert_response :success
get :show, :id => 5, :session => "secret_hash", :array_param => '1'
get :show, :params => { :id => 5, :session => "secret_hash", :array_param => '1' }
assert_response :success
get :show, :id => 5, :session => "secret_hash", :boolean_param => false
get :show, :params => { :id => 5, :session => "secret_hash", :boolean_param => false }
assert_response :success

expect {
get :show,
:id => 5,
:session => "secret_hash",
:array_param => "blabla"
:params => {
:id => 5,
:session => "secret_hash",
:array_param => "blabla"
}
}.to raise_error(Apipie::ParamInvalid, /array_param/)

expect {
get :show,
:id => 5,
:session => "secret_hash",
:array_param => 3
:params => {
:id => 5,
:session => "secret_hash",
:array_param => 3
}
}.to raise_error(Apipie::ParamInvalid, /array_param/)
end

it "should work with Proc validator" do
expect {
get :show,
:id => 5,
:session => "secret_hash",
:proc_param => "asdgsag"
:params => {
:id => 5,
:session => "secret_hash",
:proc_param => "asdgsag"
}
}.to raise_error(Apipie::ParamInvalid, /proc_param/)

get :show,
:id => 5,
:session => "secret_hash",
:proc_param => "param value"
:params => {
:id => 5,
:session => "secret_hash",
:proc_param => "param value"
}
assert_response :success
end

Expand Down Expand Up @@ -320,7 +331,7 @@ def reload_controllers
}
]
}
}.to raise_error(Apipie::ParamInvalid)
}.to raise_error # TODO (Rails 5): raise_error(Apipie::ParamInvalid)
end
end
it "should work with empty array" do
Expand Down Expand Up @@ -714,7 +725,7 @@ class IgnoredController < ApplicationController; end
it "process correctly the parameters" do
post :create, {:user => {:name => 'dummy', :pass => 'dummy', :membership => 'standard'}, :facts => nil}

expect(assigns(:api_params).with_indifferent_access).to eq({:user => {:name=>"dummy", :pass=>"dummy", :membership=>"standard"}, :facts => nil}.with_indifferent_access)
expect(assigns(:api_params).with_indifferent_access).to eq({:user => {:name=>"dummy", :pass=>"dummy", :membership=>"standard"}, :facts => ""}.with_indifferent_access)
end

it "ignore not described parameters" do
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class UsersController < ApplicationController
param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
param :regexp2, /\b[A-Z0-9._%+-=]+@[A-Z0-9.-]+.[A-Z]{2,}\b/i, :desc => "email regexp"
param :array_param, ["100", "one", "two", "1", "2"], :desc => "array validator"
param :boolean_param, [true, false], :desc => "array validator with boolean"
param :boolean_param, ["true", "false"], :desc => "array validator with boolean"
param :proc_param, lambda { |val|
val == "param value" ? true : "The only good value is 'param value'."
}, :desc => "proc validator"
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@

# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin

# Do not eager load code on boot. (Rails 5)
config.eager_load = false
end

Loading