From 15019af91379e2096353b85f224beef7a93fab9c Mon Sep 17 00:00:00 2001 From: Braktar Date: Tue, 25 Aug 2020 14:51:06 +0200 Subject: [PATCH] Add nest array coercion spec --- .../grape/validations/validators/coerce_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/grape/validations/validators/coerce_spec.rb b/spec/grape/validations/validators/coerce_spec.rb index 41acb5f0de..b891963a0a 100644 --- a/spec/grape/validations/validators/coerce_spec.rb +++ b/spec/grape/validations/validators/coerce_spec.rb @@ -620,6 +620,23 @@ def self.parsed?(value) expect(JSON.parse(last_response.body)).to eq(%w[a b c d]) end + it 'parses parameters with Array[Array[String]] type and coerce_with' do + subject.params do + requires :values, type: Array[Array[String]], coerce_with: ->(val) { val.is_a?(String) ? [val.split(/\s+/).strip] : val } + end + subject.get '/coerce_nested_strings' do + params[:values] + end + + get '/coerce_nested_strings', values: 'a,b,c,d' + expect(last_response.status).to eq(200) + expect(JSON.parse(last_response.body)).to eq([%w[a b c d]]) + + get '/coerce_nested_strings', values: [%w[a], %w[b]] + expect(last_response.status).to eq(200) + expect(JSON.parse(last_response.body)).to eq([%w[a], %w[b]]) + end + it 'parses parameters with Array[Integer] type' do subject.params do requires :values, type: Array[Integer], coerce_with: ->(val) { val.split(/\s+/).map(&:to_i) }