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

Hopefully the final fix for Rails 5 compatibility #527

Merged
merged 5 commits into from
Feb 17, 2017
Merged
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
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ rvm:
- 2.0.0
- 2.1.7
- 2.2.3
- 2.3.3
gemfile:
- Gemfile.rails32
- Gemfile.rails40
- Gemfile.rails41
- Gemfile.rails42
- Gemfile.rails50 # Min ruby 2.2.2
matrix:
exclude:
- rvm: 1.9.3
gemfile: Gemfile.rails50
- rvm: 2.0.0
gemfile: Gemfile.rails50
- rvm: 2.1.7
gemfile: Gemfile.rails50
7 changes: 0 additions & 7 deletions Gemfile

This file was deleted.

1 change: 1 addition & 0 deletions Gemfile
6 changes: 0 additions & 6 deletions Gemfile.rails32

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile.rails41
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ source "https://rubygems.org"
gemspec

gem 'rails', '~> 4.1.0'
gem 'mime-types', '~> 2.99.3'
gem 'mime-types', '~> 2.99.3'
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'
2 changes: 1 addition & 1 deletion apipie-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ["lib"]

s.add_development_dependency "rails", ">= 3.0.20"
s.add_dependency "rails", ">= 4.0"
s.add_dependency 'json'
s.add_development_dependency "rspec-rails", "~> 3.0"
s.add_development_dependency "sqlite3"
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
12 changes: 12 additions & 0 deletions lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,20 @@ def from_concern?
method_description.from_concern? || @from_concern
end

def normalized_value(value)
if value.is_a?(ActionController::Parameters) && !value.is_a?(Hash)
value.to_unsafe_hash
elsif value.is_a? Array
value.map { |v| normalized_value (v) }
else
value
end
end

def validate(value)
return true if @allow_nil && value.nil?
return true if @allow_blank && value.blank?
value = normalized_value(value)
if (!@allow_nil && value.nil?) || !@validator.valid?(value)
error = @validator.error
error = ParamError.new(error) unless error.is_a? StandardError
Expand All @@ -83,6 +94,7 @@ def validate(value)
end

def process_value(value)
value = normalized_value(value)
if @validator.respond_to?(:process_value)
@validator.process_value(value)
else
Expand Down
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 => { :user_id => "secret_hash" }
assert_response :success
end

Expand Down
Loading