diff --git a/CHANGELOG.md b/CHANGELOG.md index 052b244..94c4e37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### 2.0.1 (Next) +* [#92](https://github.com/ruby-grape/grape-active_model_serializers/pull/92): Fix: accept nil serializer - [@mateusnava](https://github.com/mateusnava). * Your contribution here. ### 2.0.0 (2025/06/02) diff --git a/lib/grape-active_model_serializers/serializer_resolver.rb b/lib/grape-active_model_serializers/serializer_resolver.rb index 5ae2b0b..dbafdb5 100644 --- a/lib/grape-active_model_serializers/serializer_resolver.rb +++ b/lib/grape-active_model_serializers/serializer_resolver.rb @@ -17,12 +17,14 @@ def serializer def serializer_class return @serializer_class if defined?(@serializer_class) - @serializer_class = resource_defined_class - @serializer_class ||= collection_class - @serializer_class ||= options[:serializer] - @serializer_class ||= namespace_inferred_class - @serializer_class ||= version_inferred_class - @serializer_class ||= resource_serializer_class + return nil if options.key?(:serializer) && options[:serializer].nil? + + @serializer_class = resource_defined_class || + collection_class || + options[:serializer] || + namespace_inferred_class || + version_inferred_class || + resource_serializer_class end def serializer_options diff --git a/spec/grape/active_model_serializers/serializer_resolver_spec.rb b/spec/grape/active_model_serializers/serializer_resolver_spec.rb index e381b23..eb036ee 100644 --- a/spec/grape/active_model_serializers/serializer_resolver_spec.rb +++ b/spec/grape/active_model_serializers/serializer_resolver_spec.rb @@ -52,6 +52,18 @@ expect(serializer).to be_kind_of(serializer_class) end + context 'specified nil by options' do + let(:options) { + super().merge( + serializer: nil + ) + } + + it 'returns nil' do + expect(serializer).to be_nil + end + end + context 'each serializer' do let(:options) { super().except(:serializer).merge(