diff --git a/CHANGELOG.md b/CHANGELOG.md index a2e783f8..40d07b0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index c208e14d..6e20d546 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -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| diff --git a/spec/non_default_api_spec.rb b/spec/non_default_api_spec.rb index f7bcd896..ee74789a 100644 --- a/spec/non_default_api_spec.rb +++ b/spec/non_default_api_spec.rb @@ -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