loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Support jsonapi.org

Open conrad-vanl opened this issue 12 years ago • 90 comments

Is there any way, or plans, to configure how JSON responses (and therefore requests) are serialized? For instance, it would be very nice to support a standard, such as http://jsonapi.org/ so that clients can take advantage of generalized tooling. Looking at the current API, it would appear to me the biggest thing would be for models to be serialized in root objects...

This:

{
   "user": {
      "id": "...",
      "name": "Conrad"
   }
}

As opposed to:

{
   "id": "...",
   "name": "Conrad"
}

There would also be other considerations in how associations and relations are serialized.

conrad-vanl avatar Aug 06 '14 16:08 conrad-vanl

+1

meng-zhang avatar Aug 06 '14 21:08 meng-zhang

+1 for this

imjoshholloway avatar Aug 07 '14 11:08 imjoshholloway

You can modify sharedMethods to have data returned like you showed above. In 2.x this is possible by modifying sharedMethod.returns.

var sharedMethod = MyModel.sharedClass.find('findById', true);
sharedMethod.returns[0].root = false;

Although I don't think this is a good long term solution. I think we should add the ability to define a global response format setting. What would be helpful is to know what other formats are useful.

ritch avatar Aug 08 '14 00:08 ritch

As far as supporting jsonapi, I'd like to see what else we need to change and if we can maintain computability with 2.x. Maybe a jsonapi flag that would modify all the right settings to support jsonapi.org conventions?

ritch avatar Aug 08 '14 00:08 ritch

+1

eabruzzese avatar Aug 17 '14 21:08 eabruzzese

+1 as well.

bemosior avatar Aug 17 '14 21:08 bemosior

+1, integration with ember apps would be cake walk if implemented.

alagunambi avatar Oct 25 '14 12:10 alagunambi

+1

kaizhu256 avatar Oct 28 '14 02:10 kaizhu256

+1, and happy Ember developers we would be!

bradplank avatar Nov 13 '14 03:11 bradplank

Does Ember include application/vnd.api+json in the accepts headers of the request?

strong-remoting can already serve different response depending on what the client accepts, it should be reasonably easy to add a new case for application/vnd.api+json - see strong-remoting/lib/http-context.js

The difficult part is how to determine the name of the root key (e.g. user for User.findById, users for User.find, but also category for Product.__get__category).

bajtos avatar Nov 14 '14 07:11 bajtos

By default, Ember uses the following in the request header: Accept:application/json, text/javascript, */*; q=0.01

However, the RESTAdapter can easily be extended to add application/vnd.api+json as an Accept header.

For the root key, wouldn't the model's name / plural name be used? I see the difficulty with the hasManyThrough relationship, as the API explorer shows the incorrect model. (I've been meaning to see if I could fix that issue.)

bradplank avatar Nov 17 '14 16:11 bradplank

For the root key, wouldn't the model's name / plural name be used? I see the difficulty with the hasManyThrough relationship, as the API explorer shows the incorrect model. (I've been meaning to see if I could fix that issue.)

I would make the initial implementation easier if we could use a constant key name. The jsonapi spec explicitly supports that:

The primary resource(s) SHOULD be keyed either by their resource type or the generic key "data".

Does Ember support this too?

bajtos avatar Nov 18 '14 13:11 bajtos

Unfortunately, it seems Ember does not treat the generic key 'data' as a special key. I reviewed the code, and tested just to make sure. The following error message is generated as there is no 'data' model defined:

Uncaught Error: Assertion Failed: Error: No model was found for 'datum'

[NOTE: I am currently using Ember version 1.7.0, however, I do not see any changes in 1.8.1 that would use the generic key 'data'.]

bradplank avatar Nov 18 '14 17:11 bradplank

@bradplank hmm... Seems like the Ember Inflector is trying to pluralize the word "data". Try this:

Ember.Inflector.inflector.uncountable('data');

sergiolepore avatar Feb 07 '15 22:02 sergiolepore

:+1:

seriousben avatar Mar 09 '15 18:03 seriousben

jsonapi will soon hit 1.0 and only backward compatible changes are planned after that. If Loopback started supporting it, it would make it very interesting to companies.

seriousben avatar Apr 17 '15 12:04 seriousben

+1

BenjaminHorn avatar May 08 '15 23:05 BenjaminHorn

Now that json api is 1.0, is there any further news on loopback support? And to note, there is strong movement ATM to make Ember Data natively handle json api.

Panman82 avatar Jun 26 '15 15:06 Panman82

+1

sahin avatar Jul 22 '15 08:07 sahin

IS there any update on this?

sahin avatar Jul 22 '15 17:07 sahin