-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding JSONAPI::Utils to controller causes PATCH operations to malfunction #44
Comments
Thanks for reporting, @delner. As you pointed, since it works with a duplicated |
Okay. From what I understand, e.g. Something akin to... irb(main):001:0> h = { c: 1 }
=> {:c=>1}
irb(main):002:0> a = { h: h }
=> {:h=>{:c=>1}}
irb(main):003:0> b = a.dup
=> {:h=>{:c=>1}}
irb(main):004:0> h.delete(:c)
=> 1
irb(main):005:0> h
=> {}
irb(main):006:0> a
=> {:h=>{}}
irb(main):007:0> b
=> {:h=>{}} Let me know what you find, or if I can be of any help! |
Hey, @delner! Sorry for the delay, I had a pretty busy friday. Well, taking a quick look now I just realized that the sample controller code is missing its actions so that JR applies its default update operation. That's why the request is being processed twice. JU kind of enforces developers not to define operations within resource classes (JR's default) but in controllers rather. Maybe for default actions – like the one from your example – we should consider a workaround, but I need to think a bit more since it's contrary to one of the gem's main goals: keep operations explicit in controllers. To fix the sample code try this: class SampleModelsController < JsonApiController
# PATCH /sample_models/:id
def update
sample_model = SampleModel.find(params[:id])
if sample_model.update(resource_params)
jsonapi_render json: sample_model
else
jsonapi_render_errors json: sample_model, status: :unprocessable_entity
end
end
end |
Well, instead of enforcing developers to write their operations in controllers we may just recommend it on |
There's also a similar open issue here. So I think it's really relevant to bring up some "fix" now. |
Hmmm, I'm not really a fan of the having to write my own I think the best way to fix it would to be to have the |
Yeah, @delner. I had a thought on it yesterday and, yes, it's a better idea not to enforce developers to write their own definition of default actions. This way the gem will also keep as transparent as possible avoiding any additional learning curve in order to make things work. Yesterday, while working on #45, I considered three ways to fix that buggy behavior with 1. Make a deep copy of
2. Override
3. Make a PR to JR applying a memoization where the
For now, I started implementing the option 2 (#45) in order to pack a minor release with the fix. In parallel, I will work on a PR suggesting the memoization of |
Fixed in #45 |
Affects jsonapi-utils
0.5.0.beta4
, jsonapi-resources0.8.1
and Rails5.0.0.1
.Given a model
SampleModel
:And a resource
SampleModelResource
:With controllers:
And routes:
And a controller spec that performs a patch operation:
Running PATCH raises a
400 Bad Request
, which manifests as:I dug deeper, and what I found was that
JSONAPI::Utils
adds a Rails callback that creates aJSONAPI::RequestParser
prior to the request being processed:This ends up running some code in jsonapi-resources which parses the data parameters for the PATCH operation. In the process of parsing this data, it performs a
delete
on the data hash.Now the
JSONAPI::Utils
callback finishes without issue. However, whenJSONAPI::RequestParser
is invoked again as Rails runs the actual action, thedata
hash inparams
no long hasid
, which is required. It raises aMissingKey
error, and the request fails.In summary...
It seems like calling
JSONAPI::RequestHelper
twice with the same parameters is not safe. DespiteJSONAPI::Util
's effort toparams.dup
prior to invoking it, theJSONAPI::RequestHelper
code still modifies the underlyingdata
hash in a pretty bad way.Frankly, this seems to be more of an issue with
jsonapi-resources
thanjsonapi-utils
. But I thought I'd bring it to your attention, because as it stands, it means one cannot compose your package into a JSON API controller.Are there any feasible workarounds? Or should an issue be opened with
jsonapi-resources
?The text was updated successfully, but these errors were encountered: