Skip to content

Commit 54d3934

Browse files
committed
Fix JSON-encoding ActiveStorage::Filename
ActiveStorage::Filename was missing quotes when encoded, generating invalid json like this - ``` JSON.generate(foo: ActiveStorage::Filename.new("bar.pdf") # => '{"foo":bar.pdf}' ``` Delete to_json and rely on the implementation from ActiveSupport::ToJsonWithActiveSupportEncoder
1 parent 6b2a9f1 commit 54d3934

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

activestorage/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix JSON-encoding of `ActiveStorage::Filename` instances.
2+
3+
*Jonathan del Strother*
4+
15
* Fix N+1 query when fetching preview images for non-image assets
26

37
*Aaron Patterson & Justin Searls*

activestorage/app/models/active_storage/filename.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ def as_json(*)
6969
to_s
7070
end
7171

72-
def to_json
73-
to_s
74-
end
75-
7672
def <=>(other)
7773
to_s.downcase <=> other.to_s.downcase
7874
end

activestorage/test/models/filename_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ class ActiveStorage::FilenameTest < ActiveSupport::TestCase
5353
test "compare sanitized" do
5454
assert_operator ActiveStorage::Filename.new("foo-bar.pdf"), :==, ActiveStorage::Filename.new("foo\tbar.pdf")
5555
end
56+
57+
test "encoding to json" do
58+
assert_equal '"foo.pdf"', ActiveStorage::Filename.new("foo.pdf").to_json
59+
assert_equal '{"filename":"foo.pdf"}', { filename: ActiveStorage::Filename.new("foo.pdf") }.to_json
60+
assert_equal '{"filename":"foo.pdf"}', JSON.generate(filename: ActiveStorage::Filename.new("foo.pdf"))
61+
end
5662
end

0 commit comments

Comments
 (0)