Skip to content

Commit

Permalink
Merge pull request #162 from trans0082/feature/coerce_form_params_rec…
Browse files Browse the repository at this point in the history
…usrsive

Change to coerce form params recursively
  • Loading branch information
ota42y authored Dec 14, 2018
2 parents edc084b + 5f28c75 commit b3ce72a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/committee/middleware/request_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def handle(request)
allow_query_params: @allow_query_params,
coerce_form_params: @coerce_form_params,
optimistic_json: @optimistic_json,
coerce_recursive: @coerce_recursive,
schema: link ? link.schema : nil
).call

Expand Down
3 changes: 2 additions & 1 deletion lib/committee/request_unpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(request, options={})
@allow_query_params = options[:allow_query_params]
@coerce_form_params = options[:coerce_form_params]
@optimistic_json = options[:optimistic_json]
@coerce_recursive = options[:coerce_recursive]
@schema = options[:schema]
end

Expand All @@ -31,7 +32,7 @@ def call
p = @request.POST

if @coerce_form_params && @schema
Committee::StringParamsCoercer.new(p, @schema).call!
Committee::StringParamsCoercer.new(p, @schema, coerce_recursive: @coerce_recursive).call!
end

p
Expand Down
24 changes: 24 additions & 0 deletions test/request_unpacker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@
end
end

it "coerces form params with coerce_form_params, coerce_recursive and a schema" do
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
schema = JsonSchema::Schema.new
schema.properties = { "x" => JsonSchema::Schema.new }
schema.properties["x"].type = ["object"]
schema.properties["x"].properties = { "y" => JsonSchema::Schema.new }
schema.properties["x"].properties["y"].type = ["integer"]

env = {
"CONTENT_TYPE" => content_type,
"rack.input" => StringIO.new("x[y]=1"),
}
request = Rack::Request.new(env)
params, _ = Committee::RequestUnpacker.new(
request,
allow_form_params: true,
coerce_form_params: true,
coerce_recursive: true,
schema: schema
).call
assert_equal({ "x" => { "y" => 1 } }, params)
end
end

it "unpacks form & query params with allow_form_params and allow_query_params" do
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
env = {
Expand Down

0 comments on commit b3ce72a

Please sign in to comment.