rspec-openapi
rspec-openapi copied to clipboard
Mounting engine to root "/" causes "No route matched for" in some scenarios
When running OPENAPI=1 bundle exec rspec any routes that are listed after mounting engine to root mount Breaker::Engine => "/" will generate No route matched for RuntimeError:
Failures:
1) Api::V1::TestersController#index returns hello
Failure/Error: raise "No route matched for #{request.request_method} #{request.path_info}"
RuntimeError:
No route matched for GET /testers
# /Users/trikic/.rvm/gems/ruby-3.0.3/gems/rspec-openapi-0.5.0/lib/rspec/openapi/record_builder.rb:90:in `find_rails_route'
# /Users/trikic/.rvm/gems/ruby-3.0.3/gems/rspec-openapi-0.5.0/lib/rspec/openapi/record_builder.rb:84:in `block in find_rails_route'
# /Users/trikic/.rvm/gems/ruby-3.0.3/gems/rspec-openapi-0.5.0/lib/rspec/openapi/record_builder.rb:81:in `find_rails_route'
# /Users/trikic/.rvm/gems/ruby-3.0.3/gems/rspec-openapi-0.5.0/lib/rspec/openapi/record_builder.rb:21:in `build'
# /Users/trikic/.rvm/gems/ruby-3.0.3/gems/rspec-openapi-0.5.0/lib/rspec/openapi/hooks.rb:14:in `block in <top (required)>'
Finished in 0.26384 seconds (files took 4.66 seconds to load)
1 example, 1 failure
Running bundle exec rspec does not generate such errors.
Here is a sample routes.rb:
namespace :api do
namespace :v1 do
# gives errors
# mount Breaker::Engine => "/", as: :breaker
# mount Breaker::Engine => "/"
# works
# mount Breaker::Engine => "/breaker"
resources :testers, only: [:index]
# works
mount Breaker::Engine => "/"
end
end
Full project that reproduces the error can be found at crnastena/openapi-test.
I guess this more of an enquiry as I am not sure if it is a problem with rspec-openapi or not. What do you think?
Thanks for preparing a repository. That is really helpful.
I tried reproducing it, but OPENAPI=1 bundle exec rspec did not produce errors and it correctly generated OpenAPI. Could you try reproducing the same thing on GitHub Actions in the repository? If I can reproduce it there, I should be able to fix the problem for that environment. Until I have at least one environment that reproduces the issue, I cannot work on it myself.
Hi @k0kubun, as is the repository does not generate error. You would have to change routes.rb:
namespace :api do
namespace :v1 do
# gives errors
# mount Breaker::Engine => "/", as: :breaker
mount Breaker::Engine => "/"
# works
# mount Breaker::Engine => "/breaker"
resources :testers, only: [:index]
# works
# mount Breaker::Engine => "/"
end
end
I will patch the repo to see if I can get Github Actions to reproduce the same thing.
Ok I have added Github Action to run bundle exec rake and OPENAPI=1 bundle exec rake. First task succeeds and second task fails.

Thank you, much appreciated. I checked if this was some sort of regression since we've had several Rails engine-related releases such as v0.3.5, v0.3.6, and v0.4.6. It doesn't seem like it; it's reproducible during v0.3.6 ~ v0.5.0, and engine support in v0.3.5 was not yet working properly.
I guess this more of an enquiry as I am not sure if it is a problem with rspec-openapi or not.
It indeed seems like a problem with rspec-openapi. We likely need to fix something in https://github.com/k0kubun/rspec-openapi/blob/v0.5.0/lib/rspec/openapi/record_builder.rb#L74-L91. I'll take a look once I find time to work on it. If you can send a pull request that fixes this, I can merge it too.
Hey :) It looks similar to an issue that I had in past: #37
At the time I prepared some changes on my fork, also with a not merged PR here. Can you try with this commit @crnastena? https://github.com/blocknotes/rspec-openapi/commit/f578eba824de88e3f1b8b52dc6a33e15257726a1
Assuming fixed by https://github.com/exoego/rspec-openapi/pull/113 and available in v0.9.0+. Please feel free to reopen if the issue still remains.