ActiveModel::Errors#to_json generates wrong json string when field has multiple errors #123

Closed
wants to merge 1 commit into
from

Projects

None yet

2 participants

@krekoten
Contributor

No description provided.

@josevalim
Member

Why the definition currently in as_json is not enough? What is the difference in the output?

@krekoten
Contributor

{"name":"can not be blank","name":"can not be nil"} is generated, but it should be
{"name":["can not be blank","can not be nil"]}. Currently when you'll try to create object from generated string you'll lose all messages for field except last.

@josevalim
Member

Hrm, maybe we should change encode_json in ActiveSupport::OrderedHash then? Because if AM::Errors exhibit this weird behavior, I would expect the same to be happening to AS::OrderedHash (as the former inherits from the latter).

@krekoten
Contributor

This issue exists because of overridden #each. To fix this I used #each_pair in #encode_json instead of #map which used in ActiveSupport::OrderedHash#encode_json. So logic in ActiveModel::Errors#encode_json is specific to it.

@josevalim
Member

Sweet! One last question, as we are overriding encode_json, do we still need to implement as_json or it can be removed?

@krekoten
Contributor

We need it.

@bswinnerton bswinnerton added a commit to bswinnerton/launchbar-github that referenced this pull request Feb 18, 2017
@bswinnerton bswinnerton Add support for rails/rails#123 formatted strings d20ec1a
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment