joshdholtz / jsonapi-ios

A library for loading data from a JSON API datasource.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing top level data field during serialization?

v-ken opened this issue · comments

Hi, Thank for this library. Just not sure if I'm serializing correctly as I'm missing the top level data field as required by JSON API spec 1.0.

let dict = JSONAPIResourceParser.dictionaryFor(object)

@v-ken Thanks for using the library 😊 Are you able to provide me with a sample object of what you are trying to parse? Easier to help with examples 😉

@joshdholtz Thanks for the quick response.

Looking at the current test case, the serialized dictionary has type and attributes at the top level but according to the spec,

A document MUST contain at least one of the following top-level members:

  • data: the document’s “primary data”
  • errors: an array of error objects
  • meta: a meta object that contains non-standard meta-information.

So, it should be

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

instead of,

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

Not a huge issue as we can just wrap the dictionary in another data level. Just wondering if this was intended?

@v-ken I personally did not write the serializer part of this library (somebody else kindly wrote it for me) so I'm not actually sure if it was intended or not 😇 but I think the intention of this was to only output the serialized object itself (leaving out the data wrapper). The dictionaryForObject method only outputs the dictionary for the object and not the whole request.

We probably could/should add a helper to add that data for you though. I'm resisting to add the data into this call because that would be a pretty nice breaking change but we could create a new call with another parameter added on that adds the data around that object. It would look something like

let dict = JSONAPIResourceParser.dictionaryFor(object, wrapInData: true)

Thoughts?

@joshdholtz Looks good!

Not urgent though, as manually wrapping it ourselves is really not that much of an issue. Just thought it might have been unintentional rather than intended.