diff --git a/app/controllers/datacite_dois_controller.rb b/app/controllers/datacite_dois_controller.rb index 9162a02c6..74639fbec 100644 --- a/app/controllers/datacite_dois_controller.rb +++ b/app/controllers/datacite_dois_controller.rb @@ -253,12 +253,14 @@ def index } aggregations = response.aggregations - facets = facets_to_facet_methods.map do |facet, method| - if aggregations.dig(facet) - buckets = facets_to_bucket_path.dig(facet) ? aggregations.dig(facet, *facets_to_bucket_path[facet]) : aggregations.dig(facet).buckets - [facet.to_s.camelize(:lower), send(method, buckets)] - end - end.compact.to_h + facets = total == 0 ? {} : + facets_to_facet_methods.map do |facet, method| + if aggregations.dig(facet) + buckets = facets_to_bucket_path.dig(facet) ? aggregations.dig(facet, *facets_to_bucket_path[facet]) : aggregations.dig(facet).buckets + [facet.to_s.camelize(:lower), send(method, buckets)] + end + end.compact.to_h + respond_to do |format| format.json do options = {} diff --git a/spec/requests/datacite_dois/datacite_dois_spec.rb b/spec/requests/datacite_dois/datacite_dois_spec.rb index ee79d372f..bec0cf9d0 100755 --- a/spec/requests/datacite_dois/datacite_dois_spec.rb +++ b/spec/requests/datacite_dois/datacite_dois_spec.rb @@ -34,6 +34,12 @@ def clear_doi_index "downloads" ] +DEFAULT_META_FIELDS = [ + "total", + "totalPages", + "page", +] + describe DataciteDoisController, type: :request, vcr: true do let(:admin) { create(:provider, symbol: "ADMIN") } let(:admin_bearer) { Client.generate_token(role_id: "staff_admin", uid: admin.symbol, password: admin.password) } @@ -265,7 +271,7 @@ def clear_doi_index expect(json.dig("meta", "total")).to eq(10) expect(json.dig("meta").length).to eq(DEFAULT_DOIS_FACETS.length + 3) - expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + ["total", "totalPages", "page"]) + expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + DEFAULT_META_FIELDS) end it "returns default facets when disable-facets is set to false" do @@ -276,7 +282,7 @@ def clear_doi_index expect(json.dig("meta", "total")).to eq(10) expect(json.dig("meta").length).to eq(DEFAULT_DOIS_FACETS.length + 3) - expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + ["total", "totalPages", "page"]) + expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + DEFAULT_META_FIELDS) end it "returns no facets when disable-facets is set to true" do @@ -319,6 +325,16 @@ def clear_doi_index expect(json.dig("meta", "madeUpFacet")).to eq(nil) expect(json.dig("meta", "made_up_facet")).to eq(nil) end + + it "returns no facets when the result set is empty" do + get "/dois?query=creators.name:foo", nil, headers + + expect(last_response.status).to eq(200) + expect(json["data"].size).to eq(0) + expect(json.dig("meta", "total")).to eq(0) + expect(json.dig("meta").length).to eq(3) + expect(json.dig("meta").keys).to match_array(DEFAULT_META_FIELDS) + end end describe "GET /dois with nil publisher values", elsasticsearch: true, prefix_pool_size: 1 do