Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove alternateIdentifiers from XML when alternateIdentifiers parameter is blank or nil #1308

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -757,16 +757,23 @@ def safe_params
# alternateIdentifiers as alias for identifiers
# easier before strong_parameters are checked
if params.dig(:data, :attributes).present? &&
params.dig(:data, :attributes, :identifiers).blank? &&
!params.dig(:data, :attributes, :alternateIdentifiers).blank?
!params.dig(:data, :attributes)&.key?(:identifiers) &&
params.dig(:data, :attributes)&.key?(:alternateIdentifiers)

alternate_identifiers = params.dig(:data, :attributes, :alternateIdentifiers)

params[:data][:attributes][:identifiers] =
Array.wrap(params.dig(:data, :attributes, :alternateIdentifiers)).
map do |a|
{
identifier: a[:alternateIdentifier],
identifierType: a[:alternateIdentifierType],
}
end
alternate_identifiers.nil? ? nil :
Array.wrap(alternate_identifiers).map do |a|
if a.respond_to?(:fetch)
{
identifier: a.fetch(:alternateIdentifier),
identifierType: a.fetch(:alternateIdentifierType),
}
else
a
end
end
end

ParamsSanitizer.sanitize_nameIdentifiers(params[:creators])
Expand Down
159 changes: 159 additions & 0 deletions spec/requests/datacite_dois/patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,163 @@
expect(json.dig("data", "attributes", "subjects")).to eq([])
end
end

context "when a doi has alternateIdentifier/identifier values" do
let(:valid_attributes) do
{
"data" => {
"type" => "dois",
"attributes" => {
"alternateIdentifiers" => nil,
},
},
}
end

it "the xml and doi record contain the values" do
xml = Maremma.from_xml(doi.xml).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).not_to eq(nil)
expect(doi.identifiers).not_to eq(nil)
end

it "the values are removed when nil values are sent in json" do
patch "/dois/#{doi.doi}", valid_attributes, headers

xml = Maremma.from_xml(Base64.decode64(json.dig("data", "attributes", "xml"))).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).to eq(nil)
expect(json.dig("data", "attributes", "identifiers")).to eq([])
expect(json.dig("data", "attributes", "alternateIdentifiers")).to eq([])
end
end

context "when a doi has alternateIdentifier/identifier values" do
let(:valid_attributes) do
{
"data" => {
"type" => "dois",
"attributes" => {
"alternateIdentifiers" => [],
},
},
}
end

it "the xml and doi record contain the values" do
xml = Maremma.from_xml(doi.xml).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).not_to eq(nil)
expect(doi.identifiers).not_to eq(nil)
end

it "the values are removed when blank values are sent in json" do
patch "/dois/#{doi.doi}", valid_attributes, headers

xml = Maremma.from_xml(Base64.decode64(json.dig("data", "attributes", "xml"))).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).to eq(nil)
expect(json.dig("data", "attributes", "identifiers")).to eq([])
expect(json.dig("data", "attributes", "alternateIdentifiers")).to eq([])
end
end

context "when a doi has alternateIdentifier/identifier values" do
let(:valid_attributes) do
{
"data" => {
"type" => "dois",
"attributes" => {
"alternateIdentifiers" => [
{
"alternateIdentifier" => "identifier",
"alternateIdentifierType" => "identifierType",
},
{
"alternateIdentifier" => "identifier_2",
"alternateIdentifierType" => "identifierType_2",
},
],
},
},
}
end

it "the xml and doi record contain the values" do
xml = Maremma.from_xml(doi.xml).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).not_to eq(nil)
expect(doi.identifiers).not_to eq(nil)
end

it "the values are changed when new values are sent in json" do
patch "/dois/#{doi.doi}", valid_attributes, headers

xml = Maremma.from_xml(Base64.decode64(json.dig("data", "attributes", "xml"))).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).to eq(
"alternateIdentifier" =>
[
{ "__content__" => "identifier", "alternateIdentifierType" => "identifierType" },
{ "__content__" => "identifier_2", "alternateIdentifierType" => "identifierType_2" }
]
)
expect(json.dig("data", "attributes", "identifiers")).to eq([
{
"identifier" => "identifier",
"identifierType" => "identifierType"
},
{
"identifier" => "identifier_2",
"identifierType" => "identifierType_2"
}
])
expect(json.dig("data", "attributes", "alternateIdentifiers")).to eq([
{
"alternateIdentifier" => "identifier",
"alternateIdentifierType" => "identifierType"
},
{
"alternateIdentifier" => "identifier_2",
"alternateIdentifierType" => "identifierType_2"
}
])
end

context "when a doi has alternateIdentifier/identifier values" do
let(:valid_attributes) do
{
"data" => {
"type" => "dois",
"attributes" => {
"subjects" => nil,
},
},
}
end

it "the xml and doi record contain the values" do
xml = Maremma.from_xml(doi.xml).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).not_to eq(nil)
expect(doi.identifiers).not_to eq(nil)
end

it "the values are the same when no values are sent in json" do
patch "/dois/#{doi.doi}", valid_attributes, headers

xml = Maremma.from_xml(Base64.decode64(json.dig("data", "attributes", "xml"))).fetch("resource", {})
expect(xml.dig("alternateIdentifiers")).to eq(
"alternateIdentifier" => {
"__content__" => "pk-1234", "alternateIdentifierType" => "publisher ID"
}
)
expect(json.dig("data", "attributes", "identifiers")).to eq([
{
"identifier" => "pk-1234",
"identifierType" => "publisher ID"
},
])
expect(json.dig("data", "attributes", "alternateIdentifiers")).to eq([
{
"alternateIdentifier" => "pk-1234",
"alternateIdentifierType" => "publisher ID"
},
])
end
end
end
end
Loading