From 036a24cf302eef775ef066b19348b50a4404052e Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Thu, 19 Dec 2019 09:48:01 -0800 Subject: [PATCH] FIX: correct user serializer user method for extended serializer (#8590) A small fix for Basic User Serializers where some downstream serializers do not correctly set user objects. This caused some issues in certain plugins that depend on the user method to return a user. --- app/serializers/basic_user_serializer.rb | 2 +- spec/fabricators/post_action_fabricator.rb | 7 +++++++ spec/serializers/basic_user_serializer_spec.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 spec/fabricators/post_action_fabricator.rb diff --git a/app/serializers/basic_user_serializer.rb b/app/serializers/basic_user_serializer.rb index ca979f82d83..964cb5b3e71 100644 --- a/app/serializers/basic_user_serializer.rb +++ b/app/serializers/basic_user_serializer.rb @@ -20,6 +20,6 @@ class BasicUserSerializer < ApplicationSerializer end def user - object[:user] || object + object[:user] || object.try(:user) || object end end diff --git a/spec/fabricators/post_action_fabricator.rb b/spec/fabricators/post_action_fabricator.rb new file mode 100644 index 00000000000..ba4568555dc --- /dev/null +++ b/spec/fabricators/post_action_fabricator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +Fabricator(:post_action) do + post + user + post_action_type_id PostActionType.types[:like] +end diff --git a/spec/serializers/basic_user_serializer_spec.rb b/spec/serializers/basic_user_serializer_spec.rb index 9109e068f6c..2f2cddb58bc 100644 --- a/spec/serializers/basic_user_serializer_spec.rb +++ b/spec/serializers/basic_user_serializer_spec.rb @@ -14,6 +14,14 @@ describe BasicUserSerializer do expect(json[:avatar_template]).to eq(user.avatar_template) end + describe 'extended serializers' do + let(:post_action) { Fabricate(:post_action, user: user) } + let(:serializer) { PostActionUserSerializer.new(post_action, scope: Guardian.new(user), root: false) } + it "returns the user correctly" do + expect(serializer.user.username).to eq(user.username) + end + end + it "doesn't return the name it when `enable_names` is false" do SiteSetting.enable_names = false expect(json[:name]).to eq(nil)