Active_model_serializers: [Bug] ActionController#namespace_for_serializer doesn't read attribute correctly

Created on 9 Aug 2017  路  5Comments  路  Source: rails-api/active_model_serializers

Version 0.10.6

Current implementation:

# lib/action_controller/serialization.rb

def namespace_for_serializer
    @namespace_for_serializer ||= self.class.parent unless self.class.parent == Object
end

If self.class.parent is an Object (which is always true for class without namespace), #namespace_for_serializer returns nil instead of @namespace_for_serializer.

Most helpful comment

When the controller is not in any namespace, setting namespace_for_serializer doesn't have any effect.

Only redefining the method namespace_for_serializer or putting the controller in a namespace works.

All 5 comments

How is this a bug?

When the controller is not in any namespace, setting namespace_for_serializer doesn't have any effect.

Only redefining the method namespace_for_serializer or putting the controller in a namespace works.

I'm having the same issue when trying to set namespace_for_serializer in a controller that is not inside a namespace. I believe the OP is correct in that this is a bug because the logic ignores the @namespace_for_serializer value that was set in the controller only in the case when the controller is not inside a namespace. I can't think of a reason why this would be the desired behavior as this functionality is potentially useful on any controller not just one inside of a namespace.

As a workaround setting namespace on the actual render call does work.

render json: @post, namespace: Api::V2

I'm having the same issue, and to set namespace on action not working too for me.

Please open a new issue

Was this page helpful?
0 / 5 - 0 ratings