jbuilder-json_api icon indicating copy to clipboard operation
jbuilder-json_api copied to clipboard

Single resource object not supported?

Open georgecheng opened this issue 9 years ago • 2 comments

It appears that resources can only be returned as arrays using the json.api_format! method, even for a single resource object, is this the intended behavior?


When I'm returning a single object, such as: json.api_format! @account

For "data", I get something like:

{
  "data": [
    {
      "type": "account", 
      "id": "1", 
      "links": {
        "self": "/accounts/1"
      }
    }
  ]
}

The example given at http://jsonapi.org/format/ for a single resource object is

{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      // ... this article's attributes
    },
    "relationships": {
      // ... this article's relationships
    }
  }
}

Which doesn't return "data" as an array for a single resource object. While the spec states A logical collection of resources MUST be represented as an array, even if it only contains one item or is empty. , it doesn't seem that applies for requests that target single resources.

Primary data MUST be either:

  • a single resource object, a single resource identifier object, or null, for requests that target single resources
  • an array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections

georgecheng avatar Mar 22 '16 20:03 georgecheng

@georgecheng I'm so happy someone is interested in my gem!

I assume it's quite easy to implement such a functionality, gonna fix it this weekend :palm_tree:

vladfaust avatar Mar 22 '16 21:03 vladfaust

I haven't fixed it yet :+1:

vladfaust avatar Oct 05 '16 00:10 vladfaust