tiagopog/jsonapi-utils

Setting the type of a resource

derekcannon opened this issue · 3 comments

Maybe I'm not understanding how jsonapi-utils/jsonapi-resource works, but I have a OrderResource and a OrderListResource (which is a simplified version of OrderResource). I am trying to render OrderListResource, but have the type value be order. Currently, it's setting it to order_list. Here's the render statement:

jsonapi_render json: Order.last(2), options: { resource: Api::V2::OrderListResource }

OrderListResource

module Api
  module V2
    class OrderListResource < JSONAPI::Resource
      attribute :status_name
    end
  end
end

Response

{
  "data": [
    {
      "id": "1",
      "type": "order_lists",  # <-- wrong, should be "order"
      "links": {
        "self": "http://localhost:3001/api/v2/order_lists/1"  # <-- wrong, should be order/1
      },
      "attributes": {
        "status_name": "some_status"
      }
    },
    {
      "id": "2",
      "type": "order_lists",   # <-- wrong, should be "order"
      "links": {
        "self": "http://localhost:3001/api/v2/order_lists/2" # <-- wrong, should be order/2
      },
      "attributes": {
        "status_name": "some_other_status"
      }
    }
  ]
}

EDIT:

I'm not sure if this is the proper solution, or if I'm using jsonapi-resource incorrectly, but I fixed it by adding the following to OrderListResource:

      def self.name
        'Order'
      end

      def self._type
        'order'
      end

☝️ That's the answer I would bring up to you, as you mentioned it fixes the issue but I'm also not sure whether it's the proper way of doing so. Unfortunately JR's doc/issues are not clear around this case.

I sure hope this isn't what I think it is. Jeremy?
Jones need to speak with you.

🤔