Fix #4614: Counts endpoint responds with invalid JSON.

Caused by the search timing out and returning nil for the count. Nil got
serialized as the empty string instead of as null.
This commit is contained in:
evazion
2020-12-05 13:10:11 -06:00
parent cc781ba2b9
commit 9b48c98c61
4 changed files with 17 additions and 8 deletions

View File

@@ -1,9 +1,15 @@
class CountsController < ApplicationController class CountsController < ApplicationController
respond_to :xml, :json respond_to :html, :xml, :json
def posts def posts
estimate_count = params.fetch(:estimate_count, "true").truthy? estimate_count = params.fetch(:estimate_count, "true").truthy?
skip_cache = params.fetch(:skip_cache, "false").truthy? skip_cache = params.fetch(:skip_cache, "false").truthy?
@count = PostQueryBuilder.new(params[:tags], CurrentUser.user).normalized_query.fast_count(timeout: CurrentUser.statement_timeout, estimate_count: estimate_count, skip_cache: skip_cache) @count = PostQueryBuilder.new(params[:tags], CurrentUser.user).normalized_query.fast_count(timeout: CurrentUser.statement_timeout, estimate_count: estimate_count, skip_cache: skip_cache)
if request.format.xml?
respond_with({ posts: @count }, root: "counts")
else
respond_with({ counts: { posts: @count }})
end
end end
end end

View File

@@ -1 +0,0 @@
{"counts": {"posts": <%= @count %>}}

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<counts>
<posts>
<%= @count %>
</posts>
</counts>

View File

@@ -7,6 +7,16 @@ class CountsControllerTest < ActionDispatch::IntegrationTest
get posts_counts_path get posts_counts_path
assert_response :success assert_response :success
end end
should "render for json" do
get posts_counts_path(format: :json)
assert_response :success
end
should "render for xml" do
get posts_counts_path(format: :xml)
assert_response :success
end
end end
end end
end end