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

How to handle include_root_in_json API with multiple types? #379

Open
Benabik opened this issue Oct 13, 2015 · 2 comments
Open

How to handle include_root_in_json API with multiple types? #379

Benabik opened this issue Oct 13, 2015 · 2 comments

Comments

@Benabik
Copy link

Benabik commented Oct 13, 2015

I have an API that works like the following (names changed to protect the innocent):

GET /fruits/1.json => {"apple": {"id": 1}}
GET /fruits/2.json => {"orange": {"id": 2}}

This is from a Rails app that has uses ActiveRecord::Inheritance to store multiple related types in a single table and include_root_in_json to return the types in the API.

The only way I see to handle this in Her (haven't implemented it) is to implement custom middleware that parses the JSON, stores the type in metadata (which doesn't seem very well documented), and reverses the process when doing requests.

Is there a better way to do this in Her that I'm not seeing?

@hubert
Copy link
Contributor

hubert commented Oct 25, 2015

Hi Brian,

Thanks for writing in. I'm not sure I've run across an API that was set up
that way.

What would the response look like for GET /fruits since the root key seems
to be derived from the individual type rather than their common superclass?

On Tue, Oct 13, 2015 at 2:03 PM, Brian Gernhardt [email protected]
wrote:

I have an API that works like the following (names changed to protect the
innocent):

GET /fruits/1.json => {"apple": {"id": 1}}
GET /fruits/2.json => {"orange": {"id": 2}}

This is from a Rails app that has uses ActiveRecord::Inheritance
http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html to
store multiple related types in a single table and include_root_in_json
to return the types in the API.

The only way I see to handle this in Her (haven't implemented it) is to
implement custom middleware that parses the JSON, stores the type in
metadata (which doesn't seem very well documented), and reverses the
process when doing requests.

Is there a better way to do this in Her that I'm not seeing?


Reply to this email directly or view it on GitHub
#379.

@Benabik
Copy link
Author

Benabik commented Oct 25, 2015

include_root_in_json affects the serialization of each object, so the controller does something akin to:

def index
   render json: Fruit.all
end

And the result is: [{"apple": {"id": 1}}, {"orange": {"id": 2}}]

I'm trying to simplify significantly from an internal API. I can try to make a simple Rails app that demonstrates this come Monday.

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

No branches or pull requests

2 participants