Parse route_param type for nested endpoints (#847)
* Fix documentation of `route_param` type when used with nested endpoints * Rubocop fixes * Rubocop fixes * grape 1.6 Co-authored-by: peter scholz <pscholz.le@gmail.com>
This commit is contained in:
parent
d5766639d2
commit
908a7a8912
|
|
@ -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)
|
### 1.4.3 (January 5, 2022)
|
||||||
|
|
||||||
#### Fixes
|
#### Fixes
|
||||||
|
|
|
||||||
4
Gemfile
4
Gemfile
|
|
@ -6,14 +6,14 @@ ruby RUBY_VERSION
|
||||||
|
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
gem 'grape', case version = ENV.fetch('GRAPE_VERSION', '>= 1.5.0')
|
gem 'grape', case version = ENV.fetch('GRAPE_VERSION', '~> 1.6')
|
||||||
when 'HEAD'
|
when 'HEAD'
|
||||||
{ git: 'https://github.com/ruby-grape/grape' }
|
{ git: 'https://github.com/ruby-grape/grape' }
|
||||||
else
|
else
|
||||||
version
|
version
|
||||||
end
|
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
|
group :development, :test do
|
||||||
gem 'bundler'
|
gem 'bundler'
|
||||||
|
|
|
||||||
|
|
@ -348,13 +348,39 @@ module Grape
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge_params(route)
|
def merge_params(route)
|
||||||
|
path_params = get_path_params(route.app&.inheritable_setting&.namespace_stackable)
|
||||||
param_keys = route.params.keys
|
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
|
route.params.delete_if { |key| key.is_a?(String) && param_keys.include?(key.to_sym) }.to_a
|
||||||
end
|
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)
|
def default_type(params)
|
||||||
default_param_type = { required: true, type: 'Integer' }
|
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
|
end
|
||||||
|
|
||||||
def expose_params(value)
|
def expose_params(value)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue