Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

fix: review round 1 of #81, keep a JSON false as no comment body

Guido Boyke58708747c725
diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb
index 6442239..af7971e 100644
--- a/app/controllers/api/v1/posts_controller.rb
+++ b/app/controllers/api/v1/posts_controller.rb
@@ -68,8 +68,11 @@ module Api
raise ActionController::ParameterMissing, :post unless input.is_a?(ActionController::Parameters)
permitted = input.permit(:body, :format, :is_private, :visible_to_user_id)
- fields = { body: permitted[:body], is_private: permitted[:is_private],
- visible_to_user_id: permitted[:visible_to_user_id] }
+ body = permitted[:body]
+ # A JSON false was no body before R3a (the old gate was body.present?),
+ # while true and numbers were their text; the rule would write "false".
+ body = nil if body == false
+ fields = { body: body, is_private: permitted[:is_private], visible_to_user_id: permitted[:visible_to_user_id] }
fields[:format] = input[:format] if input.key?(:format)
fields
end
diff --git a/spec/requests/api/v1/post_comment_format_spec.rb b/spec/requests/api/v1/post_comment_format_spec.rb
index 65af592..21c89c4 100644
--- a/spec/requests/api/v1/post_comment_format_spec.rb
+++ b/spec/requests/api/v1/post_comment_format_spec.rb
@@ -150,6 +150,13 @@ RSpec.describe 'API V1 comment format', :ci_guard, type: :request do
expect(body.dig('data', 'body_html')).to eq('true')
end
+ # The old gate was body.present?: false was no body, while true and numbers
+ # were their text.
+ it 'takes a JSON false as no body, as before' do
+ expect { comment_json({ body: false }) }.not_to(change { posts_count })
+ expect(response.body).to eq('{"error":"validation_failed","code":"invalid_input","errors":["Content can\'t be blank"]}')
+ end
+
it 'leaves an empty body to the model, which refuses it with its own message' do
expect { comment(body: '') }.not_to(change { posts_count })
expect(response).to have_http_status(:unprocessable_content)
@@ -212,6 +219,11 @@ RSpec.describe 'API V1 comment format', :ci_guard, type: :request do
expect(body).to eq(refusal(:body_empty))
end
+ it 'refuses a JSON false as a plain body as empty' do
+ expect { comment_json({ format: 'plain', body: false }) }.not_to(change { posts_count })
+ expect(body).to eq(refusal(:body_empty))
+ end
+
it 'answers the refusal in the token owner language' do
user.update!(language_preference: 'de')
comment(format: 'markdown', body: 'x')
@@ -236,6 +248,16 @@ RSpec.describe 'API V1 comment format', :ci_guard, type: :request do
expect(response).to have_http_status(:bad_request)
end
+ # An escaped lone surrogate is no JSON the parser takes: the 400 of a body
+ # that is no JSON, which BaseController answers with code.
+ it 'answers an escaped lone surrogate in JSON as a body that is no JSON' do
+ raw = "{\"post\":{\"format\":\"plain\",\"body\":\"a#{92.chr}ud800b\"}}"
+ expect { post "/api/v1/tickets/#{ticket.id}/posts", params: raw, headers: headers(json: true) }
+ .not_to(change { posts_count })
+ expect(response).to have_http_status(:bad_request)
+ expect(body).to include('error' => 'bad_request', 'code' => 'invalid_input')
+ end
+
it 'answers a post that is no object like a missing one, never a crash' do
post "/api/v1/tickets/#{ticket.id}/posts", params: {}, headers: headers
missing = [response.status, response.body]