diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 6f4becfea..e03ebed1d 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -91,10 +91,9 @@ def handle_passed_param(params_nested_path, has_passed_children = false, &_block route_options_params = options[:route_options][:params] || {} type = route_options_params.dig(key, :type) has_children = route_options_params.keys.any? { |k| k != key && k.start_with?(key) } - if type == 'Hash' && !has_children {} - elsif type == 'Array' || type&.start_with?('[') + elsif type == 'Array' || type && type.start_with?('[') && !type.include?(',') [] elsif type == 'Set' || type&.start_with?('#<Set') Set.new diff --git a/spec/grape/validations/params_scope_spec.rb b/spec/grape/validations/params_scope_spec.rb index 9ef14af9f..6ab21c258 100644 --- a/spec/grape/validations/params_scope_spec.rb +++ b/spec/grape/validations/params_scope_spec.rb @@ -91,6 +91,43 @@ def app end end + context 'when using multiple types' do + it 'coerces the parameter via the type\'s parse method' do + subject.params do + requires :bar, type: Array do + optional :foo, types: [String, Float, Integer], coerce_with: ->(c) { + if c + if /([0-9]+):([0-9]+):([0-9]+)/ =~ c.to_s + 'HH:MM:SS format' + elsif /\A[0-9]+\.{0,1}[0-9]*\z/ =~ c.to_s + 'Float or Integer format' + else + 'Invalid Time value' + end + end + } + end + end + subject.post('/types') { declared(params)[:bar].first[:foo] } + + post '/types', bar: [{ foo: '00:00:01' }] + expect(last_response.status).to eq(201) + expect(last_response.body).to eq('HH:MM:SS format') + + post '/types', bar: [{ foo: 1 }] + expect(last_response.status).to eq(201) + expect(last_response.body).to eq('Float or Integer format') + + post '/types', bar: [{ foo: 1.0 }] + expect(last_response.status).to eq(201) + expect(last_response.body).to eq('Float or Integer format') + + post '/types', bar: [{ foo: nil }] + expect(last_response.status).to eq(201) + expect(last_response.body).to eq('') + end + end + context 'when using custom types' do module ParamsScopeSpec class CustomType