JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Dealing with strong parameters with nested hash in JR

Open
#1,029 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

Not sure how to deal with this strong parameters issue in a JSONAPI::ResourceController.

I have a simple User model with settings field that is stored in Postgres as a jsonb field type. The resource looks like this:

class API::V1::UserResource < JSONAPI::Resource
  key_type :uuid

  attributes :username, :email, :first_name, :last_name, :roles
  attribute :settings

  def roles
    @model.roles.to_a
  end
end

This serializes out just fine, but trying to PATCH with something like the body below:

{
  "data": {
    "id": "776c526a-6b5f-4839-998e-591d3043645d",
    "type": "users",
    "attributes": {
      "settings": {
        "color": "blue"
      }
    }
  }
}

If config.raise_if_parameters_not_allowed = false then I get this stored in the database:

"--- !ruby/object:ActionController::Parameters\nparameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n  color: blue\npermitted: false\n"

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at JSONAPI::ResourceController's strong-parameter handling and reproduce the PATCH request for the User resource's jsonb settings field. The fix is complete when nested settings are stored as JSON data rather than a serialized ActionController::Parameters object.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, rails, ruby
Domain
api, backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.