Skip to content

Commit 61bd6a7

Browse files
authored
Merge pull request rails#54584 from Edouard-chin/ec-msgpack-safebuffer
Add SafeBuffer to the list of messagepack serializable type:
2 parents 335490a + ae3eecb commit 61bd6a7

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

actionpack/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
* The Cookie Serializer can now serialize an Active Support SafeBuffer when using message pack.
2+
3+
Such code would previously produce an error if an application was using messagepack as its cookie serializer.
4+
5+
```ruby
6+
class PostController < ApplicationController
7+
def index
8+
flash.notice = t(:hello_html) # This would try to serialize a SafeBuffer, which was not possible.
9+
end
10+
end
11+
```
12+
13+
*Edouard Chin*
14+
115
* Fix `Rails.application.reload_routes!` from clearing almost all routes.
216

317
When calling `Rails.application.reload_routes!` inside a middleware of

actionpack/test/controller/flash_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ def set_flash
263263
head :ok
264264
end
265265

266+
def set_html_flash
267+
flash["that"] = ActiveSupport::SafeBuffer.new("<p>Hello world</p>")
268+
head :ok
269+
end
270+
266271
def set_flash_now
267272
flash.now["that"] = "hello"
268273
head :ok
@@ -297,6 +302,18 @@ def test_flash
297302
end
298303
end
299304

305+
def test_flash_safebuffer
306+
with_test_route_set do
307+
get "/set_html_flash", env: { "action_dispatch.cookies_serializer" => :message_pack }
308+
assert_response :success
309+
assert_equal "<p>Hello world</p>", @request.flash["that"]
310+
311+
get "/use_flash", env: { "action_dispatch.cookies_serializer" => :message_pack }
312+
assert_response :success
313+
assert_equal "flash: <p>Hello world</p>", @response.body
314+
end
315+
end
316+
300317
def test_just_using_flash_does_not_stream_a_cookie_back
301318
with_test_route_set do
302319
get "/use_flash"

activesupport/lib/active_support/message_pack/extensions.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require "uri/generic"
88
require "msgpack/bigint"
99
require "active_support/hash_with_indifferent_access"
10+
require "active_support/core_ext/string/output_safety"
1011
require "active_support/time"
1112

1213
module ActiveSupport
@@ -102,6 +103,10 @@ def install(registry)
102103
packer: method(:write_hash_with_indifferent_access),
103104
unpacker: method(:read_hash_with_indifferent_access),
104105
recursive: true
106+
107+
registry.register_type 18, ActiveSupport::SafeBuffer,
108+
packer: :to_s,
109+
unpacker: :new
105110
end
106111

107112
def install_unregistered_type_error(registry)

activesupport/test/message_pack/shared_serializer_tests.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module MessagePackSharedSerializerTests
2424
15 => Pathname,
2525
16 => Regexp,
2626
17 => ActiveSupport::HashWithIndifferentAccess,
27+
18 => ActiveSupport::SafeBuffer,
2728
127 => Object,
2829
}
2930

0 commit comments

Comments
 (0)