Skip to content

Commit

Permalink
Merge pull request #127 from dblock/undefined-method-reject
Browse files Browse the repository at this point in the history
Fixed undefined method reject for nil:NilClass error on an undefined route.
  • Loading branch information
dblock committed Jul 22, 2014
2 parents 2693e4a + 001bb86 commit 383d838
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Added `GrapeEntity::VERSION` - [@dblock](https://github.com/dblock).
* Added Rubocop, Ruby-style linter - [@dblock](https://github.com/dblock).
* [#126](https://github.com/tim-vandecasteele/grape-swagger/pull/126): Rewritten demo in the `test` folder with CORS enabled - [@dblock](https://github.com/dblock).
* [#127](https://github.com/tim-vandecasteele/grape-swagger/pull/127): Fixed `undefined method 'reject' for nil:NilClass` error for an invalid route, now returning 404 Not Found - [@dblock](https://github.com/dblock).
* Your Contribution Here

### 0.7.2 (February 6, 2014)
Expand Down
3 changes: 3 additions & 0 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,14 @@ def self.setup(options)

models = []
routes = target_class.combined_routes[params[:name]]
error!('Not Found', 404) unless routes

ops = routes.reject(&:route_hidden).group_by do |route|
parse_path(route.route_path, api_version)
end

error!('Not Found', 404) unless ops.any?

apis = []

ops.each do |path, op_routes|
Expand Down
25 changes: 25 additions & 0 deletions spec/non_default_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,29 @@ def app
expect(ret['apis'][0]['operations'][0]['nickname']).to eq 'getSomething'
end
end

context 'invalid name' do
subject do
Class.new(Grape::API) do
get 'hidden', hidden: true
add_swagger_documentation
end
end

def app
subject
end

it 'returns a 404 for an non-existent route' do
get '/swagger_doc/invalid.json'
expect(last_response.status).to eq 404
expect(JSON.parse(last_response.body)).to eq('error' => 'Not Found')
end

it 'returns a 404 for a hidden route' do
get '/swagger_doc/hidden.json'
expect(last_response.status).to eq 404
expect(JSON.parse(last_response.body)).to eq('error' => 'Not Found')
end
end
end

0 comments on commit 383d838

Please sign in to comment.