cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 532 forks source link

Single Table Inheritance (STI) doesn't work with nested JSONAPI::Resource #660

Closed kcalmes closed 8 years ago

kcalmes commented 8 years ago

When I have the following setup

routes.rb

namespace :namespace do
    jsonapi_resources :animals
    jsonapi_resources :farms do
        jsonapi_resources :animals
    end
end

resources/namespace/animal_resource.rb

class Namespace::AnimalResource < JSONAPI::Resource
    immutable
end

resources/namespace/cat_resource.rb

class Namespace::CatResource < Namespace::AnimalResource

end

When making a request to /namespace/animal-resources/5 the type is cat as it should be according to https://github.com/cerebris/jsonapi-resources#immutable-heterogeneous-collections

however, when making a request to /namespace/farms/1/animal-resources/5 the type is animal, but it should be cat

This seems to be a bug, any thoughts?

digitalcora commented 8 years ago

I'm not a maintainer, but as per this comment:

Nested resources are not yet a supported feature.

I think the fact that you're "allowed" to create such route structures anyway is an accidental side effect of using blocks to customize which relationship/related-resource routes are generated.

As a point of advice, it usually makes sense to have routes be as shallow as possible (so e.g. /farms/1/animals is valid, but individual animals are accessed at /animals/5). While the JSONAPI spec doesn't technically prescribe any route structure, this is the structure used in their examples, and is also the only structure supported out of the box by many client libraries. JSONAPI::Resources itself also assumes your routes are arranged this way when it generates links (i.e. the generated link for an animal will be /animals/5 even if you have no such route).