From a0a7f8d99962c6bbb326a2e024733c57cb031fe2 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Tue, 20 May 2025 18:54:58 +0800 Subject: [PATCH] FIX: Add new attribute that is required when using the basic topic serializer (#375) In discourse/discourse#32618 we added the dependency on locales to the basic topic serializer gated behind SiteSetting.experimental_content_localization. This is so that fancy_titles can be rendered in the language of the user. Error: `ActiveModel::MissingAttributeError (missing attribute 'locale' for Topic)` This commit adds that dependency to extra_data_pluck_fields which lists fields required from the serializer. --- lib/discourse_data_explorer/data_explorer.rb | 2 +- spec/data_explorer_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/discourse_data_explorer/data_explorer.rb b/lib/discourse_data_explorer/data_explorer.rb index 2dd1777..bffb96e 100644 --- a/lib/discourse_data_explorer/data_explorer.rb +++ b/lib/discourse_data_explorer/data_explorer.rb @@ -115,7 +115,7 @@ module ::DiscourseDataExplorer }, topic: { class: Topic, - fields: %i[id title slug posts_count], + fields: %i[id title slug posts_count locale], serializer: BasicTopicSerializer, }, group: { diff --git a/spec/data_explorer_spec.rb b/spec/data_explorer_spec.rb index 2c0a664..bef36cc 100644 --- a/spec/data_explorer_spec.rb +++ b/spec/data_explorer_spec.rb @@ -87,6 +87,21 @@ describe DiscourseDataExplorer::DataExplorer do _, colrender = DiscourseDataExplorer::DataExplorer.add_extra_data(result[:pg_result]) expect(colrender).to eq({ 1 => "json" }) end + + describe "serializing models to serializer" do + it "serializes correctly to BasicTopicSerializer for topic relations" do + topic = Fabricate(:topic, locale: "ja") + query = Fabricate(:query, sql: "SELECT id AS topic_id FROM topics WHERE id = #{topic.id}") + + pg_result = described_class.run_query(query)[:pg_result] + relations, _ = DiscourseDataExplorer::DataExplorer.add_extra_data(pg_result) + + expect { + records = relations[:topic].object + records.map { |t| BasicTopicSerializer.new(t, root: false).as_json } + }.not_to raise_error + end + end end end end