FIX: return valid JSON when a post in enqueued (#20214)

When a post is created using the API and goes into the review queue, we
would return a 'null' string in the response which isn't valid JSON.

Internal ref: /t/92419

Co-authored-by: Leonardo Mosquera <ldmosquera@gmail.com>
This commit is contained in:
Régis Hanol 2023-02-08 14:27:26 +01:00 committed by GitHub
parent dc43de6f03
commit 7eb6223b04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 19 deletions

View File

@ -183,26 +183,25 @@ class PostsController < ApplicationController
end
def create
@manager_params = create_params
@manager_params[:first_post_checks] = !is_api?
@manager_params[:advance_draft] = !is_api?
manager_params = create_params
manager_params[:first_post_checks] = !is_api?
manager_params[:advance_draft] = !is_api?
manager = NewPostManager.new(current_user, @manager_params)
manager = NewPostManager.new(current_user, manager_params)
if is_api?
memoized_payload =
DistributedMemoizer.memoize(signature_for(@manager_params), 120) do
result = manager.perform
MultiJson.dump(serialize_data(result, NewPostResultSerializer, root: false))
end
json =
if is_api?
memoized_payload =
DistributedMemoizer.memoize(signature_for(manager_params), 120) do
MultiJson.dump(serialize_data(manager.perform, NewPostResultSerializer, root: false))
end
parsed_payload = JSON.parse(memoized_payload)
backwards_compatible_json(parsed_payload, parsed_payload["success"])
else
result = manager.perform
json = serialize_data(result, NewPostResultSerializer, root: false)
backwards_compatible_json(json, result.success?)
end
JSON.parse(memoized_payload)
else
serialize_data(manager.perform, NewPostResultSerializer, root: false)
end
backwards_compatible_json(json)
end
def update
@ -725,9 +724,13 @@ class PostsController < ApplicationController
# We can't break the API for making posts. The new, queue supporting API
# doesn't return the post as the root JSON object, but as a nested object.
# If a param is present it uses that result structure.
def backwards_compatible_json(json_obj, success)
def backwards_compatible_json(json_obj)
json_obj.symbolize_keys!
if params[:nested_post].blank? && json_obj[:errors].blank? && json_obj[:action] != :enqueued
success = json_obj[:success]
if params[:nested_post].blank? && json_obj[:errors].blank? &&
json_obj[:action].to_s != "enqueued"
json_obj = json_obj[:post]
end

View File

@ -865,6 +865,25 @@ RSpec.describe PostsController do
expect(response.body).to eq(original)
end
it "returns a valid JSON response when the post is enqueued" do
SiteSetting.approve_unless_trust_level = 4
master_key = Fabricate(:api_key).key
post "/posts.json",
params: {
raw: "this is test post #{SecureRandom.alphanumeric}",
title: "tthis is a test title #{SecureRandom.alphanumeric}",
},
headers: {
HTTP_API_USERNAME: user.username,
HTTP_API_KEY: master_key,
}
expect(response.status).to eq(200)
expect(response.parsed_body["action"]).to eq("enqueued")
end
it "allows to create posts in import_mode" do
Jobs.run_immediately!
NotificationEmailer.enable