Open ellmo opened 1 month ago
It looks like ActionController::Parameters is not encoding as a json object. I'm not really set up to test this right now so would you be able to help?
The first thing to try would be to see if there is an #as_json or #to_json method on ActionController::Parameters. If so that does it return?
prying into the controller (using 3.11.8
)
params
#=> #<ActionController::Parameters {"url"=>"(...)", "users"=>[#<ActionController::Parameters {"email"=>"test@test.com", "first_name"=>"joe", "last_name"=>"test"} permitted: false>], "uuid"=>"something-something", "format"=>"json", "controller"=>"api/v1/users", "action"=>"create"} permitted: false>
users = params[:users]
#=> [#<ActionController::Parameters {"email"=>"test@test.com", "first_name"=>"joe", "last_name"=>"test"} permitted: false>]
users.as_json
#=> [{"email"=>"test@test.com", "first_name"=>"joe", "last_name"=>"test"}]
users.to_json
#=> "[{\"email\":\"test@test.com\",\"first_name\":\"joe\",\"last_name\":\"test\"}]"
and yet, after sending users
to a sidekiq worker:
users
#=> ["{\"email\"=>\"test@test.com\", \"first_name\"=>\"joe\", \"last_name\"=>\"test\"}"]
Is this what you needed?
What I'm trying to determine is if the ActionController::Parameters.as_json exists and is being called. It kind of looks like #to_json is being called by Oj instead of #as_json.
Hey, circling back to this – any way I can help?
What does the json gem emit when calling JSON.generate
without Oj?
It appears as if the #to_json
method is being called on the params instead of #as_json
. It's as if the :use_as_json
option is not set which would be the normal case for the JSON gem.
Problem
Hello there
Seems like I have an issue with
oj
(after updating to3.16.4
) not properly being used in Rails when it comes toActionController::Parameters
.Since the
users
params is passed further to a Sidekiq worker, Sidekiq usesJSON.generate(object)
to serialize parameters.JSON.generate
is one of three different ways of doing this incorrectly:None of those approaches returns a properly serialized object. If you try to parse it back as JSON, it will return:
So now, instead of an array of hashes, we end up with an array of strings. And those strings - mind you - cannot be further parsed:
The only module/method combination that works is:
Question
So here's my question: Is there an option for Oj (
3.16.4
) and Rails6.1+
that I'm missing? This was working as expected up tooj 3.10.18
and it breaks with anything over that.My current config for Oj is this: