Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Sending emtpy params to a xml resource throws exception

#4070

Hi,
I am trying to use ActiveResource to build a new app. I discoverd the following bug.
If I send an empty request to a resource from ActiveResource like

ChannelResource.create

but the Channel on the other side expects a name param I get the following trace. I send as xml as you can see in the trace:

Started POST "/channels.xml" for 127.0.0.1 at 2010-02-28 01:25:53
Processing by ChannelsController#create as XML Parameters: {"channel"=>"\n"}

NoMethodError (undefined method stringify_keys!' for "\n":String):<br/> app/controllers/channels_controller.rb:31:innew' app/controllers/channels_controller.rb:31:in create'

Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta/lib/action_dispatch/middleware/templates/rescues/trace.erb (1.1ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta/lib/action_dispatch/middleware/templates/rescues/
request_and_response.erb (3.5ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta/lib/action_dispatch/middleware/templates/rescues/layout.erb (18.0ms)

Reported by Sam · February 28th, 2010 @ 06:03 AM

State: committed
Milestone: none
Assigned to: José Valim José Valim
Importance: none

Activity

  1. David Trasbo
    David Trasbo
    • Assigned user set to José Valim

    I've done some research and this is what I've come up with.

    The root cause of this is really that the request body that Active Resource makes when you call create with no arguments looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <product>
    </product>
    

    When it reaches Rails, it'll use Hash.from_xml so it can convert that XML into something that fits in the params hash. Hash.from_xml will return this:

    {"product"=>"\n"}
    

    Technically, that's correct because what's in between those tags is really a newline. The result is that params[:product] returns "\n", which makes Active Record vomit here because strings don't respond to stringify_keys:

    def attributes=(new_attributes, guard_protected_attributes = true)
      return if new_attributes.nil?
      attributes = new_attributes.stringify_keys
    
      # ...
    end
    

    I've attached a patch that fixes this. I believe it's the appropriate place fix it: The XML generated by Active Resource is correct and the return value of Hash.from_xml is also really correct. The fix will make ActiveRecord::Base#attributes= let nothing but hashes pass through.

    June 30th, 2010 @ 09:08 PM

  2. Repository
    Repository
    • State changed from new to committed

    July 8th, 2010 @ 10:45 PM