FIX: current user serializer consistently returns {} for custom_fields

Resolves: #5210
This commit is contained in:
Sam 2017-11-15 11:55:37 +11:00
parent 4c4410225e
commit 813e21d0e8
3 changed files with 22 additions and 2 deletions

View File

@ -135,7 +135,7 @@ class CurrentUserSerializer < BasicUserSerializer
end
if fields.present?
User.custom_fields_for_ids([object.id], fields)[object.id]
User.custom_fields_for_ids([object.id], fields)[object.id] || {}
else
{}
end

View File

@ -388,7 +388,7 @@ class UserSerializer < BasicUserSerializer
end
if fields.present?
User.custom_fields_for_ids([object.id], fields)[object.id]
User.custom_fields_for_ids([object.id], fields)[object.id] || {}
else
{}
end

View File

@ -149,6 +149,14 @@ describe Plugin::Instance do
end
context "serialized_current_user_fields" do
before do
DiscoursePluginRegistry.serialized_current_user_fields << "has_car"
end
after do
DiscoursePluginRegistry.serialized_current_user_fields.delete "has_car"
end
it "correctly serializes custom user fields" do
DiscoursePluginRegistry.serialized_current_user_fields << "has_car"
user = Fabricate(:user)
@ -157,6 +165,18 @@ describe Plugin::Instance do
payload = JSON.parse(CurrentUserSerializer.new(user, scope: Guardian.new(user)).to_json)
expect(payload["current_user"]["custom_fields"]["has_car"]).to eq("true")
payload = JSON.parse(UserSerializer.new(user, scope: Guardian.new(user)).to_json)
expect(payload["user"]["custom_fields"]["has_car"]).to eq("true")
UserCustomField.destroy_all
user.reload
payload = JSON.parse(CurrentUserSerializer.new(user, scope: Guardian.new(user)).to_json)
expect(payload["current_user"]["custom_fields"]).to eq({})
payload = JSON.parse(UserSerializer.new(user, scope: Guardian.new(user)).to_json)
expect(payload["user"]["custom_fields"]).to eq({})
end
end