This project is archived and is in readonly mode.
Support multiple errors per attribute in ActiveModel Errors JSON serialization
Reported by Adam Meehan | September 11th, 2010 @ 11:13 AM | in 3.0.5
The current errors json serialization does not work for mutiple errors on a attribute. It returns duplicate keys for subsequent errors for that attribute. This of course can't work when using the JSON object.
So for example this ActiveModel errors hash
{"name" => ["can't be blank", "is too short"]}
Becomes
{"name":"can't be blank","name":"is too short"]}
The change required is to output an regular ordered hash from as_json call with the error messages as an array value. This will be serialized correctly into JSON
{"name":["can't be blank","is too short"]}
It also brings it into line with how ActiveModel returns messages for an attribute, which is now always as an array. We need to be consistent across the board.
The reason the ActiveModel::Errors subclass does not get serialized correctly is due to the each method override. It doubles up the keys for attributes with multiple errors when iterated over. I tried just calling super in the as_json method, but this doesn't work because the Hash as_json method calls map on self. Thereby causing the same issue again.
Comments and changes to this ticket
-
Adam Meehan September 11th, 2010 @ 11:14 AM
- Tag changed from activemodel to json encoding as_json, activemodel, errors, patch, serialization
-
Lake September 16th, 2010 @ 12:22 AM
+1 applies cleanly to master and the tests pass.
I'm running ruby 1.9.2.
-
Aditya Sanghi November 27th, 2010 @ 06:42 PM
- State changed from new to open
- Milestone cleared.
- Assigned user set to Santiago Pastorino
- Tag changed from json encoding as_json, activemodel, errors, patch, serialization to json encoding as_json, activemodel, errors, patch, serialization, verified
- Importance changed from to Medium
Patch applies! tests look fine and pass.
-
Thilo Utke November 28th, 2010 @ 01:43 PM
Hi I suggest a slightly different solution that fixes to_hash to return the ordered hash instead of self. So you get the expected behavior when calling to_hash and as_json can rely on to_hash. See patch for details.
-
Repository December 1st, 2010 @ 10:46 AM
- State changed from open to resolved
(from [7148b933c4865a5140187d7ed792fd6df9b860a4]) ActiveModel::Errors.to_hash returns plain OrderedHash and used in to_json serialization to properly handle multiple errors per attribute [#5615 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
https://github.com/rails/rails/commit/7148b933c4865a5140187d7ed792f...
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>
People watching this ticket
Attachments
Referenced by
- 5616 ActiveResource error parsing support for new JSON errors serialization format It also supports what I hope will be the final format fro...
- 6014 deserialize error messages directly from error hash instead of an array of messages One thing that needs fixing before applying this is #5615...
- 5965 Rails 2.3 AR JSON validation errors incorrect format Duplicate of https://rails.lighthouseapp.com/projects/89...
- 5615 Support multiple errors per attribute in ActiveModel Errors JSON serialization (from [7148b933c4865a5140187d7ed792fd6df9b860a4]) ActiveM...