You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.0 KiB

Change IDs to strings rather than numbers in API JSON output (#5019) * Fix JavaScript interface with long IDs Somewhat predictably, the JS interface handled IDs as numbers, which in JS are IEEE double-precision floats. This loses some precision when working with numbers as large as those generated by the new ID scheme, so we instead handle them here as strings. This is relatively simple, and doesn't appear to have caused any problems, but should definitely be tested more thoroughly than the built-in tests. Several days of use appear to support this working properly. BREAKING CHANGE: The major(!) change here is that IDs are now returned as strings by the REST endpoints, rather than as integers. In practice, relatively few changes were required to make the existing JS UI work with this change, but it will likely hit API clients pretty hard: it's an entirely different type to consume. (The one API client I tested, Tusky, handles this with no problems, however.) Twitter ran into this issue when introducing Snowflake IDs, and decided to instead introduce an `id_str` field in JSON responses. I have opted to *not* do that, and instead force all IDs to 64-bit integers represented by strings in one go. (I believe Twitter exacerbated their problem by rolling out the changes three times: once for statuses, once for DMs, and once for user IDs, as well as by leaving an integer ID value in JSON. As they said, "If you’re using the `id` field with JSON in a Javascript-related language, there is a very high likelihood that the integers will be silently munged by Javascript interpreters. In most cases, this will result in behavior such as being unable to load or delete a specific direct message, because the ID you're sending to the API is different than the actual identifier associated with the message." [1]) However, given that this is a significant change for API users, alternatives or a transition time may be appropriate. 1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html * Additional fixes for stringified IDs in JSON These should be the last two. These were identified using eslint to try to identify any plain casts to JavaScript numbers. (Some such casts are legitimate, but these were not.) Adding the following to .eslintrc.yml will identify casts to numbers: ~~~ no-restricted-syntax: - warn - selector: UnaryExpression[operator='+'] > :not(Literal) message: Avoid the use of unary + - selector: CallExpression[callee.name='Number'] message: Casting with Number() may coerce string IDs to numbers ~~~ The remaining three casts appear legitimate: two casts to array indices, one in a server to turn an environment variable into a number. * Back out RelationshipsController Change This was made to make a test a bit less flakey, but has nothing to do with this branch. * Change internal streaming payloads to stringified IDs as well Per https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452 we need these changes to send deleted status IDs as strings, not integers.
7 years ago
Change IDs to strings rather than numbers in API JSON output (#5019) * Fix JavaScript interface with long IDs Somewhat predictably, the JS interface handled IDs as numbers, which in JS are IEEE double-precision floats. This loses some precision when working with numbers as large as those generated by the new ID scheme, so we instead handle them here as strings. This is relatively simple, and doesn't appear to have caused any problems, but should definitely be tested more thoroughly than the built-in tests. Several days of use appear to support this working properly. BREAKING CHANGE: The major(!) change here is that IDs are now returned as strings by the REST endpoints, rather than as integers. In practice, relatively few changes were required to make the existing JS UI work with this change, but it will likely hit API clients pretty hard: it's an entirely different type to consume. (The one API client I tested, Tusky, handles this with no problems, however.) Twitter ran into this issue when introducing Snowflake IDs, and decided to instead introduce an `id_str` field in JSON responses. I have opted to *not* do that, and instead force all IDs to 64-bit integers represented by strings in one go. (I believe Twitter exacerbated their problem by rolling out the changes three times: once for statuses, once for DMs, and once for user IDs, as well as by leaving an integer ID value in JSON. As they said, "If you’re using the `id` field with JSON in a Javascript-related language, there is a very high likelihood that the integers will be silently munged by Javascript interpreters. In most cases, this will result in behavior such as being unable to load or delete a specific direct message, because the ID you're sending to the API is different than the actual identifier associated with the message." [1]) However, given that this is a significant change for API users, alternatives or a transition time may be appropriate. 1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html * Additional fixes for stringified IDs in JSON These should be the last two. These were identified using eslint to try to identify any plain casts to JavaScript numbers. (Some such casts are legitimate, but these were not.) Adding the following to .eslintrc.yml will identify casts to numbers: ~~~ no-restricted-syntax: - warn - selector: UnaryExpression[operator='+'] > :not(Literal) message: Avoid the use of unary + - selector: CallExpression[callee.name='Number'] message: Casting with Number() may coerce string IDs to numbers ~~~ The remaining three casts appear legitimate: two casts to array indices, one in a server to turn an environment variable into a number. * Back out RelationshipsController Change This was made to make a test a bit less flakey, but has nothing to do with this branch. * Change internal streaming payloads to stringified IDs as well Per https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452 we need these changes to send deleted status IDs as strings, not integers.
7 years ago
Change IDs to strings rather than numbers in API JSON output (#5019) * Fix JavaScript interface with long IDs Somewhat predictably, the JS interface handled IDs as numbers, which in JS are IEEE double-precision floats. This loses some precision when working with numbers as large as those generated by the new ID scheme, so we instead handle them here as strings. This is relatively simple, and doesn't appear to have caused any problems, but should definitely be tested more thoroughly than the built-in tests. Several days of use appear to support this working properly. BREAKING CHANGE: The major(!) change here is that IDs are now returned as strings by the REST endpoints, rather than as integers. In practice, relatively few changes were required to make the existing JS UI work with this change, but it will likely hit API clients pretty hard: it's an entirely different type to consume. (The one API client I tested, Tusky, handles this with no problems, however.) Twitter ran into this issue when introducing Snowflake IDs, and decided to instead introduce an `id_str` field in JSON responses. I have opted to *not* do that, and instead force all IDs to 64-bit integers represented by strings in one go. (I believe Twitter exacerbated their problem by rolling out the changes three times: once for statuses, once for DMs, and once for user IDs, as well as by leaving an integer ID value in JSON. As they said, "If you’re using the `id` field with JSON in a Javascript-related language, there is a very high likelihood that the integers will be silently munged by Javascript interpreters. In most cases, this will result in behavior such as being unable to load or delete a specific direct message, because the ID you're sending to the API is different than the actual identifier associated with the message." [1]) However, given that this is a significant change for API users, alternatives or a transition time may be appropriate. 1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html * Additional fixes for stringified IDs in JSON These should be the last two. These were identified using eslint to try to identify any plain casts to JavaScript numbers. (Some such casts are legitimate, but these were not.) Adding the following to .eslintrc.yml will identify casts to numbers: ~~~ no-restricted-syntax: - warn - selector: UnaryExpression[operator='+'] > :not(Literal) message: Avoid the use of unary + - selector: CallExpression[callee.name='Number'] message: Casting with Number() may coerce string IDs to numbers ~~~ The remaining three casts appear legitimate: two casts to array indices, one in a server to turn an environment variable into a number. * Back out RelationshipsController Change This was made to make a test a bit less flakey, but has nothing to do with this branch. * Change internal streaming payloads to stringified IDs as well Per https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452 we need these changes to send deleted status IDs as strings, not integers.
7 years ago
  1. require 'rails_helper'
  2. RSpec.describe Api::V1::MediaController, type: :controller do
  3. render_views
  4. let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'write') }
  6. before do
  7. allow(controller).to receive(:doorkeeper_token) { token }
  8. end
  9. describe 'POST #create' do
  10. describe 'with paperclip errors' do
  11. context 'when imagemagick cant identify the file type' do
  12. before do
  13. expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Errors::NotIdentifiedByImageMagickError)
  14. post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') }
  15. end
  16. it 'returns http 422' do
  17. expect(response).to have_http_status(:unprocessable_entity)
  18. end
  19. end
  20. context 'when there is a generic error' do
  21. before do
  22. expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Error)
  23. post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') }
  24. end
  25. it 'returns http 422' do
  26. expect(response).to have_http_status(:error)
  27. end
  28. end
  29. end
  30. context 'image/jpeg' do
  31. before do
  32. post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') }
  33. end
  34. it 'returns http success' do
  35. expect(response).to have_http_status(:success)
  36. end
  37. it 'creates a media attachment' do
  38. expect(MediaAttachment.first).to_not be_nil
  39. end
  40. it 'uploads a file' do
  41. expect(MediaAttachment.first).to have_attached_file(:file)
  42. end
  43. it 'returns media ID in JSON' do
  44. expect(body_as_json[:id]).to eq MediaAttachment.first.id.to_s
  45. end
  46. end
  47. context 'image/gif' do
  48. before do
  49. post :create, params: { file: fixture_file_upload('files/attachment.gif', 'image/gif') }
  50. end
  51. it 'returns http success' do
  52. expect(response).to have_http_status(:success)
  53. end
  54. it 'creates a media attachment' do
  55. expect(MediaAttachment.first).to_not be_nil
  56. end
  57. it 'uploads a file' do
  58. expect(MediaAttachment.first).to have_attached_file(:file)
  59. end
  60. it 'returns media ID in JSON' do
  61. expect(body_as_json[:id]).to eq MediaAttachment.first.id.to_s
  62. end
  63. end
  64. context 'video/webm' do
  65. before do
  66. post :create, params: { file: fixture_file_upload('files/attachment.webm', 'video/webm') }
  67. end
  68. xit 'returns http success' do
  69. expect(response).to have_http_status(:success)
  70. end
  71. xit 'creates a media attachment' do
  72. expect(MediaAttachment.first).to_not be_nil
  73. end
  74. xit 'uploads a file' do
  75. expect(MediaAttachment.first).to have_attached_file(:file)
  76. end
  77. xit 'returns media ID in JSON' do
  78. expect(body_as_json[:id]).to eq MediaAttachment.first.id.to_s
  79. end
  80. end
  81. end
  82. describe 'PUT #update' do
  83. context 'when somebody else\'s' do
  84. let(:media) { Fabricate(:media_attachment, status: nil) }
  85. it 'returns http not found' do
  86. put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
  87. expect(response).to have_http_status(:not_found)
  88. end
  89. end
  90. context 'when not attached to a status' do
  91. let(:media) { Fabricate(:media_attachment, status: nil, account: user.account) }
  92. it 'updates the description' do
  93. put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
  94. expect(media.reload.description).to eq 'Lorem ipsum!!!'
  95. end
  96. end
  97. context 'when attached to a status' do
  98. let(:media) { Fabricate(:media_attachment, status: Fabricate(:status), account: user.account) }
  99. it 'returns http not found' do
  100. put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
  101. expect(response).to have_http_status(:not_found)
  102. end
  103. end
  104. end
  105. end