Skip to content

Commit 85f5b78

Browse files
committed
Add linting feature through Grape.config and lint_api! method
Add spec All specs are now linted
1 parent a80d2d4 commit 85f5b78

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

lib/grape.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def self.deprecator
7575

7676
configure do |config|
7777
config.param_builder = :hash_with_indifferent_access
78+
config.lint_api = false
7879
config.compile_methods!
7980
end
8081
end

lib/grape/dsl/routing.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def do_not_route_options!
8181
namespace_inheritable(:do_not_route_options, true)
8282
end
8383

84+
def lint_api!
85+
namespace_inheritable(:lint_api, true)
86+
end
87+
8488
def do_not_document!
8589
namespace_inheritable(:do_not_document, true)
8690
end

lib/grape/endpoint.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ def build_stack(helpers)
358358
format = namespace_inheritable(:format)
359359

360360
stack.use Rack::Head
361+
stack.use Rack::Lint if lint_api?
361362
stack.use Class.new(Grape::Middleware::Error),
362363
helpers: helpers,
363364
format: format,
@@ -408,5 +409,9 @@ def build_response_cookies
408409
Rack::Utils.set_cookie_header! header, name, cookie_value
409410
end
410411
end
412+
413+
def lint_api?
414+
namespace_inheritable(:lint_api) || Grape.config.lint_api
415+
end
411416
end
412417
end

spec/grape/api_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,4 +4738,25 @@ def uniqe_id_route
47384738
expect(last_response.body).to eq('unknown params_builder: unknown')
47394739
end
47404740
end
4741+
4742+
describe '.lint_api!' do
4743+
let(:app) do
4744+
Class.new(described_class) do
4745+
lint_api!
4746+
get '/' do
4747+
status 42
4748+
end
4749+
end
4750+
end
4751+
4752+
around do |example|
4753+
Grape.config.lint_api = false
4754+
example.run
4755+
Grape.config.lint_api = true
4756+
end
4757+
4758+
it 'raises a Rack::Lint error' do
4759+
expect { get '/' }.to raise_error(Rack::Lint::LintError, 'Status must be an Integer >=100')
4760+
end
4761+
end
47414762
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
end
1414
end
1515

16+
Grape.config.lint_api = true # lint all apis by default
1617
Grape::Util::Registry.include(Deregister)
1718
# issue with ruby 2.7 with ^. We need to extend it again
1819
Grape::Validations.extend(Grape::Util::Registry) if Gem::Version.new(RUBY_VERSION).release < Gem::Version.new('3.0')

0 commit comments

Comments
 (0)