This project is archived and is in readonly mode.

#4070 ✓committed
Sam

Sending emtpy params to a xml resource throws exception

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

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)

Comments and changes to this ticket

  • David Trasbo

    David Trasbo June 30th, 2010 @ 09:08 PM

    • 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.

  • Repository

    Repository July 8th, 2010 @ 10:45 PM

    • State changed from “new” to “committed”

    (from [786342e17f42799ef889cf6127fe97e9598272e0]) Return from ActiveRecord::Base#attributes= unless value is a hash [#4070 state:committed]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/786342e17f42799ef889cf6127fe97...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages