diff --git a/docker/Dockerfile b/docker/Dockerfile index 2bc484ecf..5cca3aa5b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -7,7 +7,7 @@ ENV RUBYOPT --enable-frozen-string-literal --yjit ENV LD_PRELOAD libjemalloc.so.2 ENV MALLOC_CONF dirty_decay_ms:1000,narenas:2,background_thread:true -RUN apk add --update --no-cache make gcc git libc-dev gcompat jemalloc && \ +RUN apk add --update --no-cache make gcc git libc-dev yaml-dev gcompat jemalloc && \ gem update --system && gem install bundler WORKDIR $LIB_PATH diff --git a/lib/grape.rb b/lib/grape.rb index a1e6f511c..4dfffc257 100644 --- a/lib/grape.rb +++ b/lib/grape.rb @@ -75,6 +75,7 @@ def self.deprecator configure do |config| config.param_builder = :hash_with_indifferent_access + config.lint = false config.compile_methods! end end diff --git a/lib/grape/dsl/routing.rb b/lib/grape/dsl/routing.rb index 8c32db364..4db89e5d3 100644 --- a/lib/grape/dsl/routing.rb +++ b/lib/grape/dsl/routing.rb @@ -81,6 +81,10 @@ def do_not_route_options! namespace_inheritable(:do_not_route_options, true) end + def lint! + namespace_inheritable(:lint, true) + end + def do_not_document! namespace_inheritable(:do_not_document, true) end diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 2e5136fda..e3bd6b61f 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -358,6 +358,7 @@ def build_stack(helpers) format = namespace_inheritable(:format) stack.use Rack::Head + stack.use Rack::Lint if lint? stack.use Class.new(Grape::Middleware::Error), helpers: helpers, format: format, @@ -408,5 +409,9 @@ def build_response_cookies Rack::Utils.set_cookie_header! header, name, cookie_value end end + + def lint? + namespace_inheritable(:lint) || Grape.config.lint + end end end diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index f16b31ee2..b530a3deb 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -4738,4 +4738,26 @@ def uniqe_id_route expect(last_response.body).to eq('unknown params_builder: unknown') end end + + describe '.lint!' do + let(:app) do + Class.new(described_class) do + lint! + get '/' do + status 42 + end + end + end + + around do |example| + Grape.config.lint = false + example.run + Grape.config.lint = true + end + + it 'raises a Rack::Lint error' do + # Status must be an Integer >= 100 + expect { get '/' }.to raise_error(Rack::Lint::LintError) + end + end end diff --git a/spec/integration/rails/mounting_spec.rb b/spec/integration/rails/mounting_spec.rb index 3d8288b93..eef8e9ad2 100644 --- a/spec/integration/rails/mounting_spec.rb +++ b/spec/integration/rails/mounting_spec.rb @@ -17,7 +17,7 @@ ActiveSupport::Dependencies.autoload_paths = [] ActiveSupport::Dependencies.autoload_once_paths = [] - Class.new(Rails::Application) do + app = Class.new(Rails::Application) do config.eager_load = false config.load_defaults "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" config.api_only = true @@ -28,15 +28,16 @@ mount GrapeApi => '/' get 'up', to: lambda { |_env| - ['200', {}, ['hello world']] + [200, {}, ['hello world']] } end end + app.initialize! + Rack::Lint.new(app) end before do stub_const('GrapeApi', api) - app.initialize! end it 'cascades' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 06cb282a0..4a8e2f1b0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,7 @@ end end +Grape.config.lint = true # lint all apis by default Grape::Util::Registry.include(Deregister) # issue with ruby 2.7 with ^. We need to extend it again Grape::Validations.extend(Grape::Util::Registry) if Gem::Version.new(RUBY_VERSION).release < Gem::Version.new('3.0')