diff options
author | Javan Makhmali <javan@javan.us> | 2018-04-10 06:36:47 -0400 |
---|---|---|
committer | Javan Makhmali <javan@javan.us> | 2018-04-10 06:36:47 -0400 |
commit | efb7dc65d9e38a824e1e8dbeb376b3fab5c7605b (patch) | |
tree | 5673b305310972240d3f97799f9f7a7e6de187b8 | |
parent | c6ae623955909141fbea33e30742596423f61f78 (diff) | |
download | rails-efb7dc65d9e38a824e1e8dbeb376b3fab5c7605b.tar.gz rails-efb7dc65d9e38a824e1e8dbeb376b3fab5c7605b.tar.bz2 rails-efb7dc65d9e38a824e1e8dbeb376b3fab5c7605b.zip |
Always exclude JSON root from direct_uploads#create response
The JavaScript component expects a bare response.
Fixes #32365
-rw-r--r-- | activestorage/app/controllers/active_storage/direct_uploads_controller.rb | 2 | ||||
-rw-r--r-- | activestorage/test/controllers/direct_uploads_controller_test.rb | 23 |
2 files changed, 24 insertions, 1 deletions
diff --git a/activestorage/app/controllers/active_storage/direct_uploads_controller.rb b/activestorage/app/controllers/active_storage/direct_uploads_controller.rb index cbe2cf2c35..78b43fc94c 100644 --- a/activestorage/app/controllers/active_storage/direct_uploads_controller.rb +++ b/activestorage/app/controllers/active_storage/direct_uploads_controller.rb @@ -15,7 +15,7 @@ class ActiveStorage::DirectUploadsController < ActiveStorage::BaseController end def direct_upload_json(blob) - blob.as_json(methods: :signed_id).merge(direct_upload: { + blob.as_json(root: false, methods: :signed_id).merge(direct_upload: { url: blob.service_url_for_direct_upload, headers: blob.service_headers_for_direct_upload }) diff --git a/activestorage/test/controllers/direct_uploads_controller_test.rb b/activestorage/test/controllers/direct_uploads_controller_test.rb index 88d85e12ab..1b16da17d9 100644 --- a/activestorage/test/controllers/direct_uploads_controller_test.rb +++ b/activestorage/test/controllers/direct_uploads_controller_test.rb @@ -121,4 +121,27 @@ class ActiveStorage::DiskDirectUploadsControllerTest < ActionDispatch::Integrati assert_equal({ "Content-Type" => "text/plain" }, details["direct_upload"]["headers"]) end end + + test "creating new direct upload does not include root in json" do + checksum = Digest::MD5.base64digest("Hello") + + set_include_root_in_json(true) do + post rails_direct_uploads_url, params: { blob: { + filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain" } } + end + + @response.parsed_body.tap do |details| + assert_nil details["blob"] + assert_not_nil details["id"] + end + end + + private + def set_include_root_in_json(value) + original = ActiveRecord::Base.include_root_in_json + ActiveRecord::Base.include_root_in_json = value + yield + ensure + ActiveRecord::Base.include_root_in_json = original + end end |