1

I've been really digging FastJsonAPI. Works great.

Is it possible to set the set_type serializer definition on a per-object basis?

i.e. I'm using Rails STI (single table inheritance). I have a mixed set of base objects and derivative objects, and I'd like to have different types for each.

This is a fake example of JSON output I would like:

{
  "data": [
    {
      "attributes": {
        "title": "Generic Vehicle"
      },
      "id": "1",
      "type": "vehicle"
    },
    {
      "attributes": {
        "title": "Fast Car"
      },
      "id": "2",
      "type": "car"
    },
    {
      "attributes": {
        "title": "Slow Car"
      },
      "id": "3",
      "type": "car"
    },
    {
      "attributes": {
        "title": "Motorcycle"
      },
      "id": "4",
      "type": "motorcycle"
    }
  ]
}

I do have an object type attribute I can use, of course, since I'm using STI. But I don't want to use it as an attribute: I want to use it as the outside type, as in the JSON above.

serializer(s):

class VehicleSerializer
  include FastJsonapi::ObjectSerializer
  set_type :vehicle  # can I tie this to individual objects, right here?
  attributes :title
end

class CarSerializer < VehicleSerializer
  set_type :car
  attributes :title
end

class MotorcycleSerializer < VehicleSerializer
  set_type :motorcycle
  attributes :title
end

class TruckSerializer < VehicleSerializer
  set_type :truck
  attributes :title
end

You see, I have some controllers which only pull from a single object type, and for them, the single CarSerializer or whatever, works great. The trouble is when I use a controller like this, that aggregates multiple vehicle types in the index method:

require_relative '../serializers/serializers.rb'

class MultiVehiclesController < ApplicationController

  def index
    @vehicles = Vehicle.where(type: ["Car", "Motorcycle"])

    # perhaps there's a way to modify the following line to use a different serializer for each item in the rendered query?
    render json: VehicleSerializer.new(@vehicles).serializable_hash
  end

  def show
    @vehicle = Vehicle.find(params[:id])

    # I suppose here as well:
    render json: VehicleSerializer.new(@vehicle).serializable_hash
  end

end
allanberry
  • 7,325
  • 6
  • 42
  • 71

2 Answers2

1

This was not possible in version 1 of FastJsonAPI, which I was using for this. It is now apparently possible as of version 1.2 (although I have not yet tested it).

allanberry
  • 7,325
  • 6
  • 42
  • 71
  • Could you provide an example? As far as I can tell there's still no support for first-level polymorphism, only for relationships. – thisismydesign Mar 04 '19 at 12:12
  • You might be right. I've not looked into this in a while, and I've not been using Rails in a while. If you find out a solution, please ping this thread, OK? Thanks. – allanberry Mar 04 '19 at 22:29
0

I also have STI and want to use proper type when I render a collection of different classes. I ended up with a custom serializer to override hash_for_collection. Inside that method it is possible to lookup for a particular collection item serializer and call its record_hash

That is a bit slower than fast_jsonapi / jsonapi-serializer implementation but now each item in a data collection has proper type

class MixedCollectionSerializer < ApplicationSerializer
  include SerializerResolverHelper

  def hash_for_collection
    serializable_hash = {}
    data = []
    included = []
    fieldset = @fieldsets[self.class.record_type.to_sym]
    @resource.each do |record|
      record_klazz = jsonapi_serializer_class_resolver(record,false)
      data << record_klazz.record_hash(record, fieldset, @includes, @params)
      included.concat record_klazz.get_included_records(record, @includes, @known_included_objects, @fieldsets, @params) if @includes.present?
    end
    serializable_hash[:data] = data
    serializable_hash[:included] = included if @includes.present?
    serializable_hash[:meta] = @meta if @meta.present?
    serializable_hash[:links] = @links if @links.present?
    serializable_hash  
  end
end
module SerializerResolverHelper

    def jsonapi_serializer_class_resolver(resource, is_collection)
      if resource.respond_to?(:first)
# if it is a collection and it contailns different types
        first_item_class = resource.first.class
        return MixedCollectionSerializer if resource.any? { |item| item.class != first_item_class }
      end

      JSONAPI::Rails.serializer_class(resource, is_collection)
    rescue NameError
       # if use STI it is necessary to resolve serializers manually
resource = resource.first if is_collection && resource.respond_to?(:first)
      resource_name = case
                      when resource.is_a?(Vehicle) then resource.type.to_s
                      # another STI parent classes ...
                      end

      "#{resource_name}Serializer".constantize if resource_name.present?
    end
end
user1827334
  • 43
  • 1
  • 5