-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
This is useful to me! I want to selectively use different serializers for different endpoints in my app. |
This commit fixed my issue from #916. Thanks! |
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. |
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! |
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 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:
Our solution was to define a
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 |
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
|
I added an
namespace
option to the serializer associations and array serializer. This option can be used to define in whichmodule
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. :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 asuffix
option for example).