From aa57df3489d7170680280e3b89b90c888f4574de Mon Sep 17 00:00:00 2001 From: kjgarza Date: Tue, 14 Apr 2020 15:36:18 +0200 Subject: [PATCH] creating a doi with the mds overwrites schemaVersion resolves https://github.com/datacite/bracco/issues/225 --- app/controllers/dois_controller.rb | 10 ++++-- spec/requests/dois_spec.rb | 49 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 4702bdbf6..f2be8415c 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -355,7 +355,11 @@ def update @doi.assign_attributes(safe_params.slice(:client_id)) else authorize! :update, @doi - @doi.assign_attributes(safe_params.except(:doi, :client_id)) + if safe_params[:schema_version].blank? + @doi.assign_attributes(safe_params.except(:doi, :client_id).merge(schema_version: @doi[:schema_version] || LAST_SCHEMA_VERSION)) + else + @doi.assign_attributes(safe_params.except(:doi, :client_id)) + end end else doi_id = validate_doi(params[:id]) @@ -624,7 +628,7 @@ def safe_params meta = xml.present? ? parse_xml(xml, doi: p[:doi]) : {} p[:schemaVersion] = METADATA_FORMATS.include?(meta["from"]) ? LAST_SCHEMA_VERSION : p[:schemaVersion] xml = meta["string"] - + # if metadata for DOIs from other registration agencies are not found fail ActiveRecord::RecordNotFound if meta["state"] == "not_found" @@ -663,7 +667,7 @@ def safe_params p[:version_info] = p[:version] || meta["version_info"] if p.has_key?(:version) || meta["version_info"].present? # only update landing_page info if something is received via API to not overwrite existing data p.merge!(landing_page: p[:landingPage]) if p[:landingPage].present? - + p.merge( regenerate: p[:regenerate] || regenerate ).except( diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index f536e11ef..1f2d418c8 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -2813,6 +2813,55 @@ # end end + context 'mds doi' do + let(:xml) { Base64.strict_encode64(file_fixture('datacite_schema_3.xml').read) } + let(:valid_attributes) do + { + "data" => { + "type" => "dois", + "attributes" => { + "url" => "http://www.bl.uk/pdf/patspec.pdf", + "should_validate" => "true", + "source" => "mds", + "event" => "publish" + } + } + } + end + + let(:update_attributes) do + { + "data" => { + "type" => "dois", + "attributes" => { + "doi" => "10.14454/10703", + "should_validate" => "true", + "xml" => xml, + "source" => "mds", + "event" => "show" + } + } + } + end + + + it 'add metadata' do + puts "####FIRST ROUND" + put "/dois/10.14454/10703", update_attributes, headers + + puts json + expect(json.dig('data', 'attributes', 'doi')).to eq("10.14454/10703") + expect(json.dig('data', 'attributes', 'schemaVersion')).to eq("http://datacite.org/schema/kernel-3") + + puts "####SECOND ROUND" + put '/dois/10.14454/10703', valid_attributes, headers + + expect(json.dig('data', 'attributes', 'doi')).to eq("10.14454/10703") + expect(json.dig('data', 'attributes', 'schemaVersion')).to eq("http://datacite.org/schema/kernel-3") + expect(json.dig('data', 'attributes', 'titles')).to eq([{"title"=>"Data from: A new malaria agent in African hominids."}]) + end + end + context 'update rightsList' do let(:rights_list) { [{ "rights" => "Creative Commons Attribution 3.0", "rightsUri" => "http://creativecommons.org/licenses/by/3.0/", "lang" => "en"}] } let(:update_attributes) do