jsonapi-serializers icon indicating copy to clipboard operation
jsonapi-serializers copied to clipboard

Serializing a different type of object while specifying the serializer fails to find relationships with includes

Open uhrohraggy opened this issue 8 years ago • 0 comments

Raising as an issue in case I don't understand why it's implemented this way, but If you specify the serializer similar to:

JSONAPI::Serializer.serialize(model, namespace: Api::V1, serializer: UserStorySerializer, include: ['foo'], context: context) and model is a User (not a UserStory), find_serializer_class_name will return Api::V1::UserSerializer, which fails when JSONAPI::Serializer.find_serializer(root_object, options) is called and later used via serializer.has_many_relationships.has_key?(unformatted_attr_name)

From: /Users/myuser/.rvm/gems/ruby-2.3.3@rails5/gems/jsonapi-serializers-1.0.0/lib/jsonapi-serializers/serializer.rb @ line 237 JSONAPI::Serializer.find_serializer_class_name:

    236: def self.find_serializer_class_name(object, options)
 => 237:   if options[:namespace]
    238:     return "#{options[:namespace]}::#{object.class.name}Serializer"
    239:   end
    240:   if object.respond_to?(:jsonapi_serializer_class_name)
    241:     return object.jsonapi_serializer_class_name.to_s
    242:   end
    243:   "#{object.class.name}Serializer"
    244: end
[2] pry(JSONAPI::Serializer)> options
=> {:context=>{:date=>nil},
 :serializer=>Api::V1::UserStorySerializer,
 :namespace=>Api::V1,
 :include=>["foo"],
 :fields=>{},
 :base_url=>nil,
 :include_linkages=>["foo"]}
[3] pry(JSONAPI::Serializer)> object.class
=> User(id: integer, title: string,

I think it should use the options[:serializer] directly if specified in the options

uhrohraggy avatar May 18 '17 10:05 uhrohraggy