Skip to content

Commit

Permalink
Filter the attributes that display on the json views
Browse files Browse the repository at this point in the history
This prevents data from displaying through the APIs even though it
wasn't clear that it would be displayed.
Fixes #1899
  • Loading branch information
jcoyne committed Jun 7, 2018
1 parent 2455f95 commit 3b0d6c9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def show

respond_to do |format|
format.html { @search_context = setup_next_and_previous_documents }
format.json { render json: { response: { document: @document } } }
format.json
additional_export_formats(@document, format)
end
end
Expand Down
11 changes: 10 additions & 1 deletion app/views/catalog/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ end
json.data do
json.array! @presenter.documents do |document|
json.id document.id
json.attributes document
json.attributes do
doc_presenter = index_presenter(document)

index_fields(document).each do |field_name, field|
if should_render_index_field? document, field
json.set! field_name, doc_presenter.field_value(field_name)
end
end
end

json.links do
json.self polymorphic_url(url_for_document(document))
end
Expand Down
16 changes: 16 additions & 0 deletions app/views/catalog/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
json.links do
json.self polymorphic_url(@document)
end

json.data do
json.id @document.id
json.attributes do
doc_presenter = show_presenter(@document)

document_show_fields(@document).each do |field_name, field|
if should_render_show_field? @document, field
json.set! field_name, doc_presenter.field_value(field_name)
end
end
end
end
12 changes: 6 additions & 6 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@

it "gets the documents" do
expect(docs).to have(10).documents
expect(docs.first['attributes'].keys).to include(
"author_tsim", "format", "title_tsim", "id", "subject_ssim",
"language_ssim", "score", "timestamp")
expect(docs.first['attributes'].keys).to match_array(
%w[author_tsim format language_ssim lc_callnum_ssim published_ssim title_tsim])
expect(docs.first['links']['self']).to eq solr_document_url(id: docs.first['id'])
end

Expand Down Expand Up @@ -268,13 +267,14 @@
end

describe "with format :json" do
render_views
it "gets the feed" do
get :show, params: { id: doc_id, format: 'json' }
expect(response).to be_success
json = JSON.parse response.body
expect(json["response"]["document"].keys).to include(
"author_tsim", "format", "title_tsim", "id", "subject_ssim",
"language_ssim", "timestamp")
expect(json["data"]["attributes"].keys).to match_array(
%w[author_tsim format isbn_ssim language_ssim lc_callnum_ssim
published_ssim subtitle_tsim title_tsim url_suppl_ssim])
end
end

Expand Down
17 changes: 13 additions & 4 deletions spec/views/catalog/index.json.jbuilder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# frozen_string_literal: true
RSpec.describe "catalog/index.json" do
let(:response) { instance_double(Blacklight::Solr::Response, documents: docs, prev_page: nil, next_page: 2, total_pages: 3) }
let(:docs) { [SolrDocument.new(id: '123', title_tsim: 'Book1'), SolrDocument.new(id: '456', title_tsim: 'Book2')] }
let(:docs) do
[
SolrDocument.new(id: '123', title_tsim: 'Book1', author_tsim: 'Julie'),
SolrDocument.new(id: '456', title_tsim: 'Book2', author_tsim: 'Rosie')
]
end
let(:facets) { double("facets") }
let(:config) { Blacklight::Configuration.new }
let(:config) do
Blacklight::Configuration.new do |config|
config.add_index_field 'title_tsim', label: 'Title:'
end
end
let(:presenter) { Blacklight::JsonPresenter.new(response, facets, config) }

let(:hash) do
Expand Down Expand Up @@ -44,12 +53,12 @@
expect(hash).to include(data: [
{
id: '123',
attributes: { 'id' => '123', 'title_tsim' => 'Book1' },
attributes: { 'title_tsim' => 'Book1' },
links: { self: 'http://test.host/catalog/123' }
},
{
id: '456',
attributes: { 'id' => '456', 'title_tsim' => 'Book2' },
attributes: { 'title_tsim' => 'Book2' },
links: { self: 'http://test.host/catalog/456' }
},
])
Expand Down
31 changes: 31 additions & 0 deletions spec/views/catalog/show.json.jbuilder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

RSpec.describe "catalog/show.json" do
let(:document) do
SolrDocument.new(id: '123', title_tsim: 'Book1', author_tsim: 'Julie')
end
let(:config) do
Blacklight::Configuration.new do |config|
config.add_show_field 'title_tsim', label: 'Title:'
end
end

let(:hash) do
render template: "catalog/show.json", format: :json
JSON.parse(rendered).with_indifferent_access
end

before do
allow(view).to receive(:blacklight_config).and_return(config)
assign :document, document
end

it "includes document attributes" do
expect(hash).to include(data:
{
id: '123',
attributes: { 'title_tsim' => 'Book1' }
}
)
end
end

0 comments on commit 3b0d6c9

Please sign in to comment.