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

Added serializer namespacing option #879

Closed
wants to merge 1 commit into from

Conversation

groyoh
Copy link
Member

@groyoh groyoh commented Apr 23, 2015

I added an namespace option to the serializer associations and array serializer. This option can be used to define in which module the correct serializer may be found. This option is pretty useful when dealing with versionized serializer and polymorphic associations. I added some information on how to use it in the README, e.g. :

You may use the :namespace options to specify the module where the serializer(s) of an
association may be found, for example:

has_many :comments, namespace: Blogging::Serializers

The serializer used then depends on the class of the object to be serialized e.g. if a
comment is an instance of class AdminComment, the serializer would use
Blogging::Serializers::AdminComment to serializer this comment.

To make it clear, I do not use rails so I don't know if this would fit well with rails. I'm opened to any kind of discussions and changes. And of course, serializers here are used without Serializer suffix because that's the way we use it in our team but this behavior may as well be changed (adding a suffix option for example).

@groyoh groyoh changed the title Added serializer namespacing Added serializer namespacing option Apr 28, 2015
@cfeckardt
Copy link

This is useful to me!

I want to selectively use different serializers for different endpoints in my app.
One of these associations are polymorphic, and should use different serializers, depending on which endpoint is hit. This PR solves this problem. 👍

@dpmccabe
Copy link

This commit fixed my issue from #916. Thanks!

@joaomdmoura
Copy link
Member

Just letting you guys know that I'm aware of this PR, but before merge it I have to discuss the implementation with other AMS members, it might take some weeks yet, but this is on my radar.
For now you can still workaround it by explicitly specifying the Serializer inside you action.

@bryanrite
Copy link

This would be very useful for me as well, as different versions of an API use differently namespaced serializers and working with polymorphic associations is very difficult. Thanks!

@bf4
Copy link
Member

bf4 commented Feb 10, 2016

Closing as it appears to have been resolved by #1225 Sorry for the lost work here :( Please re-open if there's more to discuss

@bf4 bf4 closed this Feb 10, 2016
@groyoh
Copy link
Member Author

groyoh commented Feb 10, 2016

@bf4 I completely forgot about this issue over time. Unfortunately #1225 does not solve our issue. We are mostly using sinatra apps and follow our own structure which does not fit with Rails and AMS convention. Basically, our structure is defined like so (purely fictional example):

module Model
  class User; end
  class Computer; end
  class Laptop < Computer; end
  class Server < Computer; end
end

module Serializer
  module V1 # for API version 1
    class User
      has_many :computers # can be Laptop or Server
    end
    class Laptop
    end
    class Server
    end
  end

  module V2 # for API version 2
    class User
      has_many :computers # can be Laptop or Server
    end
    class Laptop
    end
    class Server
    end
  end
end

So there are 5 issues here that we have to tackle:

  • We want to define many different serializer namespaces (one per API version)
  • The serializers and models are not defined in the same namespace
  • The serializers do not end with Serializer (this is actually a different concern but I still wanted to point it out)
  • The has_many :computers is a polymorphic association which can have instances of Laptop and Server
  • The Laptop and Server serializer do not fit as nested class in the User serializer because they can be used outside of the scope of the User serializer. So Add support for nested serializers #1225 does not apply too much in this case.

Our solution was to define a namespace option which could be used in two different ways:

  • ArraySerializer.new(computers, namespace: Serializer::V2) which would look up the serializer for the array elements in the Serializer::V2 module.
  • With a relationship:
module Serializer
  module V1
    class User
      has_many :computers, namespace: Serializer::V1
    end
  end
end

serializer = V1::User.new(user) # this will lookup the `Laptop` and `Server` serializers within the `Serializer::V1` namespace

Do you have any opinion on how to solve these issues with having a namespace option or strictly respecting Rails/AMS conventions? Maybe I missed a feature that could help us here. As always, if my example was not clear enough I could give in more details.

@bf4
Copy link
Member

bf4 commented Feb 10, 2016

Maybe we can write failing tests for each scenario. There is also another pr for adding a namespace option to the serialization context like 0.9 had. That should help, I think. #1436

B mobile phone

On Feb 10, 2016, at 5:48 AM, Yohan Robert notifications@github.com wrote:

@bf4 I completely forgot about this issue over time. Unfortunately #1225 does not solve our issue. We are mostly using sinatra apps and follow our own structure which does not fit with Rails and AMS convention. Basically, our structure is defined like so (purely fictional example):

module Model
class User; end
class Computer; end
class Laptop < Computer; end
class Server < Computer; end
end

module Serializer
module V1 # for API version 1
class User
has_many :computers # can be Laptop or Server
end
class Laptop
end
class Server
end
end

module V2 # for API version 2
class User
has_many :computers # can be Laptop or Server
end
class Laptop
end
class Server
end
end
end
So there are 5 issues here that we have to tackle:

We want to define many different serializer namespaces (one per API version)
The serializers and models are not defined in the same namespace
The serializers do not end with Serializer (this is actually a different concern but I still wanted to point it out)
The has_many :computers is a polymorphic association which can have instances of Laptop and Server
The Laptop and Server serializer do not fit as nested class in the User serializer because they can be used outside of the scope of the User serializer. So #1225 does not apply too much in this case.
Our solution was to define a namespace option which could be used in two different ways:

ArraySerializer.new(computers, namespace: Serializer::V2) which would look up the serializer for the array elements in the Serializer::V2 module.
With a relationship:
module Serializer
module V1
class User
has_many :computers, namespace: Serializer::V1
end
end
end

serializer = V1::User.new(user) # this will lookup the Laptop and Server serializers within the Serializer::V1 namespace
Do you have any opinion on how to solve these issues with having a namespace option or strictly respecting Rails/AMS conventions? Maybe I missed a feature that could help us here. As always, if my example was not clear enough I could give in more details.


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants