How to handle include_root_in_json API with multiple types?
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?
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 https://github.com/remiprev/her/issues/379.
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.