Skip to content

StrongParameters#permit has inconsistent behavior with numeric-looking keys #41625

@nightpool

Description

@nightpool

Steps to reproduce

params = ActionController::Parameters.new(items: {'1': {x: 2, y: 3}, '2': {x: 3, y: 4}})

params.require(:items).permit('1': [:x, :y], '2': [:x])
#  {"1"=>{"x"=>2, "y"=>3}, "2"=>{"x"=>3}}

params.permit(items: {'1': [:x, :y], '2': [:x]})
#  {"items"=>{"1"=>{}, "2"=>{}}}

Expected behavior

Permit should behave the same when called on a parent hash as it does when called on a child hash.

Actual behavior

Permit's behavior is inconsistent, and it's very difficult and confusing to permit a nested hash with integer-like keys. More generally, when dealing with JSON parameters, the implicit "array-like" handling behavior should be limited to real, actual arrays—not just objects with keys that look like numbers.

System configuration

Rails version: 6.0.2.2

Ruby version: 2.6.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions