Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
uhrohraggy opened this issue May 18, 2017 · 0 comments

Comments

@uhrohraggy
Copy link

uhrohraggy commented May 18, 2017

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 uhrohraggy changed the title Serializing a different type of object while specifying the serializer fails with includes Serializing a different type of object while specifying the serializer fails to find relationships with includes May 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant