From c0fcd877a518b71acffed871806cf564ea1e161c Mon Sep 17 00:00:00 2001 From: Lhc_fl Date: Fri, 4 Jul 2025 16:54:47 +0800 Subject: [PATCH] FIX: Don't add `only: nil` serializing options of data explorer results `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 8f01b1d4..ff43fa5d 100644 --- a/lib/discourse_data_explorer/data_explorer.rb +++ b/lib/discourse_data_explorer/data_explorer.rb @@ -198,11 +198,9 @@ def self.add_extra_data(pg_result) .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 af0385b1..9fa562bb 100644 --- a/spec/data_explorer_spec.rb +++ b/spec/data_explorer_spec.rb @@ -100,6 +100,9 @@ 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