FIX: post revision serializer when tags is a string (#27499)

In some instances, the `modifications` of `tags` hasn't been properly serialized as a Ruby array but rather as a string (I've seen `""`, `"[]"`, and `"[\"\"]"`).

This generates an error when we try to `filter_tags` and remove `hidden_tags` (which is an array) from `tags` which might be a string.

Internal ref - t/131126

I wasn't able to figure out the root cause of this so I reverted the behavior that was introduced ~6 years ago in f2c060bdf2
This commit is contained in:
Régis Hanol 2024-06-21 00:09:21 +02:00 committed by GitHub
parent c1f477c1b6
commit 22128ff1ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -279,7 +279,7 @@ class PostRevisionSerializer < ApplicationSerializer
end
def filter_tags(tags)
tags - hidden_tags
tags.is_a?(Array) && tags.any? ? tags - hidden_tags : tags
end
def filter_category_id(category_id)

View File

@ -41,6 +41,16 @@ RSpec.describe PostRevisionSerializer do
end
end
it "handles tags not being an array" do
pr = Fabricate(:post_revision, post: post, modifications: { "tags" => ["[]", ""] })
json =
PostRevisionSerializer.new(pr, scope: Guardian.new(Fabricate(:user)), root: false).as_json
expect(json[:tags_changes][:previous]).to eq("[]")
expect(json[:tags_changes][:current]).to eq([])
end
context "with hidden tags" do
fab!(:public_tag) { Fabricate(:tag, name: "public") }
fab!(:public_tag2) { Fabricate(:tag, name: "visible") }