fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Error on defining an anonymous serializer class

Open daniel-illi opened this issue 7 years ago • 0 comments

We have a baseclass ApiSerializer with the shared behaviour of all subclasses. In our testsuite we use an anonymous subclass of ApiSerializer to test some behaviour. I.e. like this:

serializer_klass = Class.new(ApiSerializer) do
    has_one :some_thing
    has_many :many_things

    attribute :foo { 'bar' }
end

This gives me an error:

#<NoMethodError: undefined method `end_with?' for nil:NilClass>

["fast_jsonapi-1.5/lib/fast_jsonapi/object_serializer.rb:133:in `reflected_record_type'",
 "fast_jsonapi-1.5/lib/fast_jsonapi/object_serializer.rb:125:in `inherited'",
 "(irb):17:in `initialize'",
 "(irb):17:in `new'",
...
]

ObjectSerializer tries to determine the record type based on the class name, but does not take into account that the class can be unnamed: if self.name.end_with?('Serializer')

daniel-illi avatar Nov 22 '18 09:11 daniel-illi