fast_jsonapi
fast_jsonapi copied to clipboard
set_type on a per-record basis?
Thanks for good software; FJA works well for me so far.
I'd like to use set_type
on a per record basis when aggregating diverse record types in a single resource stream. I delineated the issue on this StackOverflow post, and then (belatedly) decided to read the FJA source. I don't see a way to do it, hence the new feature request.
I'm not exactly sure how it would look; I'm a bit of a ruby newbie. I expect calling a callback on set_type()
might make sense:
class VehicleSerializer
include FastJsonapi::ObjectSerializer
set_type @records.each do |record|
return record.type
end
attributes :title
end
I'm not sure @records
exists at this stage, but hopefully you get the idea.
If you can point me in the right direction, I'm happy to get my hands dirty with a pull request, but I might need a little hand holding. I haven't done it before. Thanks.
For this version we only support serializing homogeneous arrays. We have optimized for that case as that is a common use case where we needed performance gains. We might have to re-architect some parts of the gem if we want to support this.
Thank you for the confirmation; I suspected as much. Much obliged.
Looking forward to 2.0! :)
We require such a general functionality to support polymorphic types
how does this look ?
set_type do |movie_media|
movie_media.type ? movie_media.type : 'unknown'
end
@jshow polymorphic types are already supported in dev branch https://github.com/Netflix/fast_jsonapi/tree/dev#customizable-options
@allanberry We have added better support for inheritence in fast_jsonapi v1.2 Please try using it and see if it works for your STI usecase
Thanks for good software; FJA works well for me so far.
I'd like to use
set_type
on a per record basis when aggregating diverse record types in a single resource stream. I delineated the issue on this StackOverflow post, and then (belatedly) decided to read the FJA source. I don't see a way to do it, hence the new feature request.I'm not exactly sure how it would look; I'm a bit of a ruby newbie. I expect calling a callback on
set_type()
might make sense:class VehicleSerializer include FastJsonapi::ObjectSerializer set_type @records.each do |record| return record.type end attributes :title end
I'm not sure
@records
exists at this stage, but hopefully you get the idea.If you can point me in the right direction, I'm happy to get my hands dirty with a pull request, but I might need a little hand holding. I haven't done it before. Thanks.
@allanberry Did you manage to use it like you expected?
Hi there, thanks for this feature, and sorry about my slow response.
The project for which I would have used this feature has unfortunately fizzled. I don't currently have a use-case available where I can test.
I have updated my StackOverflow post at least, so people can find it. Thanks!
Hey all,
I see a need for a dynamic type. We are building an application which have some reservations
and appointments
. Both records have the same interface.
Now I have an endpoint which serves blocking events for a specific foreign key. Blocking events can be both, a reservation or an appointment.
I would like to set the type reservations
for reservations and the type appointments
for appointments in my json
without a separate serializer. Is this currently possible?
@shishirmk Unless I'm missing something polymorphism is not supported on the first level, only on associations. Would be great to have an option on set_type
similar to set_id
:
set_type { |record| record.class }
What I ended up doing is fixing the type manually, which is a big hack and has its performance implication as well. It's an interactor pattern but you hopefully get the idea.
class FixFastJsonapiPolymorphicTypes
include Interactor
def call
entities = context.entities
fixed_serializable_data = nil
if context.serializable_hash[:data]
fixed_serializable_data = context.serializable_hash[:data].each_with_index.map do |item, index|
item[:type] = entities[index].class.to_s.underscore
item
end
end
context.fixed_serializable_hash = context.serializable_hash.merge(data: fixed_serializable_data)
end
end
This feature could be very useful! Is it planned for the next release?
It would be helpful!
Can't wait to see this feature in action! It would be really helpfull :)
Is there a reason this issue is closed? Do we have now a way of using set_type
based on the record type?
Having a single table inheritance should use only one serializer and sets the type based on the record type. How do I accomplish this?
Please handle this feature, it would be extremely useful for manually setting type.