Skip to content

Commit 7abb1e2

Browse files
committed
Fix documentation of route_param type when used with nested endpoints
1 parent 4ea61c4 commit 7abb1e2

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
### Next
2-
3-
#### Features
4-
5-
* Your contribution here.
1+
### 1.4.3
62

73
#### Fixes
84

9-
* Your contribution here.
5+
* [#840](https://github.com/ruby-grape/grape-swagger/pull/847): Fixes documentation of `route_param` type when used with nested endpoints - [@dmoss18](https://github.com/dmoss18)
106

117

128
### 1.4.2 (October 22, 2021)

lib/grape-swagger/endpoint.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,38 @@ def build_request_params(route, settings)
348348
end
349349

350350
def merge_params(route)
351+
path_params = get_path_params(route.app&.inheritable_setting&.namespace_stackable)
351352
param_keys = route.params.keys
353+
354+
# Merge path params options into route params
355+
route_params = route.params
356+
route_params.keys.each do |key|
357+
path = path_params[key] || {}
358+
params = route_params[key]
359+
params = {} if !params.is_a? Hash
360+
route_params[key] = path.merge(params)
361+
end
362+
352363
route.params.delete_if { |key| key.is_a?(String) && param_keys.include?(key.to_sym) }.to_a
353364
end
354365

366+
# Iterates over namespaces recursively
367+
# to build a hash of path params with options, including type
368+
def get_path_params(stackable_values)
369+
params = {}
370+
return param unless stackable_values
371+
return params unless stackable_values.is_a? Grape::Util::StackableValues
372+
stackable_values&.new_values[:namespace]&.each do |namespace|
373+
space = namespace.space.to_s.gsub(':', '')
374+
params[space] = namespace.options || {}
375+
end
376+
inherited_params = get_path_params(stackable_values.inherited_values)
377+
inherited_params.merge(params)
378+
end
379+
355380
def default_type(params)
356381
default_param_type = { required: true, type: 'Integer' }
357-
params.each { |param| param[-1] = param.last == '' ? default_param_type : param.last }
382+
params.each { |param| param[-1] = param.last.empty? ? default_param_type : param.last }
358383
end
359384

360385
def expose_params(value)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe '#847 route_param type is included in documentation' do
6+
let(:app) do
7+
Class.new(Grape::API) do
8+
resource :accounts do
9+
route_param :account_number, type: String do
10+
resource :records do
11+
route_param :id do
12+
get do
13+
{ message: 'hello world' }
14+
end
15+
end
16+
end
17+
end
18+
end
19+
20+
add_swagger_documentation
21+
end
22+
end
23+
let(:parameters) { subject['paths']['/accounts/{account_number}/records/{id}']['get']['parameters'] }
24+
25+
subject do
26+
get '/swagger_doc'
27+
JSON.parse(last_response.body)
28+
end
29+
30+
specify do
31+
account_number_param = parameters.find { |param| param['name'] == 'account_number' }
32+
expect(account_number_param['type']).to eq 'string'
33+
id_param = parameters.find { |param| param['name'] == 'id' }
34+
# Default is still integer
35+
expect(id_param['type']).to eq 'integer'
36+
end
37+
end

0 commit comments

Comments
 (0)