toddmohney / json-api

Haskell Implementation of the JSON-API specification
http://hackage.haskell.org/package/json-api
MIT License
35 stars 14 forks source link

Allow control over whether data is returned as an array or not #27

Closed iand675 closed 7 years ago

iand675 commented 7 years ago

Hi, I'm experimenting a bit with ember-data, which incidentally inspired a lot of the design of the json-api spec. Whenever I return a 0-N query that happens to have one item, it is returned unwrapped (e.g. not in a JSON array). This causes ember-data to complain via assertion error that results from the query API should always be returned in an array.

I'd like to propose a few solutions:

  1. splitting mkDocument (non-breaking) into something like:
mkDocument = ... old behavior ...
mkDocumentSingle :: a -> Maybe Links -> Maybe Meta -> Document a
mkDocumentArray :: [a] -> Maybe Links -> Maybe Meta -> Document a
  1. Specify via ADT (breaking, but seems a bit cleaner)
data DataField a = Single a | Plural [a]

mkDocument :: DataField a -> Maybe Links -> Maybe Meta -> Document
yuliswe commented 7 years ago

I can confirm this needs to be improved. Specifically, the current mkDocument interface lacks of the ability to create an array that has 1 item. It looks like an easy fix. I'm going to fork this. I think 1 is better. I would be too lazy to type the 2nd implementation.

toddmohney commented 7 years ago

Ah, I see what you mean. These are good suggestions. I can make these changes soon. Alternatively, if you would like to submit a pull request, I'd be happy to review it so you can have the update sooner!

Thanks!