Active_model_serializers: Serializer concerns not being loaded

Created on 28 Mar 2017  ยท  3Comments  ยท  Source: rails-api/active_model_serializers

Expected behavior vs actual behavior

I have an versioned Rails API application using concerns to bundle shared functionality between multiple serializers. I would expect namespaced serializer concerns to load properly, similar to how namespaced controller concerns work. However, when I namespace a serializer concern, it encounters an uninitialized constant exception. I believe the concern files aren't being loaded at all by by Active Model Serializers. Is there something I am missing to set this up properly?

Steps to reproduce

app/serializers/application_serializer.rb
class ApplicationSerializer < ActiveModel::Serializer
  # Shared functionality
end
app/serializers/v2/persona_serializer.rb
module V2
  class PersonaSerializer < ApplicationSerializer
    include V2::ClaimableSerializer

    # Other serializer attributes / methods
  end
end
app/serializers/v2/concerns/claimable_serializer.rb
module V2
  module ClaimableSerializer
    extend ActiveSupport::Concern

    included do
      # Shared attributes to be included with this concern
    end
  end
end

Environment

ActiveModelSerializers Version 0.10.5
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
Rails Version 5.0.2

Backtrace

Completed 500 Internal Server Error in 560ms (ActiveRecord: 46.3ms)

NameError (uninitialized constant V2::ClaimableSerializer):

app/serializers/v2/persona_serializer.rb:3:in `<class:PersonaSerializer>'
app/serializers/v2/persona_serializer.rb:2:in `<module:V2>'
app/serializers/v2/persona_serializer.rb:1:in `<top (required)>'
app/controllers/v2/auth/sessions_controller.rb:8:in `render_create_success'
app/docs/base_doc.rb:33:in `block in doc_for'
app/controllers/application_controller.rb:36:in `set_time_zone'

Most helpful comment

@bf4 Thanks, that led me to finding the solution.

Using include ::V2::ClaimableSerializer doesn't change anything since I don't have any other classes called ClaimableSerializer in my project. Both eager loading and autoloading the directory resulted in the namespace being lost (i.e. it would look for ClaimableSerializer instead of V2::ClaimableSerializer. What I ended up doing was moving my concerns to the top level and putting my v2 directory inside of it. All worked out of the box without any configuration this way:

Old Directory Structure
โ””โ”€โ”€โ”€ serializers
    โ””โ”€โ”€โ”€ v2
        โ”‚   persona_serializer.rb
        โ””โ”€โ”€โ”€ concerns
            โ”‚   claimable_serializer.rb
New Directory Structure
โ””โ”€โ”€โ”€ serializers
    โ””โ”€โ”€โ”€ concerns
    โ”‚   โ””โ”€โ”€โ”€ v2
    โ”‚       โ”‚   claimable_serializer.rb
    โ””โ”€โ”€โ”€ v2
        โ”‚   persona_serializer.rb

All 3 comments

Try include ::V2::ClaimableSerializer instead of include V2::ClaimableSerializer.

I'm going to close this because its a Ruby issue, possibly resolvable by modifying the autoload paths in Rails, and not an AMS issue. Please feel free to continue discussing though.

Per https://github.com/rails/rails/pull/18213 and https://github.com/rails/rails/pull/18239/files

you probably want to add

Rails.application.configure do
  config.paths.add "app/serializers/concerns", eager_load: true
end

@bf4 Thanks, that led me to finding the solution.

Using include ::V2::ClaimableSerializer doesn't change anything since I don't have any other classes called ClaimableSerializer in my project. Both eager loading and autoloading the directory resulted in the namespace being lost (i.e. it would look for ClaimableSerializer instead of V2::ClaimableSerializer. What I ended up doing was moving my concerns to the top level and putting my v2 directory inside of it. All worked out of the box without any configuration this way:

Old Directory Structure
โ””โ”€โ”€โ”€ serializers
    โ””โ”€โ”€โ”€ v2
        โ”‚   persona_serializer.rb
        โ””โ”€โ”€โ”€ concerns
            โ”‚   claimable_serializer.rb
New Directory Structure
โ””โ”€โ”€โ”€ serializers
    โ””โ”€โ”€โ”€ concerns
    โ”‚   โ””โ”€โ”€โ”€ v2
    โ”‚       โ”‚   claimable_serializer.rb
    โ””โ”€โ”€โ”€ v2
        โ”‚   persona_serializer.rb

Yay!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexCppns picture AlexCppns  ยท  5Comments

Looooong picture Looooong  ยท  5Comments

attenzione picture attenzione  ยท  4Comments

steverob picture steverob  ยท  4Comments

JohnMerlino2 picture JohnMerlino2  ยท  3Comments