Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API only, multi DB. Undefined method `id' for {}:Hash #50398

Closed
maxrosecollins opened this issue Dec 19, 2023 · 8 comments
Closed

API only, multi DB. Undefined method `id' for {}:Hash #50398

maxrosecollins opened this issue Dec 19, 2023 · 8 comments

Comments

@maxrosecollins
Copy link

I am upgrading to 7.1.2 from 7.0.7.
It is api_only.
I am using multi db.

I am pretty sure it is related to adding session management back into an api only app and using multi db but I don't know the root cause.

After upgrading, to 7.1.2 I get errors on all request specs.

undefined method `id' for {}:Hash

> To access an interactive console with this error, point your browser to: /__better_errors


spec/requests/domains_spec.rb, line 298
---------------------------------------

ruby
  293     end
  294
  295     # Test suite for GET /v1/domains
  296     describe 'GET /v1/domains' do
  297       before do
> 298         get '/v1/domains',
  299             headers: {'ACCEPT'        => 'application/json',
  300                       'Authorization' => "Bearer #{token}"}
  301       end
  302
  303       context 'when domain exists' do


App backtrace
-------------

 - spec/requests/domains_spec.rb:298:in `block (3 levels) in <top (required)>'
 - spec/rails_helper.rb:103:in `block (3 levels) in <top (required)>'
 - spec/rails_helper.rb:102:in `block (2 levels) in <top (required)>'

Full backtrace
--------------

 - rack (2.2.8) lib/rack/session/abstract/id.rb:329:in `current_session_id'
 - rack (2.2.8) lib/rack/session/abstract/id.rb:335:in `session_exists?'
 - actionpack (7.1.2) lib/action_dispatch/request/session.rb:230:in `exists?'
 - actionpack (7.1.2) lib/action_dispatch/request/session.rb:270:in `load!'
 - actionpack (7.1.2) lib/action_dispatch/request/session.rb:258:in `load_for_write!'
 - actionpack (7.1.2) lib/action_dispatch/request/session.rb:185:in `update'
 - actionpack (7.1.2) lib/action_dispatch/request/session.rb:20:in `create'
 - actionpack (7.1.2) lib/action_dispatch/middleware/session/abstract_store.rb:76:in `prepare_session'
 - rack (2.2.8) lib/rack/session/abstract/id.rb:265:in `context'
 - rack (2.2.8) lib/rack/session/abstract/id.rb:260:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/cookies.rb:689:in `call'
 - rack-attack (6.7.0) lib/rack/attack.rb:127:in `call'
 - activerecord (7.1.2) lib/active_record/middleware/database_selector.rb:67:in `block in call'
 - activesupport (7.1.2) lib/active_support/notifications/instrumenter.rb:58:in `instrument'
 - activerecord (7.1.2) lib/active_record/middleware/database_selector/resolver.rb:58:in `block in read_from_primary'
 - activerecord (7.1.2) lib/active_record/connection_handling.rb:361:in `with_role_and_shard'
 - activerecord (7.1.2) lib/active_record/connection_handling.rb:147:in `connected_to'
 - activerecord (7.1.2) lib/active_record/middleware/database_selector/resolver.rb:57:in `read_from_primary'
 - activerecord (7.1.2) lib/active_record/middleware/database_selector/resolver.rb:37:in `read'
 - activerecord (7.1.2) lib/active_record/middleware/database_selector.rb:77:in `select_database'
 - activerecord (7.1.2) lib/active_record/middleware/database_selector.rb:66:in `call'
 - rack (2.2.8) lib/rack/etag.rb:27:in `call'
 - rack (2.2.8) lib/rack/conditional_get.rb:27:in `call'
 - rack (2.2.8) lib/rack/head.rb:12:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 - activesupport (7.1.2) lib/active_support/callbacks.rb:101:in `run_callbacks'
 - actionpack (7.1.2) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/actionable_exceptions.rb:16:in `call'
 - appsignal (3.4.10) lib/appsignal/rack/rails_instrumentation.rb:32:in `call_with_appsignal_monitoring'
 - appsignal (3.4.10) lib/appsignal/rack/rails_instrumentation.rb:17:in `call'
 - better_errors (2.10.1) lib/better_errors/middleware.rb:87:in `protected_app_call'
 - better_errors (2.10.1) lib/better_errors/middleware.rb:82:in `better_errors_call'
 - better_errors (2.10.1) lib/better_errors/middleware.rb:60:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
 - railties (7.1.2) lib/rails/rack/logger.rb:37:in `call_app'
 - railties (7.1.2) lib/rails/rack/logger.rb:24:in `block in call'
 - activesupport (7.1.2) lib/active_support/tagged_logging.rb:135:in `block in tagged'
 - activesupport (7.1.2) lib/active_support/tagged_logging.rb:39:in `tagged'
 - activesupport (7.1.2) lib/active_support/tagged_logging.rb:135:in `tagged'
 - activesupport (7.1.2) lib/active_support/broadcast_logger.rb:240:in `method_missing'
 - railties (7.1.2) lib/rails/rack/logger.rb:24:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/remote_ip.rb:92:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/request_id.rb:28:in `call'
 - rack (2.2.8) lib/rack/runtime.rb:22:in `call'
 - activesupport (7.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
 - actionpack (7.1.2) lib/action_dispatch/middleware/static.rb:25:in `call'
 - rack (2.2.8) lib/rack/sendfile.rb:110:in `call'
 - rack-cors (2.0.1) lib/rack/cors.rb:102:in `call'
 - railties (7.1.2) lib/rails/engine.rb:529:in `call'
 - rack-test (2.1.0) lib/rack/test.rb:360:in `process_request'
 - rack-test (2.1.0) lib/rack/test.rb:153:in `request'
 - actionpack (7.1.2) lib/action_dispatch/testing/integration.rb:288:in `process'
 - actionpack (7.1.2) lib/action_dispatch/testing/integration.rb:16:in `get'
 - actionpack (7.1.2) lib/action_dispatch/testing/integration.rb:379:in `get'
 - spec/requests/domains_spec.rb:298:in `block (3 levels) in <top (required)>'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:457:in `instance_exec'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:365:in `run'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:529:in `block in run_owned_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:528:in `run_owned_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:615:in `block in run_example_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:614:in `run_example_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:484:in `run'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:505:in `run_before_example'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:261:in `block in run'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:511:in `block in with_around_and_singleton_context_hooks'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:468:in `block in with_around_example_hooks'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:486:in `block in run'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:626:in `block in run_around_example_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:352:in `call'
 - rspec-rails (6.1.0) lib/rspec/rails/adapters.rb:75:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:457:in `instance_exec'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:390:in `execute_with'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:628:in `block (2 levels) in run_around_example_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:352:in `call'
 - spec/rails_helper.rb:103:in `block (3 levels) in <top (required)>'
 - database_cleaner-core (2.0.1) lib/database_cleaner/strategy.rb:30:in `cleaning'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/3.2.0/forwardable.rb:240:in `cleaning'
 - database_cleaner-core (2.0.1) lib/database_cleaner/cleaners.rb:34:in `block (2 levels) in cleaning'
 - database_cleaner-core (2.0.1) lib/database_cleaner/cleaners.rb:35:in `cleaning'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/3.2.0/forwardable.rb:240:in `cleaning'
 - spec/rails_helper.rb:102:in `block (2 levels) in <top (required)>'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:457:in `instance_exec'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:390:in `execute_with'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:628:in `block (2 levels) in run_around_example_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:352:in `call'
 - webmock (3.18.1) lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:457:in `instance_exec'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:390:in `execute_with'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:628:in `block (2 levels) in run_around_example_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:352:in `call'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:629:in `run_around_example_hooks_for'
 - rspec-core (3.12.2) lib/rspec/core/hooks.rb:486:in `run'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:468:in `with_around_example_hooks'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:511:in `with_around_and_singleton_context_hooks'
 - rspec-core (3.12.2) lib/rspec/core/example.rb:259:in `run'
 - rspec-core (3.12.2) lib/rspec/core/example_group.rb:646:in `block in run_examples'
 - rspec-core (3.12.2) lib/rspec/core/example_group.rb:642:in `run_examples'
 - rspec-core (3.12.2) lib/rspec/core/example_group.rb:607:in `run'
 - rspec-core (3.12.2) lib/rspec/core/example_group.rb:608:in `block in run'
 - rspec-core (3.12.2) lib/rspec/core/example_group.rb:608:in `run'
 - rspec-core (3.12.2) lib/rspec/core/example_group.rb:608:in `block in run'
 - rspec-core (3.12.2) lib/rspec/core/example_group.rb:608:in `run'
 - rspec-core (3.12.2) lib/rspec/core/runner.rb:121:in `block (3 levels) in run_specs'
 - rspec-core (3.12.2) lib/rspec/core/runner.rb:121:in `block (2 levels) in run_specs'
 - rspec-core (3.12.2) lib/rspec/core/configuration.rb:2070:in `with_suite_hooks'
 - rspec-core (3.12.2) lib/rspec/core/runner.rb:116:in `block in run_specs'
 - rspec-core (3.12.2) lib/rspec/core/reporter.rb:74:in `report'
 - rspec-core (3.12.2) lib/rspec/core/runner.rb:115:in `run_specs'
 - rspec-core (3.12.2) lib/rspec/core/runner.rb:89:in `run'
 - rspec-core (3.12.2) lib/rspec/core/runner.rb:71:in `run'
 - rspec-core (3.12.2) lib/rspec/core/runner.rb:45:in `invoke'
 - rspec-core (3.12.2) exe/rspec:4:in `<top (required)>'
 - /Users/max/.rbenv/versions/3.2.2/bin/rspec:25:in `<top (required)>'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/cli/exec.rb:58:in `kernel_load'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/cli/exec.rb:23:in `run'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/cli.rb:492:in `exec'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/cli.rb:34:in `dispatch'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/cli.rb:28:in `start'
 - bundler (2.4.19) exe/bundle:37:in `block in <top (required)>'
 - /Users/max/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/bundler/friendly_errors.rb:117:in `with_friendly_errors'
 - bundler (2.4.19) exe/bundle:29:in `<top (required)>'
 - /Users/max/.rbenv/versions/3.2.2/bin/bundle:25:in `<main>'
@maxrosecollins maxrosecollins changed the title undefined method `id' for {}:Hash API only, multi DB. Undefined method `id' for {}:Hash Dec 19, 2023
@fatkodima
Copy link
Member

Can you create a sample app that reproduces the issue?

@fatkodima fatkodima added the more-information-needed When reporter needs to provide more information label Dec 22, 2023
@agrobbin
Copy link
Contributor

We ran into this as well, but ended up finding that it wasn't an issue with Rails itself, but rather an incompatibility between Rails / Rack and WebMock's RackResponse functionality (bblimke/webmock#985). Our workaround was to force session to be nil, so that Rails sets the default session appropriately.

@bparanj
Copy link
Contributor

bparanj commented Dec 25, 2023

In order to isolate the problem to either Rails or RSpec. Please create a reproducible test case using this template:

https://github.com/rails/rails/blob/main/guides/bug_report_templates/action_controller.rb

if this is not applicable, use the genertic template:

https://github.com/rails/rails/blob/main/guides/bug_report_templates/generic.rb

@maxrosecollins
Copy link
Author

@agrobbin

Did you do this in environments/test.rb?

    def session
      fetch_header(Rack::RACK_SESSION) do |k|
        set_header Rack::RACK_SESSION, default_session
      end
    end

    private
    def default_session
      self.session_options = {}
      self.session = nil
    end
  end
end

@rails-bot rails-bot bot removed the more-information-needed When reporter needs to provide more information label Dec 28, 2023
@agrobbin
Copy link
Contributor

@maxrosecollins we patched WebMock::RackResponse:

module WebMock
  class RackResponse < Response
    def session
      nil
    end

    def session_options
      nil
    end
  end
end

@skipkayhil skipkayhil added the more-information-needed When reporter needs to provide more information label Dec 28, 2023
@maxrosecollins
Copy link
Author

@agrobbin

It looks like it is the same issue then.

@rails-bot rails-bot bot removed the more-information-needed When reporter needs to provide more information label Dec 29, 2023
@skipkayhil skipkayhil added the more-information-needed When reporter needs to provide more information label Dec 29, 2023
@zzak zzak added third party issue and removed more-information-needed When reporter needs to provide more information labels Dec 31, 2023
@zzak
Copy link
Member

zzak commented Dec 31, 2023

This looks like it's resolved, and the problem was a third party. Please re-open if you have new information or open a new bug. 🙇

@zzak zzak closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2023
@magni-
Copy link

magni- commented May 30, 2024

For anyone else coming here via Google, looks like this is related to #44261 https://github.com/rails/rails/pull/44283/files#diff-40af0c0e76bdb69f4240deed5d844e4564115d291f8f73b0c4ebf523a1ed16bdR270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants