Skip to content

Breaking change for nested params in declared in Grape 1.3.3 #2101

Closed
@tlconnor

Description

@tlconnor

A PR was merged back in April and included in Grape 1.3.3 that has changed the way declared works.
#2043

Before this PR was merged, the declared helper would guarantee that the entire params structure would be recreated, including deeply nested parameters. Any leaf params not provided would have nil values.

With this merged PR, any missing Hash params would be evaluated as {}, even if they has deeper nested params defined.

Here is an example from endpoint_spec.rb. The params are defined as:

subject.params do
  requires :first
  optional :second
  optional :third, default: 'third-default'
  optional :nested, type: Hash do
    optional :fourth
    optional :fifth
    optional :nested_two, type: Hash do
      optional :sixth
      optional :nested_three, type: Hash do
        optional :seventh
      end
    end
    optional :nested_arr, type: Array do
      optional :eighth
    end
  end
  optional :arr, type: Array do
    optional :nineth
  end
end

Given a test that calls get '/declared?first=present', the result of declared(params) would be:

In Grape 1.3.2:

{
  "first": "present",
  "second": null,
  "third": "third-default",
  "nested": {
    "fourth": null,
    "fifth": null,
    "nested_two": {
      "sixth": null,
      "nested_three": {
        "seventh": null
      }
    }
  },
  "nested_arr": []
}

In Grape 1.3.3:

{
  "first": "present",
  "second": null,
  "third": "third-default",
  "nested": {},
  "arr": []
}

It is not clear which of these is the correct behavior. The merged PR #2043 would suggest that the latter is correct, however it is actually very useful to have the previous behavior from 1.3.2.

The README isn't clear about what the behavior should be for deeply nested params.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions