Underscore incoming params before filtering during Deserialization #1986
+1
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
TL;DR Ensure general use case of using snake case for permitted params i.e.
jsonapi_parse(params, only: [:first_name])
even when deserializing dasherized or camel cased params.Long version: In Rails-land, we generally default to snake case when describing variables, symbols, etc. Right now, if we have incoming params with kebab or camel cased fields, we have to match the case when deserializing in the controller.
E.g.
If we send dasherized parameters such as
to a UserController containing this (fairly standard) method
the result will be empty
user_params
as both 'first-name' and 'last-name' are filtered out by theonly
option before the params are ever parsed.The current (ugly and unsatisfying) workaround is to list out the kebabed or camel cased parameters as strings (i.e.
only: ['first-name', 'last-name']
or to convince whoever is building a front end for your API to patch their Ember adapter or whatever to match whatever you're using (snake case FTW).I'd much rather be able to use snake case for permitted params consistently across all of my Rails apps and not worry about remembering how one particular front end is configured to send params over (and not worry about having to change the casing of all of my permitted params in all of my controllers in the event the status quo changes!).
Changes
Call existing underscore function on incoming parameter field keys when filtering against options passed into the
parse
methodCaveats
Might be overkill.
Related GitHub issues
Possible made obsolete by #1927 if there's ever any movement on it.
Additional helpful information
Here's the blog post that let me know that I and the developers I talked to about this weren't alone: http://kyleshevlin.com/all-the-steps-needed-to-get-active-model-serializers-to-work-with-jsonapiadapter-and-jsonapiserializer-in-ember/