fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Automatic lookup within Rails controllers

Open dillonwelch opened this issue 8 years ago • 6 comments

If I do something like render json: model, when I'm using AMS it automatically detects ModelSerializer but when I'm switching over to fast_jsonapi it doesn't seem to have that same magic. Am I messing something up or is that not a feature of this gem?

P.S. Overall love the gem and the philosophy, thanks for open sourcing this.

dillonwelch avatar Apr 19 '18 00:04 dillonwelch

Yes this is not automatic yet. However #89 and other pull requests have made it a bit less verbose.

This should work

render json: WidgetSerializer.new(widget)

shishirmk avatar Apr 22 '18 01:04 shishirmk

I have a PR to add this ready but I'm a bit concerned about causing conflicts with AMS (I imagine that there are projects using both libraries simultaneously)

WDYT about using:

render jsonapi: model

To avoid conflicts with json renderer overrided by AMS?

guilleiguaran avatar May 15 '18 00:05 guilleiguaran

That doesn't seem necessary, as I feel like it should use the model name to load the appropriate serializer and that serializer would define whether it's using AMS or fast_jsonapi via what it's inheriting from/what it includes.

One thought - are you're saying that people have a model being serialized by both gems in different controllers/actions?

dillonwelch avatar May 15 '18 00:05 dillonwelch

@oniofchaos AMS right now is overriding ActionController::Renderers internal private methods making a bit hard your proposal.

To use :json and keep the compatibility with AMS we will need to detect if AMS is in the project and override again the methods that it's overriding to make them aware of fast_jsonapi serializers and have another code path to override the default JSON renderer if AMS is not present in the project. It can be done but it'll be "hacky" in my opinion.

One thought - are you're saying that people have a model being serialized by both gems in different controllers/actions?

I'm talking about people using AMS for some models and fast_jsonapi for other models, I think that is the case for some people looking to improving the performance in some serializers without having to migrate a whole existing project to fast_jsonapi.

guilleiguaran avatar May 15 '18 00:05 guilleiguaran

Just want to add a thumbs up to this.

Yes this is not automatic yet. However #89 and other pull requests have made it a bit less verbose.

This should work

render json: WidgetSerializer.new(widget)

Yes, it works, how about serializing an Array ?

It would be great if fast_json could detect models inside an array and serialize properly & automagically.

EDIT: Answering my question, if you pass an array it will serialize automatically. What got me was that you have to explicitly cast the AR relation to array.

   render json: WidgetSerializer.new(Widget.all.to_a)

c0ze avatar Aug 22 '18 12:08 c0ze

I started a little project to help integrate fast_jsonapi with rails/.

To be honest, I'm not sure how much of the JSONAPI spec fast_jsonapi want's to include. While working on the renderer side it's kinda clear that it's not just the serializer lookup support that needs to be provided, it's also the error handling/rendering support and the error serializers (#102) that should available.

Consider this, as an example implementation: https://github.com/stas/jsonapi.rb/blob/master/lib/jsonapi/rails.rb#L28-L100

stas avatar Jan 14 '19 16:01 stas