From 0b98080259d6d5e7416b7d2a2006c6800ac2fabd Mon Sep 17 00:00:00 2001 From: Mateus Nava Date: Thu, 29 Aug 2019 23:30:21 -0300 Subject: [PATCH 1/2] fix: Accept nil serializer --- CHANGELOG.md | 1 + .../serializer_resolver.rb | 3 +++ .../serializer_resolver_spec.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1399b27..b639570 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 1.5.3 (Next) * Your contribution here. +* [#92](https://github.com/ruby-grape/grape-active_model_serializers/pull/92): Fix: accept nil serializer - [@mateusnava](https://github.com/mateusnava). ### 1.5.2 (March 14, 2018) diff --git a/lib/grape-active_model_serializers/serializer_resolver.rb b/lib/grape-active_model_serializers/serializer_resolver.rb index 13220a1..69e49e3 100644 --- a/lib/grape-active_model_serializers/serializer_resolver.rb +++ b/lib/grape-active_model_serializers/serializer_resolver.rb @@ -16,6 +16,9 @@ def serializer def serializer_class return @serializer_class if defined?(@serializer_class) + + return nil if options.key?(:serializer) && options[:serializer].nil? + @serializer_class = resource_defined_class @serializer_class ||= collection_class @serializer_class ||= options[:serializer] diff --git a/spec/grape/active_model_serializers/serializer_resolver_spec.rb b/spec/grape/active_model_serializers/serializer_resolver_spec.rb index da10876..477b9f5 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( From b61d4c8dc8aab6b6abcb13268a1d8701c07fad22 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Tue, 3 Jun 2025 14:09:58 -0700 Subject: [PATCH 2/2] Obey the law --- .../serializer_resolver.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/grape-active_model_serializers/serializer_resolver.rb b/lib/grape-active_model_serializers/serializer_resolver.rb index 1ade05c..dbafdb5 100644 --- a/lib/grape-active_model_serializers/serializer_resolver.rb +++ b/lib/grape-active_model_serializers/serializer_resolver.rb @@ -19,12 +19,12 @@ def serializer_class return nil if options.key?(:serializer) && options[:serializer].nil? - @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 + @serializer_class = resource_defined_class || + collection_class || + options[:serializer] || + namespace_inferred_class || + version_inferred_class || + resource_serializer_class end def serializer_options