From 12a3fdc246033eae3cde585aef5fb881d1937d99 Mon Sep 17 00:00:00 2001 From: Linca <41134017+Lhcfl@users.noreply.github.com> Date: Mon, 7 Jul 2025 08:46:49 +0800 Subject: [PATCH] FIX: Don't add `only: nil` serializing options of data explorer results (#381) `only: nil` will cause ArraySerializer to overwrite the default Serializer settings, which will cause Topic, Post, etc. to be serialized as empty `{}`. We need to exclude the configuration without only. The current test case only ensures that no error is raised, but does not guarantee the accuracy of the result, which indirectly introduces this bug. Additional tests are added to ensure consistent results. --- lib/discourse_data_explorer/data_explorer.rb | 8 +++----- spec/data_explorer_spec.rb | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/discourse_data_explorer/data_explorer.rb b/lib/discourse_data_explorer/data_explorer.rb index 8f01b1d..ff43fa5 100644 --- a/lib/discourse_data_explorer/data_explorer.rb +++ b/lib/discourse_data_explorer/data_explorer.rb @@ -198,11 +198,9 @@ module ::DiscourseDataExplorer .includes(support_info[:include]) .order(:id) - ret[cls] = ActiveModel::ArraySerializer.new( - all_objs, - each_serializer: support_info[:serializer], - only: support_info[:only], - ) + opts = { each_serializer: support_info[:serializer] } + opts[:only] = support_info[:only] if support_info[:only] + ret[cls] = ActiveModel::ArraySerializer.new(all_objs, **opts) end [ret, col_map] end diff --git a/spec/data_explorer_spec.rb b/spec/data_explorer_spec.rb index af0385b..9fa562b 100644 --- a/spec/data_explorer_spec.rb +++ b/spec/data_explorer_spec.rb @@ -100,6 +100,9 @@ describe DiscourseDataExplorer::DataExplorer do records = relations[:topic].object records.map { |t| BasicTopicSerializer.new(t, root: false).as_json } }.not_to raise_error + + json = relations[:topic].as_json + expect(json).to include(BasicTopicSerializer.new(topic, root: false).as_json) end it "chooses the correct serializer for tag_group" do