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

Include namespace when looking up superclass #2172

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Fixes:
- [#2223](https://github.com/rails-api/active_model_serializers/pull/2223) Support Fieldset in Attributes/JSON adapters documented in [docs/general/fields.md](https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/fields.md) that worked partially before (@bf4)
- [#2337](https://github.com/rails-api/active_model_serializers/pull/2337) fix incorrect belongs_to serialization when foreign_key on object and belongs_to is blank (@InteNs)
- Fixes incorrect json-api generation when `jsonapi_use_foreign_key_on_belongs_to_relationship` is `true` and the relationship is blank
- [#2172](https://github.com/rails-api/active_model_serializers/pull/2172) Preserve the namespace when falling back to a superclass serializer

Misc:

Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def self.get_serializer_for(klass, namespace = nil)
if serializer_class
serializer_class
elsif klass.superclass
get_serializer_for(klass.superclass)
get_serializer_for(klass.superclass, namespace)
else
nil # No serializer found
end
Expand Down
6 changes: 6 additions & 0 deletions test/serializers/serializer_for_with_namespace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Book < ::Model
attributes :title, :author_name
associations :publisher, :pages
end
class Ebook < Book; end
class Page < ::Model; attributes :number, :text end
class Publisher < ::Model; attributes :name end

Expand Down Expand Up @@ -85,6 +86,11 @@ class BookSerializer < ActiveModel::Serializer
}
assert_equal expected, result
end

test 'follows inheritance with a namespace' do
serializer = ActiveModel::Serializer.serializer_for(Ebook.new, namespace: Api::V3)
assert_equal Api::V3::BookSerializer, serializer
end
end
end
end