Skip to content

Commit

Permalink
Safety when converting alternateIdentifiers to identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
codycooperross committed Jan 13, 2025
1 parent 59364ba commit 6cc1950
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
32 changes: 18 additions & 14 deletions app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -757,20 +757,24 @@ def safe_params
# alternateIdentifiers as alias for identifiers
# easier before strong_parameters are checked
if params.dig(:data, :attributes).present? &&
!params.dig(:data, :attributes)&.key?(:identifiers) &&
params.dig(:data, :attributes)&.key?(:alternateIdentifiers)

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

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

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

params[:data][:attributes][:identifiers] =
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])
ParamsSanitizer.sanitize_nameIdentifiers(params[:contributors])
Expand Down
52 changes: 47 additions & 5 deletions spec/requests/datacite_dois/patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -870,14 +870,14 @@
{ "__content__" => "identifier_2", "alternateIdentifierType" => "identifierType_2" }
]
)
expect(json.dig("data", "attributes", "alternateIdentifiers")).to eq([
expect(json.dig("data", "attributes", "identifiers")).to eq([
{
"alternateIdentifier" => "identifier",
"alternateIdentifierType" => "identifierType"
"identifier" => "identifier",
"identifierType" => "identifierType"
},
{
"alternateIdentifier" => "identifier_2",
"alternateIdentifierType" => "identifierType_2"
"identifier" => "identifier_2",
"identifierType" => "identifierType_2"
}
])
expect(json.dig("data", "attributes", "alternateIdentifiers")).to eq([
Expand All @@ -891,5 +891,47 @@
}
])
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

0 comments on commit 6cc1950

Please sign in to comment.