This is a heads-up for those using Ember Data. As of 1.0.0-beta.7, it doesn’t seem to conform to the following prescription of JSON API for creation of records:
“Its root key MUST be the same as the root key provided in the server’s response to GET request for the collection.” (jsonapi.org)
Also, Ember Data expects:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "post": { | |
| "id": 1, | |
| "title": "Rails is omakase", | |
| "comments": [ | |
| "1", | |
| "2" | |
| ], | |
| "user": "dhh" | |
| }, | |
| "comments": [ | |
| { | |
| "id": "1", | |
| "body": "Rails is unagi" | |
| }, | |
| { | |
| "id": "2", | |
| "body": "Omakase O_o" | |
| } | |
| ] | |
| } |
JSON API prescribes:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "posts": [{ | |
| "id": 1, | |
| "title": "Rails is omakase", | |
| "links": { | |
| "comments": [ | |
| "1", | |
| "2" | |
| ] | |
| }, | |
| "user": "dhh" | |
| }], | |
| "linked": { | |
| "comments": [ | |
| { | |
| "id": "1", | |
| "body": "Rails is unagi" | |
| }, | |
| { | |
| "id": "2", | |
| "body": "Omakase O_o" | |
| } | |
| ] | |
| } | |
| } |
Update: The documentation is out of sync with the code, evidently. There seems to be support for “links” but not “linked.”
I can see how Ember’s pattern is more terse and avoids nesting, both good qualities, but there is no clear indicator that post.comments[] is related to root.comments[]. The developer would have to know of the relationship in advance.
Whereas JSON-API’s standard introduces a nested object, but makes explicit the relationship between everything in post.links and root.linked. A developer need not know which keys have relationships in advance; they need only to relate everything under ‘links’ with ‘linked’.
Of the two examples provided, which do you prefer? In other words: would you like to see Ember Data conform to the JSON-API standard or vice versa?