Active_model_serializers: Active Model Serializer Config Adapter setting not working for RC 5

Created on 9 Apr 2016  路  9Comments  路  Source: rails-api/active_model_serializers

ActiveModel::Serializer.config.adapter = :json is not working on rc5, but it works fine on rc2. Also tried ActiveModelSerializers.config.adapter = :json to no avail

Most helpful comment

I will take a deeper look into the new issue in coming days. thanks @Mifrill for the details

All 9 comments

Guess it's changed to: ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
?

@sollabeck

Keep in mind that the JsonApi (:json_api) and Json (:json) adapters are different.

See https://github.com/rails-api/active_model_serializers/blob/master/docs/general/adapters.md#adapters

not working in rc5 doesn't provide much to go on. If you're still having trouble would you please add more detail as asked for by default when creating a new issue.

the same issue, besides the link is broken now
the config does not fluent to Serializer for some reason:

#config/initializers/ams.rb
ActiveModel::Serializer.config.adapter = :json_api

but the root key is not included in JSON, as same as for :json key

gem 'active_model_serializers', '~> 0.10.0'

can you please set the configuration as

ActiveModelSerializers.config.adapter = :json_api

and see if things get improved

@wasifhossain unfortunately, nope I've tried this, anyway thanks)

@Mifrill if you don't bother to update the gem in the Gemfile as

gem 'active_model_serializers', :git => 'https://github.com/rails-api/active_model_serializers.git', :branch => '0-10-stable'

and revisit your issues, hope that should fix those cases

Nah... looks really strange

rails -v
Rails 4.2.10

even with:

ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = true
end

as_json returns without root.

here is correct implementation:
https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/howto/upgrade_from_0_8_to_0_10.md

      def as_json(options = {})
        if root
          {
            root => super
          }
        else
          super
        end
      end

but it seems like they missed in current 0.10.x gem build, look at this:

    def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
      adapter_options ||= {}
      options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
      if (fieldset = adapter_options[:fieldset])
        options[:fields] = fieldset.fields_for(json_key)
      end

      resource = attributes_hash(adapter_options, options, adapter_instance)
      relationships = associations_hash(adapter_options, options, adapter_instance)
      resource.merge(relationships)
    end
    alias to_hash serializable_hash
    alias to_h serializable_hash

    # @see #serializable_hash
    def as_json(adapter_opts = nil)
      serializable_hash(adapter_opts)
    end

@wasifhossain FYI: I've open the detailed issue here: https://github.com/rails-api/active_model_serializers/issues/2339, any help is appreciated. Thank you

I will take a deeper look into the new issue in coming days. thanks @Mifrill for the details

Was this page helpful?
0 / 5 - 0 ratings