From 83af9b4869c4d4b6108d6bfd37dd1aa770c8cb7a Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Mon, 4 Jul 2022 12:17:20 +0100 Subject: [PATCH 1/4] Convern function to preserve fields qstring --- app/controllers/concerns/fieldable.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/concerns/fieldable.rb b/app/controllers/concerns/fieldable.rb index 5e1cee97d..9d07caa7a 100644 --- a/app/controllers/concerns/fieldable.rb +++ b/app/controllers/concerns/fieldable.rb @@ -11,5 +11,13 @@ def fields_from_params(params) fields.each { |k, v| fields[k] = v.to_s.split(",") } fields end + + def fields_hash_from_params(params) + fields = params.to_unsafe_h.dig(:fields) + return nil unless fields.is_a?(Hash) + + fields.each { |k, v| fields[k] = v } + fields + end end end From 67e072199b777749575abd43e700546690c47fa5 Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Mon, 4 Jul 2022 12:18:16 +0100 Subject: [PATCH 2/4] add fields to the pagination links --- app/controllers/clients_controller.rb | 1 + app/controllers/datacite_dois_controller.rb | 1 + app/controllers/organizations_controller.rb | 1 + app/controllers/providers_controller.rb | 1 + app/controllers/repositories_controller.rb | 1 + 5 files changed, 5 insertions(+) diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index fb1e46525..e6f63a912 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -111,6 +111,7 @@ def index "page[number]" => page[:number] + 1, "page[size]" => page[:size], sort: params[:sort], + fields: fields_hash_from_params(params) }.compact. to_query end, diff --git a/app/controllers/datacite_dois_controller.rb b/app/controllers/datacite_dois_controller.rb index 1710c5f82..466f8a191 100644 --- a/app/controllers/datacite_dois_controller.rb +++ b/app/controllers/datacite_dois_controller.rb @@ -340,6 +340,7 @@ def index page[:number] + 1 end, "page[size]" => page[:size], + fields: fields_hash_from_params(params) }.compact. to_query end, diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 7adbea8df..a3359dd15 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -102,6 +102,7 @@ def index "page[number]" => page[:number] + 1, "page[size]" => page[:size], sort: sort, + fields: fields_hash_from_params(params) }.compact. to_query end, diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index ee2e56fa3..993dc5355 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -125,6 +125,7 @@ def index "page[number]" => page[:number] + 1, "page[size]" => page[:size], sort: sort, + fields: fields_hash_from_params(params) }.compact. to_query end, diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 4d6b45a7c..a1b1d5570 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -114,6 +114,7 @@ def index "page[number]" => page[:number] + 1, "page[size]" => page[:size], sort: params[:sort], + fields: fields_hash_from_params(params) }.compact. to_query end, From e2331f248f758516143262f37eda336c8307d773 Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Mon, 4 Jul 2022 12:18:30 +0100 Subject: [PATCH 3/4] tests for fieldable concern --- spec/concerns/fieldable_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 spec/concerns/fieldable_spec.rb diff --git a/spec/concerns/fieldable_spec.rb b/spec/concerns/fieldable_spec.rb new file mode 100644 index 000000000..148dce4cc --- /dev/null +++ b/spec/concerns/fieldable_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe "Dois", type: :controller do + subject { DataciteDoisController.new } + + it "no params" do + params = ActionController::Parameters.new + expect(subject.fields_from_params(params)).to be_nil + expect(subject.fields_hash_from_params(params)).to be_nil + end + + it "single value" do + params = ActionController::Parameters.new(fields: { dois: "id" }) + expect(subject.fields_from_params(params)).to eq({ dois: ["id"] }.with_indifferent_access) + expect(subject.fields_hash_from_params(params)).to eq({ dois: "id" }.with_indifferent_access) + end + + it "multiple values" do + params = ActionController::Parameters.new(fields: { dois: "id,subjects" }) + expect(subject.fields_from_params(params)).to eq({ dois: ["id", "subjects"] }.with_indifferent_access) + expect(subject.fields_hash_from_params(params)).to eq({ dois: "id,subjects" }.with_indifferent_access) + end +end From f334847985daa46ecabb6facd90881d52e10a79d Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Mon, 4 Jul 2022 12:18:44 +0100 Subject: [PATCH 4/4] fields filters spec tests --- spec/requests/datacite_dois_spec.rb | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/spec/requests/datacite_dois_spec.rb b/spec/requests/datacite_dois_spec.rb index 910497cae..d55f5a703 100644 --- a/spec/requests/datacite_dois_spec.rb +++ b/spec/requests/datacite_dois_spec.rb @@ -176,6 +176,53 @@ expect(doi.dig("relationships", "provider", "data", "id")).to eq(provider.symbol.downcase) end end + + it "applies field filters for a single filter" do + get "/dois?fields[dois]=id", nil, headers + + expect(last_response.status).to eq(200) + expect(json["data"].size).to eq(10) + expect(json.dig("meta", "total")).to eq(10) + json["data"].each do |doi| + expect(doi).to include("id") + expect(doi).to include("attributes") + expect(doi).to include("relationships") + expect(doi.dig("attributes")).to eq({}) + expect(doi.dig("relationships")).to eq({}) + end + end + + it "applies field filters for multiple filters" do + get "/dois?fields[dois]=id,subjects", nil, headers + + expect(last_response.status).to eq(200) + expect(json["data"].size).to eq(10) + expect(json.dig("meta", "total")).to eq(10) + json["data"].each do |doi| + expect(doi).to include("id") + expect(doi).to include("attributes") + expect(doi).to include("relationships") + expect(doi.dig("attributes")).to have_key("subjects") + expect(doi.dig("attributes")).to_not have_key("creators") + expect(doi.dig("relationships")).to eq({}) + end + end + + it "preserves field filters in pagination links" do + get "/dois?fields[dois]=id&page[size]=2&page[number]=1", nil, headers + + expect(last_response.status).to eq(200) + next_link_absolute = Addressable::URI.parse(json.dig("links", "next")) + next_link = next_link_absolute.path + "?" + next_link_absolute.query + expect(next_link).to eq("/dois?fields%5Bdois%5D=id&page%5Bnumber%5D=2&page%5Bsize%5D=2") + + get "/dois?fields[dois]=id,subjects&page[size]=2&page[number]=1", nil, headers + + expect(last_response.status).to eq(200) + next_link_absolute = Addressable::URI.parse(json.dig("links", "next")) + next_link = next_link_absolute.path + "?" + next_link_absolute.query + expect(next_link).to eq("/dois?fields%5Bdois%5D=id%2Csubjects&page%5Bnumber%5D=2&page%5Bsize%5D=2") + end end describe "GET /dois with query", elasticsearch: true do