fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

set_type on a per-record basis?

Open allanberry opened this issue 6 years ago • 15 comments

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 avatar Mar 17 '18 15:03 allanberry

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.

shishirmk avatar Mar 18 '18 00:03 shishirmk

Thank you for the confirmation; I suspected as much. Much obliged.

Looking forward to 2.0! :)

allanberry avatar Mar 18 '18 18:03 allanberry

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 avatar Apr 17 '18 19:04 jshow

@jshow polymorphic types are already supported in dev branch https://github.com/Netflix/fast_jsonapi/tree/dev#customizable-options

shishirmk avatar Apr 22 '18 01:04 shishirmk

@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

shishirmk avatar May 21 '18 00:05 shishirmk

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?

denikus avatar Nov 22 '18 16:11 denikus

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!

allanberry avatar Nov 28 '18 12:11 allanberry

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?

deniciocode avatar Dec 13 '18 13:12 deniciocode

@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 }

thisismydesign avatar Mar 04 '19 12:03 thisismydesign

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

thisismydesign avatar Mar 21 '19 13:03 thisismydesign

This feature could be very useful! Is it planned for the next release?

phlegx avatar Jun 26 '19 10:06 phlegx

It would be helpful!

inska avatar Jul 09 '19 02:07 inska

Can't wait to see this feature in action! It would be really helpfull :)

saralonngren avatar Aug 13 '19 13:08 saralonngren

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?

deniciocode avatar Dec 19 '19 10:12 deniciocode

Please handle this feature, it would be extremely useful for manually setting type.

maxKimoby avatar Apr 22 '20 19:04 maxKimoby