This project is archived and is in readonly mode.
2.3.2 ActiveResource JSON doesn't send parameters with root node
-
Matthew Moore
Here's the applicable code within ActiveResource::Base, which makes it pretty obvious what's going on:
# Returns the serialized string representation of the resource in the configured # serialization format specified in ActiveResource::Base.format. The options # applicable depend on the configured encoding format. def encode(options={}) case self.class.format when ActiveResource::Formats[:xml] self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) else self.class.format.encode(attributes, options) end end -
Matthew Moore
- Assigned user set to Rick
-
Joe Martinez
You can fix this by adding
ActiveRecord::Base.include_root_in_json = truein a initializer. Hope this will be default in Rails3
-
Joe Martinez
Nevermind the above - it's for ActiveRECORD. This problem does exist in ActiveResource.
-
Joe Martinez
Patched it.
Added an ActiveResource::Base option to include_root_in_json just like ActiveRecord.
-
samvincent
Looked over Joe's patch. Looks clean.
-
Matthew Parker
thanks for this patch joe. i'm working with a big team for migrating a high-traffic website from cold fusion to ruby on rails, and for the first phase, we'll be doing a rails front end / cf backend, with RESTful communication between the two over JSON. Just today i was playing around with ActiveResource over :json and ran into this issue.
any idea on when this might make it into a rails release?
-
Joe Martinez
No problem, I wrote this patch for a client and it's part of a production application. We haven't had any problems with it so far, but I'm sure the Rails 3 guys have lots in mind for the ActiveResource stuff which is why this has probably been looked over.
I actually have this patch in the form of a rails initializer/monkey patch as well. If you need that just let me know.
-
Matthew Parker
Thanks Joe, would you send over your monkey patch? either post it here or email me at moonmaster9000 at gee mail
We also had an issue where rails 2.3.3, by default, returns json with a root node (caused by setting ActiveRecord::Base.include_root_in_json = true in config/initializers/new_rails_defaults.rb). ActiveResource over :json (unlike ActiveResource over :xml) expects a json response without a root node, and doesn't have any logic to handle the situation where the ActiveResource attributes are encapsulated in the root node in the json response.
It's, of course, not the biggest deal (we just set ActiveRecord::Base.include_root_in_json = false in the app sending json representations of ActiveRecord objects), but it is a bit odd now that we have one app sending ActiveRecord responses as json without a root node, and the other app sending ActiveResource :json CREATE/PUT messages with a root node.
-
Jarred Nicholls
I've edited the json_format.rb file to detect and load the json response appropriately when the model name is the root of the response object. Pretty simple solution, but I hope an "elegant & universal" solution makes it to the core so I don't have to remember to keep patching my ActiveResource installations everywhere :-)
-
Elad Meidar
+1 verified on 2-3-stable, but patch failed to apply, here's a patch for 2-3-stable.
-1 on existing patch, fails on 2-3-stable and on master, on master it fails with:
error: activeresource/test/base_test.rb: No such file or directoryThis is something i have been patching myself for a while too, definitely a requirement as far as i concern
-
Mike (at coverallcrew)
Anyone know if this patch will be applied to Rails 3?
-
Santiago Pastorino
Patch for master here
-
Repository
- State changed from new to committed
(from [72f89b5d971b48a133c4c0af56fbeda35d738dae]) create option to include_root_in_json for ActiveResource [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:committed] http://github.com/rails/rails/commit/72f89b5d971b48a133c4c0af56fbed...
-
Santiago Pastorino
- State changed from committed to open
This patch still need to be applied on 2-3-stable
-
Santiago Pastorino
- Tag changed from activeresource, bug to activeresource, bug, patch
- Assigned user changed from Rick to Jeremy Kemper
-
Repository
- State changed from open to committed
(from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert "create option to include_root_in_json for ActiveResource [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:committed]"
This reverts commits 72f89b5d971b48a133c4c0af56fbeda35d738dae, 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972.
Should reuse Active Model.
[#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:incomplete] http://github.com/rails/rails/commit/2a6e0f34ad6b48dcf41989e0d7555c...
-
Repository
- State changed from committed to incomplete
(from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert "create option to include_root_in_json for ActiveResource [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:committed]"
This reverts commits 72f89b5d971b48a133c4c0af56fbeda35d738dae, 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972.
Should reuse Active Model.
[#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:incomplete] http://github.com/rails/rails/commit/2a6e0f34ad6b48dcf41989e0d7555c...
-
Repository
- State changed from incomplete to committed
(from [aa401bd75a055a0bb12094128b01c8ce5fc2abe8]) Add ActiveResource::Base.include_root_in_json, like Active Record, to serialize instances as hash of model name -> attributes hash rather than the bare attributes hash. [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:committed]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/aa401bd75a055a0bb12094128b01c8... -
Repository
(from [7cd1d37a51f5f53f8fc1360f886d26cabf12d969]) Reuse Active Model serialization in Active Resource. [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/7cd1d37a51f5f53f8fc1360f886d26...
