Skip to content

Commit

Permalink
Merge pull request #113 from pcoliveira/fix-engine-path-resolution
Browse files Browse the repository at this point in the history
Fix engine path resolution
  • Loading branch information
exoego committed Sep 29, 2023
2 parents 6a1fbf4 + 8456b3f commit 74c05d3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
29 changes: 16 additions & 13 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ 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
fixed_request.path_info = File.join(request.script_name, request.path_info) if request.script_name.present?

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 @@ -102,21 +108,18 @@ 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)
return find_rails_route(request, app: route.app.app, fix_path: false) if route.app.engine?

return route
if route.app.engine?
route, path = find_rails_route(request, app: route.app.app, path_prefix: path)
next if route.nil?
end
return [route, path_prefix + path]
end
end
raise "No route matched for #{request.request_method} #{request.path_info}"
nil
end

# workaround to get real request parameters
Expand Down
9 changes: 9 additions & 0 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,12 @@ class EngineTest < ActionDispatch::IntegrationTest
assert_response 200
end
end

class EngineExtraRoutesTest < ActionDispatch::IntegrationTest
openapi!

test 'returns the block content' do
get '/my_engine/test'
assert_response 200
end
end
2 changes: 2 additions & 0 deletions spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Rails.application.routes.draw do
mount ::MyEngine::Engine => '/my_engine'

get '/my_engine/test' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['ANOTHER TEST']] }

defaults format: 'json' do
resources :tables, only: [:index, :show, :create, :update, :destroy]
resources :images, only: [:index, :show] do
Expand Down
25 changes: 23 additions & 2 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@
}
}
},
"/eng_route": {
"/my_engine/eng_route": {
"get": {
"summary": "GET /eng_route",
"summary": "GET /my_engine/eng_route",
"tags": [

],
Expand All @@ -741,6 +741,27 @@
}
}
},
"/my_engine/test": {
"get": {
"summary": "GET /my_engine/test",
"tags": [

],
"responses": {
"200": {
"description": "returns the block content",
"content": {
"text/plain": {
"schema": {
"type": "string"
},
"example": "ANOTHER TEST"
}
}
}
}
}
},
"/images": {
"get": {
"summary": "index",
Expand Down
16 changes: 14 additions & 2 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ paths:
schema:
type: string
example: A TEST
"/eng_route":
"/my_engine/eng_route":
get:
summary: GET /eng_route
summary: GET /my_engine/eng_route
tags: []
responses:
'200':
Expand Down Expand Up @@ -526,6 +526,18 @@ paths:
example:
- name: file.png
tags: []
"/my_engine/test":
get:
summary: GET /my_engine/test
tags: []
responses:
'200':
description: returns the block content
content:
text/plain:
schema:
type: string
example: ANOTHER TEST
"/images/upload":
post:
summary: upload
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,12 @@
end
end
end

RSpec.describe 'Engine extra routes', type: :request do
describe '#test' do
it 'returns the block content' do
get '/my_engine/test'
expect(response.status).to eq(200)
end
end
end

0 comments on commit 74c05d3

Please sign in to comment.