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.

[PATCH] json validations errors for ActiveResource

#1956

ActiveResource can interpret validations errors from xml body, but not from json and even worse : he tries to interpret json output as xml and parse it.

This patch adds json support for validations errors based on the work of Rick Olson. You can render errors in json like this : render :json => @person.errors.to_json, :status => 422

Reported by Fabien Jakimowicz · February 13th, 2009 @ 12:18 AM

State: committed
Milestone: 2.3.6
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. Fabien Jakimowicz
    Fabien Jakimowicz

    Patch updated for current rails version.

    March 30th, 2009 @ 01:00 AM

  2. Fabien Jakimowicz
    Fabien Jakimowicz
    • Tag changed from activeresource, json, patch, validations to activeresource, active_resource, json, patch, validations

    March 30th, 2009 @ 01:04 AM

  3. Fabien Jakimowicz
    Fabien Jakimowicz
    • Tag changed from activeresource, active_resource, json, patch, validations to activeresource, active_resource, json, patch, validation, validations

    March 30th, 2009 @ 01:04 AM

  4. Fabien Jakimowicz
    Fabien Jakimowicz
    • Tag changed from activeresource, active_resource, json, patch, validation, validations to 2-3-stable, activeresource, active_resource, json, patch, validation, validations

    updated patch for 2-3-stable branch.

    April 18th, 2009 @ 12:01 AM

  5. Samsonov Ivan
    Samsonov Ivan

    Our company faces the same problem. Why is this patch not accepted?

    July 8th, 2009 @ 04:20 PM

  6. Fabien Jakimowicz
    Fabien Jakimowicz

    I tried to contact rails-core mailing list, but nobody answers. I also try to contact directly a member of the rails-core team who told me the patch seems good and should be applied within 2 weeks ... but that was 2 months ago and he does not answer me anymore.

    I checked with both 2.3 and 3.0 branches and it still applies cleanly.

    Maybe if you can move things on the mailing list, you can have this patch applied.

    July 27th, 2009 @ 12:04 PM

  7. Fabien Jakimowicz
    Fabien Jakimowicz
    • Tag changed from 2-3-stable, activeresource, active_resource, json, patch, validation, validations to 2-3-stable, 2.3.x, 2.x, 3.0, activeresource, active_resource, json, patch, validation, validations

    July 27th, 2009 @ 12:13 PM

  8. Rizwan Reza
    Rizwan Reza
    • Tag changed from 2-3-stable, 2.3.x, 2.x, 3.0, activeresource, active_resource, json, patch, validation, validations to 2-3-stable, 2.3.x, 2.x, 3.0, activeresource, active_resource, bugmash, json, patch, validation, validations

    verified

    +1 This applies to 2-3-stable cleanly but not master.

    August 9th, 2009 @ 12:28 AM

  9. Rizwan Reza
  10. Elad Meidar
    Elad Meidar

    Patch applies and tests pass on 2-3-stable

    Patch does not apply on master.

    August 9th, 2009 @ 04:03 AM

  11. Fabien Jakimowicz
  12. Fabien Jakimowicz
    Fabien Jakimowicz
    • Title changed from json validations errors for ActiveResource to [PATCH] json validations errors for ActiveResource

    August 9th, 2009 @ 04:02 PM

  13. Elad Meidar
    Elad Meidar

    +1 Verified, +1 on 2-3-stable patch => applies and all tests pass, +1 on master patch => applies and all tests pass

    August 9th, 2009 @ 06:07 PM

  14. Simon Jefford
    Simon Jefford

    +1 verified on both stable and master.

    August 9th, 2009 @ 07:25 PM

  15. David Trasbo
    David Trasbo

    -1

    Both patches don't apply and needs to be updated respectively.

    August 9th, 2009 @ 07:53 PM

  16. Fabien Jakimowicz
    Fabien Jakimowicz

    david trasbo: this is weird, I just test it and was able to apply it to both branches. How does it fail ?

    August 9th, 2009 @ 08:00 PM

  17. Nathan Humbert
    Nathan Humbert

    +1 on 2-3-stable patch => applies and all tests pass

    August 9th, 2009 @ 08:28 PM

  18. Josh Nichols
    Josh Nichols

    +1, I like the cut of this patch, for it brings consistency between xml and json behavior with regards to validation errors.

    Verified to apply and pass tests on master and 2-3-stable.

    August 10th, 2009 @ 04:52 AM

  19. Repository
  20. Repository
  21. Jeremy Kemper
    Jeremy Kemper
    • Tag changed from 2-3-stable, 2.3.x, 2.x, 3.0, activeresource, active_resource, bugmash, json, patch, validation, validations to 2-3-stable, 2.3.x, 2.x, 3.0, activeresource, active_resource, json, patch, validation, validations
    • Milestone changed from 2.x to 2.3.4

    August 10th, 2009 @ 06:42 AM

  22. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6
    • State changed from committed to open
    • Assigned user set to Jeremy Kemper

    A change to ActiveResource::Validations was introduced in 2.3.4 which adds support for JSON errors:

    This is looking for an exact match on Content-Type 'application/xml'. If, for example, the returned Content-Type is 'application/xml; charset=utf-8' then the error response is ignored.

    September 17th, 2009 @ 07:23 PM

  23. Repository
  24. Repository
  25. Gabe da Silveira
    Gabe da Silveira

    This patch introduced a regression my app's tests because our activeresource mocks did not set a content-type header. This was easy enough to fix once I debugged it, but it was pretty nasty to track down since the result is simply that the error messages are ignored and save returns true and the object appears valid with no trace of what happened without debugging deep into ActiveResource. Even though in production this case ought to never happen, in tests it's the default with no indication that you would need to set a Content-Type header.

    The bottom line in my opinion is that any case where a 422 response is returned, the object should end up as invalid.

    Although I'm not sure that defaulting to XML is "correct" or complete, I think we should do it to maintain backwards compatibility.

    At the very least the ActiveResource::HttpMock documentation should show an example of stubbing an error.

    What does everyone think?

    October 2nd, 2009 @ 07:55 PM

  26. Jeremy Kemper
    Jeremy Kemper
    • State changed from committed to open

    Agreed, Gabe.

    October 6th, 2009 @ 08:42 PM

  27. James Brennan
  28. Jatinder Singh
    Jatinder Singh

    To avoid the problem faced by Gabe, the solution is to verify the format of ARes rather than Content-Type header for the remote errors.

    I've attached patches for master and 2-3-stable.

    October 27th, 2009 @ 07:09 AM

  29. Christian Seiler
  30. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to incomplete

    Jatinder - tests?

    January 27th, 2010 @ 04:16 AM

  31. Jatinder Singh
  32. Repository
    Repository
    • State changed from incomplete to committed

    (from [158e7b63ab83dbcb9cdc7f951be920faf16fe3c0]) Use format of ARes rather than content-type of remote errors to load errors.

    [#1956 [PATCH] json validations errors for ActiveResource state:committed]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
    http://github.com/rails/rails/commit/158e7b63ab83dbcb9cdc7f951be920...

    January 28th, 2010 @ 02:28 AM

  33. Repository
  34. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 8th, 2010 @ 08:22 AM