-
Notifications
You must be signed in to change notification settings - Fork 324
Description
When moving a project from ActiveResource to her, my team and I came across a problem. The API we are using (which is being built by our team for an enterprise application) wraps the JSON response in a model name (e.g. GET /api/v1/accounts -> {"accounts":[<data>]}, or GET /api/v1/accounts/2 -> {"account":{<data>}}). This API, however, is not using ActiveModelSerializer to generate the responses (it uses JBuilder templates, FWIW). When creating the models to be backed by her, we simply had:
class Account
include Her::Model
parse_root_in_json true
end
Attempting to access that model threw an error, however: Undefined method '.keys' for #<Array>. In debugging this issue, I found that the problem lied in the extract_array method in the parse.rb file:
def extract_array(request_data)
if request_data[:data].is_a?(Hash) && (active_model_serializers_format? || json_api_format?)
request_data[:data][pluralized_parsed_root_element]
else
request_data[:data]
end
end
The issue is that our request_data wasn't passing the first if clause, since I didn't state that the format was one of those two options, although the response was a Hash. This method thus simply returned the hash response (i.e. accounts: {<data>}), instead of accessing the nested data.
There are obviously numerous simple fixes (remove the second half of the if check, move that second half into a new nested if check, if Hash, try to access request_data[:data][pluralized_parsed_root_element] and handle possible errors, etc), but I don't presume to understand precisely what your large scale project decisions are that led to this particular implementation, thus I haven't yet put in a Pull Request.
Hopefully, this is clear enough to display the issue that my team and I had. If not, let me know what else would prove valuable to you.
Love the project!
stephen