Skip to content

Commit 5a93cd7

Browse files
kgilpinapotterri
authored andcommitted
feat: Emit swagger-style normalized paths instead of Rails-style ones
1 parent b6eaacb commit 5a93cd7

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

lib/appmap/handler/rails/request_handler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def normalized_path(request, router = ::Rails.application.routes.router)
6565
next unless app.matches? request
6666
return normalized_path request, app.rack_app.routes.router if app.engine?
6767

68-
return route.path.spec.to_s
68+
return AppMap::Util.swaggerize_path(route.path.spec.to_s)
6969
end
7070
end
7171
end

lib/appmap/util.rb

+10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ def normalize_path(path)
101101
end
102102
end
103103

104+
# Convert a Rails-style path from /org/:org_id(.:format)
105+
# to Swagger-style paths like /org/{org_id}
106+
def swaggerize_path(path)
107+
path = path.split('(.')[0]
108+
tokens = path.split('/')
109+
tokens.map do |token|
110+
token.gsub /^:(.*)/, '{\1}'
111+
end.join('/')
112+
end
113+
104114
# Atomically writes AppMap data to +filename+.
105115
def write_appmap(filename, appmap)
106116
require 'fileutils'

spec/abstract_controller_base_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def tmpdir
4242
hash_including(
4343
'http_server_request' => hash_including(
4444
'request_method' => 'POST',
45-
'normalized_path_info' => '/api/users(.:format)',
45+
'normalized_path_info' => '/api/users',
4646
'path_info' => '/api/users'
4747
),
4848
'message' => include(
@@ -197,7 +197,7 @@ def tmpdir
197197
'http_server_request' => {
198198
'request_method' => 'GET',
199199
'path_info' => '/users/alice',
200-
'normalized_path_info' => '/users/:id(.:format)',
200+
'normalized_path_info' => '/users/{id}',
201201
'headers' => {
202202
'Host' => 'test.host',
203203
'User-Agent' => 'Rails Testing'

spec/util_spec.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
require 'appmap/util'
55

66
describe AppMap::Util, docker: false do
7-
let(:subject) { AppMap::Util.method(:scenario_filename) }
87
describe 'scenario_filename' do
8+
let(:subject) { AppMap::Util.method(:scenario_filename) }
99
it 'leaves short names alone' do
1010
expect(subject.call('foobar')).to eq('foobar.appmap.json')
1111
end
@@ -18,4 +18,21 @@
1818
expect(subject.call(fname, max_length: 50)).to eq('abcdefghijklmno-RAd_SFbH1sUZ_OXfwPsfzw.appmap.json')
1919
end
2020
end
21+
describe 'swaggerize path' do
22+
it 'replaces rails-style parameters' do
23+
expect(AppMap::Util.swaggerize_path('/org/:org_id(.:format)')).to eq('/org/{org_id}')
24+
end
25+
26+
it 'strips the format specifier' do
27+
expect(AppMap::Util.swaggerize_path('/org(.:format)')).to eq('/org')
28+
end
29+
30+
it 'ignores malformed parameter specs' do
31+
expect(AppMap::Util.swaggerize_path('/org/o:rg_id')).to eq('/org/o:rg_id')
32+
end
33+
34+
it 'ignores already swaggerized paths' do
35+
expect(AppMap::Util.swaggerize_path('/org/{org_id}')).to eq('/org/{org_id}')
36+
end
37+
end
2138
end

0 commit comments

Comments
 (0)