Skip to content

Commit 2fc603d

Browse files
author
LeFnord
committed
issue #584: do not mutate route.path
- adds changelog entry
1 parent 6fc47e0 commit 2fc603d

File tree

9 files changed

+37
-33
lines changed

9 files changed

+37
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#### Fixes
1010

1111
* [#580](https://github.com/ruby-grape/grape-swagger/pull/580): Issue #578: fixes duplicated path params - [@LeFnord](https://github.com/LeFnord).
12+
* [#585](https://github.com/ruby-grape/grape-swagger/pull/585): Issue #584: do not mutate route.path - [@LeFnord](https://github.com/LeFnord).
1213

1314
* Your contribution here.
1415

lib/grape-swagger/doc_methods/move_params.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ def can_be_moved?(params, http_verb)
88
move_methods.include?(http_verb) && includes_body_param?(params)
99
end
1010

11-
def to_definition(params, route, definitions)
11+
def to_definition(path, params, route, definitions)
1212
@definitions = definitions
1313
unify!(params)
1414

1515
params_to_move = movable_params(params)
1616

1717
return (params + correct_array_param(params_to_move)) if should_correct_array?(params_to_move)
1818

19-
params << parent_definition_of_params(params_to_move, route)
19+
params << parent_definition_of_params(params_to_move, path, route)
2020

2121
params
2222
end
@@ -33,8 +33,8 @@ def correct_array_param(param)
3333
param
3434
end
3535

36-
def parent_definition_of_params(params, route)
37-
definition_name = OperationId.manipulate(parse_model(route.path))
36+
def parent_definition_of_params(params, path, route)
37+
definition_name = OperationId.manipulate(parse_model(path))
3838
referenced_definition = build_definition(definition_name, params, route.request_method.downcase)
3939
definition = @definitions[referenced_definition]
4040

lib/grape-swagger/doc_methods/operation_id.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ class OperationId
44
class << self
55
def build(route, path = nil)
66
if route.options[:nickname]
7-
operation_id = route.options[:nickname]
7+
route.options[:nickname]
88
else
99
verb = route.request_method.to_s.downcase
10-
1110
operation = manipulate(path) unless path.nil?
12-
13-
operation_id = "#{verb}#{operation}"
11+
"#{verb}#{operation}"
1412
end
15-
16-
operation_id
1713
end
1814

1915
def manipulate(path)

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ module GrapeSwagger
22
module DocMethods
33
class ParseParams
44
class << self
5-
def call(param, settings, route, definitions)
6-
path = route.path
5+
def call(param, settings, path, route, definitions)
76
method = route.request_method
8-
97
additional_documentation = settings.fetch(:documentation, {})
108
settings.merge!(additional_documentation)
119
data_type = DataType.call(settings)

lib/grape-swagger/doc_methods/path_string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module DocMethods
33
class PathString
44
class << self
55
def build(route, options = {})
6-
path = route.path
6+
path = route.path.dup
77
# always removing format
88
path.sub!(/\(\.\w+?\)$/, '')
99
path.sub!('(.:format)', '')

lib/grape-swagger/endpoint.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ def method_object(route, options, path)
108108
method[:description] = description_object(route)
109109
method[:produces] = produces_object(route, options[:produces] || options[:format])
110110
method[:consumes] = consumes_object(route, options[:format])
111-
method[:parameters] = params_object(route)
111+
method[:parameters] = params_object(route, path)
112112
method[:security] = security_object(route)
113113
method[:responses] = response_object(route)
114-
method[:tags] = route.options.fetch(:tags, tag_object(route))
114+
method[:tags] = route.options.fetch(:tags, tag_object(route, path))
115115
method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
116116
method.delete_if { |_, value| value.blank? }
117117

@@ -161,7 +161,7 @@ def consumes_object(route, format)
161161
mime_types
162162
end
163163

164-
def params_object(route)
164+
def params_object(route, path)
165165
parameters = partition_params(route).map do |param, value|
166166
value = { required: false }.merge(value) if value.is_a?(Hash)
167167
_, value = default_type([[param, value]]).first if value == ''
@@ -170,11 +170,11 @@ def params_object(route)
170170
elsif value[:documentation]
171171
expose_params(value[:documentation][:type])
172172
end
173-
GrapeSwagger::DocMethods::ParseParams.call(param, value, route, @definitions)
173+
GrapeSwagger::DocMethods::ParseParams.call(param, value, path, route, @definitions)
174174
end
175175

176176
if GrapeSwagger::DocMethods::MoveParams.can_be_moved?(parameters, route.request_method)
177-
parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(parameters, route, @definitions)
177+
parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(path, parameters, route, @definitions)
178178
end
179179

180180
parameters
@@ -227,11 +227,11 @@ def apply_success_codes(route)
227227
[default_code]
228228
end
229229

230-
def tag_object(route)
230+
def tag_object(route, path)
231231
version = GrapeSwagger::DocMethods::Version.get(route)
232232
version = [version] unless version.is_a?(Array)
233233
Array(
234-
route.path.split('{')[0].split('/').reject(&:empty?).delete_if do |i|
234+
path.split('{')[0].split('/').reject(&:empty?).delete_if do |i|
235235
i == route.prefix.to_s || version.map(&:to_s).include?(i)
236236
end.first
237237
)

spec/lib/move_params_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,19 @@
9191
end
9292

9393
describe 'parent_definition_of_params' do
94+
let(:path) { '/in_body' }
9495
describe 'POST' do
95-
let(:params) { paths['/in_body'][:post][:parameters] }
96+
let(:params) { paths[path][:post][:parameters] }
9697
let(:options) do
9798
{
9899
method: 'POST'
99100
}
100101
end
101-
let(:env) { Rack::MockRequest.env_for('/in_body', options) }
102+
let(:env) { Rack::MockRequest.env_for(path, options) }
102103
let(:request) { Grape::Request.new(env) }
103104

104105
specify do
105-
subject.to_definition(params, request, definitions)
106+
subject.to_definition(path, params, request, definitions)
106107
expect(params).to eql(
107108
[
108109
{ name: 'InBody', in: 'body', required: true, schema: { '$ref' => '#/definitions/postInBody' } }
@@ -120,11 +121,11 @@
120121
method: 'PUT'
121122
}
122123
end
123-
let(:env) { Rack::MockRequest.env_for('/in_body', options) }
124+
let(:env) { Rack::MockRequest.env_for(path, options) }
124125
let(:request) { Grape::Request.new(env) }
125126

126127
specify do
127-
subject.to_definition(params, request, definitions)
128+
subject.to_definition(path, params, request, definitions)
128129
expect(params).to eql(
129130
[
130131
{ in: 'path', name: 'key', description: nil, type: 'integer', format: 'int32', required: true },

spec/lib/path_string_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
specify { expect(subject).to eql GrapeSwagger::DocMethods::PathString }
77
specify { expect(subject).to respond_to :build }
88

9-
describe 'operation_id_object' do
9+
describe 'path_string_object' do
10+
specify 'The original route path is not mutated' do
11+
route = Struct.new(:version, :path).new
12+
route.path = '/foo/:dynamic/bar'
13+
subject.build(route, add_version: true)
14+
expect(route.path).to eq '/foo/:dynamic/bar'
15+
end
16+
1017
describe 'version' do
1118
describe 'defaults: given, true' do
1219
let(:options) { { add_version: true } }

spec/lib/tag_name_description_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
end
1616

1717
describe '#build' do
18-
subject { described_class.build(paths) }
18+
let(:object) { described_class.build(paths) }
19+
1920
describe 'empty paths' do
2021
let(:paths) { {} }
2122
specify do
22-
expect(subject).to eql([])
23+
expect(object).to eql([])
2324
end
2425
end
2526

@@ -30,7 +31,7 @@
3031
end
3132

3233
specify do
33-
expect(subject).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
34+
expect(object).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
3435
end
3536
end
3637

@@ -40,7 +41,7 @@
4041
end
4142

4243
specify do
43-
expect(subject).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
44+
expect(object).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
4445
end
4546
end
4647

@@ -53,7 +54,7 @@
5354
end
5455

5556
specify do
56-
expect(subject).to eql [
57+
expect(object).to eql [
5758
{ name: 'tags_given', description: 'Operations about tags_givens' },
5859
{ name: 'another_tag_given', description: 'Operations about another_tag_givens' }
5960
]
@@ -68,7 +69,7 @@
6869
end
6970

7071
specify do
71-
expect(subject).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
72+
expect(object).to eql [{ name: 'tags_given', description: 'Operations about tags_givens' }]
7273
end
7374
end
7475
end

0 commit comments

Comments
 (0)