From 0ea5ae86fffac7c93ba37fb9b217ddf1e00f007a Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Tue, 25 Apr 2023 18:25:57 +0400 Subject: [PATCH] DEV: return user IDs on the user search route (#21206) We call the `/u/search/users` URL when autocompleting users. It returns user's name, username and avatar template, but not user ID. We need it to return user IDs in order to display user status in certain situations. I could add ID to FoundUserWithStatusSerializer, so it will be added only if user status is enabled in site settings. But I feel that it's good to always return it, it's not a lot of data comparing to what we already return, and it should be useful in other scenarios. --- app/serializers/found_user_serializer.rb | 2 +- spec/serializers/found_user_serializer_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/serializers/found_user_serializer.rb b/app/serializers/found_user_serializer.rb index 6bdf045563f..4c2da44e614 100644 --- a/app/serializers/found_user_serializer.rb +++ b/app/serializers/found_user_serializer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class FoundUserSerializer < ApplicationSerializer - attributes :username, :name, :avatar_template + attributes :id, :username, :name, :avatar_template def include_name? SiteSetting.enable_names? diff --git a/spec/serializers/found_user_serializer_spec.rb b/spec/serializers/found_user_serializer_spec.rb index 43db8092196..9793306bf7e 100644 --- a/spec/serializers/found_user_serializer_spec.rb +++ b/spec/serializers/found_user_serializer_spec.rb @@ -4,6 +4,14 @@ RSpec.describe FoundUserSerializer do fab!(:user) { Fabricate(:user) } let(:serializer) { described_class.new(user, root: false) } + describe "#id" do + it "returns user id" do + json = serializer.as_json + expect(json.keys).to include :id + expect(json[:id]).to eq(user.id) + end + end + describe "#name" do it "returns name if enabled in site settings" do SiteSetting.enable_names = true