From 9b48c98c61f2557de6e055ec300c3a5c01320f24 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 5 Dec 2020 13:10:11 -0600 Subject: [PATCH] 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. --- app/controllers/counts_controller.rb | 8 +++++++- app/views/counts/posts.json.erb | 1 - app/views/counts/posts.xml.erb | 6 ------ test/functional/counts_controller_test.rb | 10 ++++++++++ 4 files changed, 17 insertions(+), 8 deletions(-) delete mode 100644 app/views/counts/posts.json.erb delete mode 100644 app/views/counts/posts.xml.erb diff --git a/app/controllers/counts_controller.rb b/app/controllers/counts_controller.rb index 35694b3ee..4baf35f2d 100644 --- a/app/controllers/counts_controller.rb +++ b/app/controllers/counts_controller.rb @@ -1,9 +1,15 @@ class CountsController < ApplicationController - respond_to :xml, :json + respond_to :html, :xml, :json def posts estimate_count = params.fetch(:estimate_count, "true").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) + + if request.format.xml? + respond_with({ posts: @count }, root: "counts") + else + respond_with({ counts: { posts: @count }}) + end end end diff --git a/app/views/counts/posts.json.erb b/app/views/counts/posts.json.erb deleted file mode 100644 index 7c68802cb..000000000 --- a/app/views/counts/posts.json.erb +++ /dev/null @@ -1 +0,0 @@ -{"counts": {"posts": <%= @count %>}} diff --git a/app/views/counts/posts.xml.erb b/app/views/counts/posts.xml.erb deleted file mode 100644 index 9ae07f67d..000000000 --- a/app/views/counts/posts.xml.erb +++ /dev/null @@ -1,6 +0,0 @@ - - - - <%= @count %> - - diff --git a/test/functional/counts_controller_test.rb b/test/functional/counts_controller_test.rb index 24c6dad03..b400521de 100644 --- a/test/functional/counts_controller_test.rb +++ b/test/functional/counts_controller_test.rb @@ -7,6 +7,16 @@ class CountsControllerTest < ActionDispatch::IntegrationTest get posts_counts_path assert_response :success 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