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.

format patches to support serializing data out in the correct format with correct http request headers per http method type

#450

Currently all data is serialized out in XML for PUT and POST requests even when you choose JSON as your exchange format. The simple fix was to remove all references when serializing data with the to_xml method and replace them to use a method which passes through the selected format's encode method (ex. ActiveResource::Formats::JsonFormat.encode). Please see the ActiveResource::Base.encode method in the patch.

Secondly, the proper format based http request headers were not being set. Constructive requests (POST/PUT) should be using the 'Content-Type' header to describe the format of their payloads, while DELETE and GET requests should be using the 'Accept' header to indicate what format of data they will accept from the reqeust in question. So you would have something like:

Content-Type:application/xml (for POST/PUT)

Accept:application/xml (for GET/DELETE)

Thirdly, this also helped me uncover that the CustomMethods "post" instance method was only serializing data to_xml when the body provided to that method was explicitly nil. This would never happen because the default "body" argument to that method was an empty String, hence data was never serialized to_xml.

Lastly, the http_mock.rb did not take into account request payloads, which are quite important to testing. I modified the mock to support a request_body parameter as well as the ability to override the format based request header for testing the various formats. It is backwards compatible with the current tests.

All the tests run.

Reported by rbpandey · June 19th, 2008 @ 05:30 PM

State: duplicate
Milestone: none
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. rhg_mbrooks
    rhg_mbrooks

    +1. A very much needed change. Good work Rus.

    June 19th, 2008 @ 05:37 PM

  2. Doug Ramsay
    Doug Ramsay

    +1.

    The changes to the request headers follow RESTful principles. And calling .nil? on the default empty string body argument was apparently an oversight.

    June 19th, 2008 @ 05:43 PM

  3. jeff
    jeff

    +1 excellent - i could really use this in my current project!

    June 19th, 2008 @ 06:28 PM

  4. rbpandey
    rbpandey

    The latest patch file includes an update to support parsing errors from both xml and json per the validations.rb class.

    June 19th, 2008 @ 07:49 PM

  5. Ryan Daigle
  6. Rick
    Rick
    • State changed from new to open
    • Tag set to activeresource, bug, patch
    • Milestone set to 2.1.1
    • Assigned user set to Rick

    Shouldn't #to_json in #encode take an options hash too? Also, is #encode really the right name for it? #serialize maybe?

    July 4th, 2008 @ 07:53 AM

  7. rbpandey
    rbpandey

    Rick, good catch, I have attached another patch file which includes the to_json(options) call.

    As for the naming, I'll leave that for the community to decide. I prefer "serialize" myself as that is how I had named my original implementation, but after seeing DHH's implementation I didn't want to start renaming things.

    July 7th, 2008 @ 04:32 PM

  8. Tarmo Tänav
    Tarmo Tänav

    Added a patch that fixes the whitespace errors added by formats_patch.diff

    July 16th, 2008 @ 12:55 AM

  9. rbpandey
    rbpandey

    TT, I don't think you patched the latest patch. Thanks anyway though, I'll make the correction and submit a new patch. Thx, -RBP

    July 17th, 2008 @ 04:27 PM

  10. rbpandey
    rbpandey

    i think TT patched the patch for whitespace issues. so we don't really need another patch.

    August 12th, 2008 @ 07:39 PM

  11. Jeremy Kemper
    Jeremy Kemper
    • Assigned user changed from Rick to Jeremy Kemper

    Could you rebase against master?

    August 30th, 2008 @ 02:11 AM

  12. Repository
    Repository
    • State changed from open to resolved

    (from [caabe228bc6c6e920043334d717e72093559e118]) Format related patches to support serializing data out in the correct format with correct http request headers per http method type [#450 format patches to support serializing data out in the correct format with cor... state:resolved]

    Signed-off-by: Tarmo Tänav tarmo@itech.ee Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...

    August 30th, 2008 @ 02:47 AM

  13. Wesley Moxam
    Wesley Moxam
    • Tag changed from activeresource, bug, patch to activeresource, bug, patch

    While this is probably a good idea overall, the patch stank.

    • breaks the API in a minor point release
    • documentation wasn't updated, so those browsing the docs are all "WTF, why doesn't this work?"

    Yet it got 3 (+1's) within an hour of being posted. That's hardly enough time to apply the patch, test it and review the code.

    The +1 system is broken. And API changes have to be taken a little more seriously. I thought there was a process in place for it (deprecate, then change in the next release).

    September 17th, 2008 @ 10:42 PM

  14. Tarmo Tänav
    Tarmo Tänav
    • State changed from resolved to open
    • Milestone changed from 2.1.1 to 2.1.2

    Thank you for noticing this. Do you intend to prepare a patch adressing it?

    September 17th, 2008 @ 11:47 PM

  15. Doug Ramsay
    Doug Ramsay

    Wesley,

    How about some details? How does the patch break the API? Perhaps you could provide a test that shows how it breaks the API.

    The original code had flaws in serializing and setting the headers, so I think a correction needed to be made in one form or another. Do you have a suggestion about how the solution could be better implemented? If so, I would be very interested in checking it out.

    Finally, the three people who posted +1's within an hour of it being posted all had used the fixes in the patch for several weeks prior to it being posted here.

    September 17th, 2008 @ 11:47 PM

  16. Tarmo Tänav
    Tarmo Tänav

    Hi Doug,

    The API is broken by the removal of to_xml (being replaced with encode), it's in fact the first change in the patch, funny how of the many reviewers here noone noticed it :)

    As for the +1's, to me it doesn't seem fair to have people working in the same team pushing eachothers patches without stating their affiliation, especially due to the fact that being coworkers likely means they all tested it in similiar environments.

    September 18th, 2008 @ 12:39 AM

  17. Doug Ramsay
    Doug Ramsay

    Fair enough, perhaps we should have indicated our affiliation. We obviously had no issues with the change while working in a similar environment.

    However, if patches are accepted purely on the basis of +1's, then Wesley is right - the system is seriously broken.

    The fact that a change that broke the API was missed by tests tells me I need to write more tests for ActiveResource. I'll work on that.

    September 18th, 2008 @ 02:14 AM

  18. Tarmo Tänav
    Tarmo Tänav

    This patch actually seems to have triggered both #1011 Re-add to_xml to ActiveResource and #1053 Removed http header Accept by default, it would be great if someone who worked on this patch could help solve/review those tickets.

    September 18th, 2008 @ 04:00 AM

  19. Michael Koziarski
    Michael Koziarski
    • Milestone cleared.

    I'm going to revert this change from 2-1-stable, the api breaking and change of behaviour with Accept headers isn't worth it.

    But these issues also need to be addressed in the 2.2 lifetime.

    September 18th, 2008 @ 09:54 AM

  20. Michael Koziarski
    Michael Koziarski

    The breakage introduced here was accidental and could and should have been caught during the review process. But shit happens, let's fix it and move forward :) We're all on the same team here.

    Clearly this shows there's some test coverage sorely missing in active resource though....

    September 18th, 2008 @ 09:59 AM

  21. Wesley Moxam
  22. rbpandey
    rbpandey

    I just submitted a fix for 1011. Please have a look. It looks like 1053 was already fixed in the master branch with the patch provided in the issue report.

    September 23rd, 2008 @ 08:55 PM

  23. Pratik
    Pratik
    • State changed from open to duplicate
    • Tag changed from activeresource, bug, patch to activeresource, bug, patch

    October 17th, 2008 @ 05:22 PM