Skip to content

Commit

Permalink
Fix support for Hanami 2.1
Browse files Browse the repository at this point in the history
In Hanami 2.1 the `sessions_enabled` keyword argument was renamed to
`session_enabled` for the `Hanami::Action::Request` class. Upon closer
inspection, the `Hanami::Action::Request` class does nothing we need, so
let's remove our dependency on it.

To fetch the request parameters, directly ask the Hanami Action request
parameters class. This is (also) a private API, but limits the surface
area in which things can break in the next release. I'll continue
looking at a better to fix this.

Fix specs for the Hanami middleware options. They added another item to
the registered middleware list.
  • Loading branch information
tombruijn committed Jun 27, 2024
1 parent d299589 commit a7b056b
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .changesets/support-hanami-2-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: fix
---

Support Hanami version 2.1. On older versions of our Ruby gem it would error on an unknown keyword argument "sessions_enabled".
96 changes: 84 additions & 12 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ blocks:
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.0.5 for hanami
- name: Ruby 3.0.5 for hanami-2.0
env_vars:
- *2
- *3
Expand All @@ -668,9 +668,27 @@ blocks:
- name: RUBY_VERSION
value: 3.0.5
- name: GEMSET
value: hanami
value: hanami-2.0
- name: BUNDLE_GEMFILE
value: gemfiles/hanami.gemfile
value: gemfiles/hanami-2.0.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.0.5 for hanami-2.1
env_vars:
- *2
- *3
- *4
- *5
- name: RUBY_VERSION
value: 3.0.5
- name: GEMSET
value: hanami-2.1
- name: BUNDLE_GEMFILE
value: gemfiles/hanami-2.1.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
Expand Down Expand Up @@ -1094,7 +1112,25 @@ blocks:
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.1.3 for hanami
- name: Ruby 3.1.3 for hanami-2.0
env_vars:
- *2
- *3
- *4
- *5
- name: RUBY_VERSION
value: 3.1.3
- name: GEMSET
value: hanami-2.0
- name: BUNDLE_GEMFILE
value: gemfiles/hanami-2.0.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.1.3 for hanami-2.1
env_vars:
- *2
- *3
Expand All @@ -1103,9 +1139,9 @@ blocks:
- name: RUBY_VERSION
value: 3.1.3
- name: GEMSET
value: hanami
value: hanami-2.1
- name: BUNDLE_GEMFILE
value: gemfiles/hanami.gemfile
value: gemfiles/hanami-2.1.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
Expand Down Expand Up @@ -1511,7 +1547,7 @@ blocks:
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.2.1 for hanami
- name: Ruby 3.2.1 for hanami-2.0
env_vars:
- *2
- *3
Expand All @@ -1520,9 +1556,27 @@ blocks:
- name: RUBY_VERSION
value: 3.2.1
- name: GEMSET
value: hanami
value: hanami-2.0
- name: BUNDLE_GEMFILE
value: gemfiles/hanami.gemfile
value: gemfiles/hanami-2.0.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.2.1 for hanami-2.1
env_vars:
- *2
- *3
- *4
- *5
- name: RUBY_VERSION
value: 3.2.1
- name: GEMSET
value: hanami-2.1
- name: BUNDLE_GEMFILE
value: gemfiles/hanami-2.1.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
Expand Down Expand Up @@ -1928,7 +1982,25 @@ blocks:
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.3.1 for hanami
- name: Ruby 3.3.1 for hanami-2.0
env_vars:
- *2
- *3
- *4
- *5
- name: RUBY_VERSION
value: 3.3.1
- name: GEMSET
value: hanami-2.0
- name: BUNDLE_GEMFILE
value: gemfiles/hanami-2.0.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
value: latest
commands:
- "./support/bundler_wrapper exec rake test"
- name: Ruby 3.3.1 for hanami-2.1
env_vars:
- *2
- *3
Expand All @@ -1937,9 +2009,9 @@ blocks:
- name: RUBY_VERSION
value: 3.3.1
- name: GEMSET
value: hanami
value: hanami-2.1
- name: BUNDLE_GEMFILE
value: gemfiles/hanami.gemfile
value: gemfiles/hanami-2.1.gemfile
- name: _RUBYGEMS_VERSION
value: latest
- name: _BUNDLER_VERSION
Expand Down
9 changes: 8 additions & 1 deletion build_matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,14 @@ matrix:
- "3.2.1"
- "3.3.1"
- gem: "grape"
- gem: "hanami"
- gem: "hanami-2.0"
only:
ruby:
- "3.0.5"
- "3.1.3"
- "3.2.1"
- "3.3.1"
- gem: "hanami-2.1"
only:
ruby:
- "3.0.5"
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions gemfiles/hanami-2.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://rubygems.org'

gem "hanami", "~> 2.1.0"
gem "hanami-router", "~> 2.1.0"
gem "hanami-controller", "~> 2.1.0"

gemspec :path => '../'
12 changes: 1 addition & 11 deletions lib/appsignal/rack/hanami_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Rack
# @api private
class HanamiMiddleware < AbstractMiddleware
def initialize(app, options = {})
options[:request_class] ||= ::Hanami::Action::Request
options[:params_method] ||= :params
options[:instrument_span_name] ||= "process_action.hanami"
super
Expand All @@ -14,16 +13,7 @@ def initialize(app, options = {})
private

def params_for(request)
super&.to_h
end

def request_for(env)
params = ::Hanami::Action.params_class.new(env)
@request_class.new(
:env => env,
:params => params,
:sessions_enabled => true
)
::Hanami::Action.params_class.new(request.env).to_h
end
end
end
Expand Down
16 changes: 12 additions & 4 deletions spec/lib/appsignal/integrations/hanami_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def uninstall_hanami_middleware

expect(::Hanami.app.config.middleware.stack[::Hanami::Router::DEFAULT_PREFIX])
.to include(
[Rack::Events, [[kind_of(Appsignal::Rack::EventHandler)]], nil],
[Appsignal::Rack::HanamiMiddleware, [], nil]
[Rack::Events, [[kind_of(Appsignal::Rack::EventHandler)]], *hanami_middleware_options],
[Appsignal::Rack::HanamiMiddleware, [], *hanami_middleware_options]
)
end

Expand All @@ -54,10 +54,10 @@ def uninstall_hanami_middleware

middleware_stack = ::Hanami.app.config.middleware.stack[::Hanami::Router::DEFAULT_PREFIX]
expect(middleware_stack).to_not include(
[Rack::Events, [[kind_of(Appsignal::Rack::EventHandler)]], nil]
[Rack::Events, [[kind_of(Appsignal::Rack::EventHandler)]], *hanami_middleware_options]
)
expect(middleware_stack).to_not include(
[Appsignal::Rack::HanamiMiddleware, [], nil]
[Appsignal::Rack::HanamiMiddleware, [], *hanami_middleware_options]
)
end
end
Expand Down Expand Up @@ -123,5 +123,13 @@ def make_request(env, app: HanamiApp::Actions::Books::Index)
end
end
end

def hanami_middleware_options
if DependencyHelper.hanami2_1_present?
[{}, nil]
else
[nil]
end
end
end
end
5 changes: 5 additions & 0 deletions spec/support/helpers/dependency_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ def hanami_present?
dependency_present? "hanami"
end

def hanami2_1_present?
hanami_present? &&
Gem::Version.new(::Hanami::VERSION) >= Gem::Version.new("2.1.0")
end

def dry_monitor_present?
dependency_present? "dry-monitor"
end
Expand Down

0 comments on commit a7b056b

Please sign in to comment.