Skip to content

Commit

Permalink
fix engine path
Browse files Browse the repository at this point in the history
  • Loading branch information
pcoliveira committed May 5, 2023
1 parent 6b6b9e0 commit 288b7c3
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,16 @@ def extract_request_attributes(request, example)
raw_path_params = request.path_parameters
path = request.path
if rails?
route = find_rails_route(request)
path = route.path.spec.to_s.delete_suffix('(.:format)')
# Reverse the destructive modification by Rails https://github.com/rails/rails/blob/v6.0.3.4/actionpack/lib/action_dispatch/journey/router.rb#L33-L41
fixed_request = request.dup
if request.script_name.present?
fixed_request.path_info = File.join(request.script_name, request.path_info)
end

route, path = find_rails_route(fixed_request)
raise "No route matched for #{fixed_request.request_method} #{fixed_request.path_info}" if route.nil?

path = path.delete_suffix('(.:format)')
summary ||= route.requirements[:action]
tags ||= [route.requirements[:controller]&.classify].compact
# :controller and :action always exist. :format is added when routes is configured as such.
Expand Down Expand Up @@ -99,23 +107,17 @@ def rack_test?(context)
end

# @param [ActionDispatch::Request] request
def find_rails_route(request, app: Rails.application, fix_path: true)
# Reverse the destructive modification by Rails https://github.com/rails/rails/blob/v6.0.3.4/actionpack/lib/action_dispatch/journey/router.rb#L33-L41
if fix_path && !request.script_name.empty?
request = request.dup
request.path_info = File.join(request.script_name, request.path_info)
end

def find_rails_route(request, app: Rails.application, path_prefix: "")
app.routes.router.recognize(request) do |route|
path = route.path.spec.to_s
if route.app.matches?(request)
if route.app.engine?
route = find_rails_route(request, app: route.app.app, fix_path: false)
route, path = find_rails_route(request, app: route.app.app, path_prefix: path)
next if route.nil?
end
return route
return [route, path_prefix + path]
end
end
raise "No route matched for #{request.request_method} #{request.path_info}" if fix_path
nil
end

Expand Down

0 comments on commit 288b7c3

Please sign in to comment.