Skip to content

Setting the type of a resource #60

Closed
@derekcannon

Description

@derekcannon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions