Skip to content

Parse route_param type for nested endpoints #847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 1.4.4

#### Fixes

* [#840](https://github.com/ruby-grape/grape-swagger/pull/847): Fix documentation of `route_param` type when used with nested endpoints - [@dmoss18](https://github.com/dmoss18)

### 1.4.3 (January 5, 2022)

#### Fixes
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ ruby RUBY_VERSION

gemspec

gem 'grape', case version = ENV.fetch('GRAPE_VERSION', '>= 1.5.0')
gem 'grape', case version = ENV.fetch('GRAPE_VERSION', '~> 1.6')
when 'HEAD'
{ git: 'https://github.com/ruby-grape/grape' }
else
version
end

gem ENV['MODEL_PARSER'] if ENV.key?('MODEL_PARSER')
gem ENV.fetch('MODEL_PARSER', nil) if ENV.key?('MODEL_PARSER')

group :development, :test do
gem 'bundler'
Expand Down
28 changes: 27 additions & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,39 @@ def build_request_params(route, settings)
end

def merge_params(route)
path_params = get_path_params(route.app&.inheritable_setting&.namespace_stackable)
param_keys = route.params.keys

# Merge path params options into route params
route_params = route.params
route_params.each_key do |key|
path = path_params[key] || {}
params = route_params[key]
params = {} unless params.is_a? Hash
route_params[key] = path.merge(params)
end

route.params.delete_if { |key| key.is_a?(String) && param_keys.include?(key.to_sym) }.to_a
end

# Iterates over namespaces recursively
# to build a hash of path params with options, including type
def get_path_params(stackable_values)
params = {}
return param unless stackable_values
return params unless stackable_values.is_a? Grape::Util::StackableValues

stackable_values&.new_values&.dig(:namespace)&.each do |namespace|
space = namespace.space.to_s.gsub(':', '')
params[space] = namespace.options || {}
end
inherited_params = get_path_params(stackable_values.inherited_values)
inherited_params.merge(params)
end

def default_type(params)
default_param_type = { required: true, type: 'Integer' }
params.each { |param| param[-1] = param.last == '' ? default_param_type : param.last }
params.each { |param| param[-1] = param.last.empty? ? default_param_type : param.last }
end

def expose_params(value)
Expand Down
37 changes: 37 additions & 0 deletions spec/issues/847_route_param_options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'spec_helper'

describe '#847 route_param type is included in documentation' do
let(:app) do
Class.new(Grape::API) do
resource :accounts do
route_param :account_number, type: String do
resource :records do
route_param :id do
get do
{ message: 'hello world' }
end
end
end
end
end

add_swagger_documentation
end
end
let(:parameters) { subject['paths']['/accounts/{account_number}/records/{id}']['get']['parameters'] }

subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

specify do
account_number_param = parameters.find { |param| param['name'] == 'account_number' }
expect(account_number_param['type']).to eq 'string'
id_param = parameters.find { |param| param['name'] == 'id' }
# Default is still integer
expect(id_param['type']).to eq 'integer'
end
end