Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom json body #289

Closed
wants to merge 8 commits into from
Closed

Allow custom json body #289

wants to merge 8 commits into from

Conversation

Morxander
Copy link

Some APIs doesn't follow the known standards and they're returning the json array or the json object inside a key for example :

{
    "status": 200,
    "data": [
      {
          "id": 1,
          "active": true
      },
      {
          "id": 2,
          "active": false
      }
    ]
}

And now ActiveResource doesn't understand these APIs so I added an optional attribute called response_array_key you can set its value inside the ActiveResource model to solve this issue.
So now if you added self.response_array_key = 'data' to your ActiveResource model it will understand your API and if you left it empty nothing will happen.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @jeremy (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@@ -144,6 +150,14 @@ def handle_response(response)
when 301, 302, 303, 307
raise(Redirection.new(response))
when 200...400
unless @response_array_key.blank?
begin
new_response_body = ActiveSupport::JSON.decode(response.body)[@response_array_key]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like in the wrong place. Why do we need to handle JSON here? Should not the Format take care of it like it do actually?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca I moved it to the JSON Format class, please check it.

@lancecarlson
Copy link

This is interesting. I actually just started to investigate this when trying to get a jsonapi compatible api working with active resource. Does this work on singleton resources in addition to collections? It doesn't look like it from the commit. Maybe it should also include that functionality? In that case, may want to consider renaming the configurable option to response_key? Also, not sure if response_key is the most descriptive, but I don't have any better suggestions yet. :)

@Morxander
Copy link
Author

@rafaelfranca can you please check and let me know If you have any other comments? :)

@rails-bot rails-bot bot added the stale label Aug 4, 2018
@rails-bot
Copy link

rails-bot bot commented Aug 4, 2018

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.

If it is an issue and you can still reproduce this error on the master branch,
please reply with all of the information you have about it in order to keep the issue open.

If it is a pull request and you are still interested on having it merged please make sure it can be merged clearly.

Thank you for all your contributions.

@rails-bot rails-bot bot closed this Aug 11, 2018
@rafaelfranca
Copy link
Member

Thank you for the pull request and sorry for the delay. Active Resource is a very opinionated client and I feel like we can't support all formats of json response that exists. By adding this option we may give the wrong impression that it should be possible to configure Active Resource to consume any json API but this is not the case. That said I prefer to not support this.

@rails-bot rails-bot bot removed the stale label Aug 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants