From 906d772202d223b4481966e4d960013782f256ff Mon Sep 17 00:00:00 2001 From: "J.W. Koelewijn" Date: Wed, 14 Jan 2015 16:35:11 +0100 Subject: [PATCH 1/2] Look for namespace and other options to configure serializers By default not all serializer options are considered (for instance the :root option, or the :each_serializer option). This potentially results in an incomplete configuration of the serializers used. Added 2 specs to test this when used as a single endpoint and when used within a namespace. At the moment it is fixed by plucking the correct settings in the .build_options_from_endpoint, but I could imagine that this is not exactly the right spot. --- .../formatter.rb | 2 +- .../formatter_spec.rb | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/grape-active_model_serializers/formatter.rb b/lib/grape-active_model_serializers/formatter.rb index 4398be3..ce41fde 100644 --- a/lib/grape-active_model_serializers/formatter.rb +++ b/lib/grape-active_model_serializers/formatter.rb @@ -51,7 +51,7 @@ def meta_key=(key) end def build_options_from_endpoint(endpoint) - [endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options].reduce(:merge) + [endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options, endpoint.options, endpoint.options.fetch(:route_options)].reduce(:merge) end # array root is the innermost namespace name ('space') if there is one, diff --git a/spec/grape-active_model_serializers/formatter_spec.rb b/spec/grape-active_model_serializers/formatter_spec.rb index 079bdbd..73b83de 100644 --- a/spec/grape-active_model_serializers/formatter_spec.rb +++ b/spec/grape-active_model_serializers/formatter_spec.rb @@ -32,13 +32,32 @@ end end + describe 'serializer options from namespace' do + let(:app){ Class.new(Grape::API) } + + before do + app.format :json + app.formatter :json, Grape::Formatter::ActiveModelSerializers + + app.namespace('space') do |ns| + ns.get('/', root: false) do + { user: { first_name: 'JR', last_name: 'HE' } } + end + end + end + + it 'should read serializer options like "root"' do + expect(described_class.build_options_from_endpoint(app.endpoints.first)).to include :root + end + end + describe '.fetch_serializer' do let(:user) { User.new(first_name: 'John') } if Grape::Util.const_defined?('InheritableSetting') - let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo') } + let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo', root: false) } else - let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo') } + let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo', root: false) } end let(:env) { { 'api.endpoint' => endpoint } } @@ -65,5 +84,9 @@ def endpoint.default_serializer_options expect(subject.instance_variable_get('@only')).to eq([:only]) expect(subject.instance_variable_get('@except')).to eq([:except]) end + + it 'should read serializer options like "root"' do + expect(described_class.build_options_from_endpoint(endpoint).keys).to include :root + end end end From 6b16d221231c6cc50ba7f1b44e5fab9d5ee3c51c Mon Sep 17 00:00:00 2001 From: "J.W. Koelewijn" Date: Wed, 14 Jan 2015 16:57:56 +0100 Subject: [PATCH 2/2] Changed some cosmetics in layout --- spec/grape-active_model_serializers/formatter_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/grape-active_model_serializers/formatter_spec.rb b/spec/grape-active_model_serializers/formatter_spec.rb index 73b83de..06885f2 100644 --- a/spec/grape-active_model_serializers/formatter_spec.rb +++ b/spec/grape-active_model_serializers/formatter_spec.rb @@ -33,7 +33,7 @@ end describe 'serializer options from namespace' do - let(:app){ Class.new(Grape::API) } + let(:app) { Class.new(Grape::API) } before do app.format :json