diff --git a/lib/ro_crate/reader.rb b/lib/ro_crate/reader.rb index ab6b43a..6ec034c 100644 --- a/lib/ro_crate/reader.rb +++ b/lib/ro_crate/reader.rb @@ -233,13 +233,25 @@ def self.extract_contextual_entities(crate, entity_hash) entity_hash.each do |id, entity_props| entity_class = ROCrate::ContextualEntity.specialize(entity_props) - entity = entity_class.new(crate, id, entity_props) + entity = entity_class.new(crate, decode_local_id(id), entity_props) entities << entity end entities end + ## + # Decode a local (non-absolute) ID read from the metadata, so that it is not encoded a second time when it is + # formatted on assignment to an entity. Absolute IDs are returned unchanged. + # @param id [String] The ID as it appears in the metadata. + # @return [String] The decoded ID. + def self.decode_local_id(id) + uri = URI(id) rescue nil + return id if uri&.absolute? + + Addressable::URI.unencode_component(id) + end + ## # Create a DataEntity of the given class. # @param crate [Crate] The RO-Crate being read. @@ -250,7 +262,7 @@ def self.extract_contextual_entities(crate, entity_hash) def self.create_data_entity(crate, entity_class, source, entity_props) id = entity_props.delete('@id') raise ROCrate::ReadException, "Data Entity missing '@id': #{entity_props.inspect}" unless id - decoded_id = URI.decode_www_form_component(id) + decoded_id = decode_local_id(id) path = nil uri = URI(id) rescue nil if uri&.absolute? diff --git a/test/reader_test.rb b/test/reader_test.rb index 5ca5dde..675fd8e 100644 --- a/test/reader_test.rb +++ b/test/reader_test.rb @@ -167,6 +167,49 @@ class ReaderTest < Test::Unit::TestCase end end + test 'reading does not double-encode encoded contextual entity IDs' do + crate = ROCrate::Crate.new + jane = crate.add_person('Jane Bloggs', name: 'Jane Bloggs') + team = crate.add_contextual_entity(ROCrate::ContextualEntity.new(crate, 'C++ Team', name: 'C++ Team')) + crate.author = [jane, team] + assert_equal '#Jane%20Bloggs', jane.id + assert_equal '#C++%20Team', team.id + + # Write/Read the crate 3 times to ensure! + 3.times do + Tempfile.create do |file| + ROCrate::Writer.new(crate).write_zip(file) + crate = ROCrate::Reader.read_zip(file) + end + end + + ids = crate.contextual_entities.map(&:id) + assert_includes ids, '#Jane%20Bloggs' + assert_includes ids, '#C++%20Team' + assert_equal ['#Jane%20Bloggs', '#C++%20Team'], crate['author'].map { |ref| ref['@id'] } + assert_equal ['Jane Bloggs', 'C++ Team'], crate.author.map { |a| a['name'] } + end + + test 'reading preserves plus signs in data entity IDs' do + crate = ROCrate::Crate.new + file = crate.add_file(StringIO.new('notes'), 'C++ notes.txt') + assert_equal 'C++%20notes.txt', file.id + + # Write/Read the crate 3 times to ensure! + 3.times do + Tempfile.create do |f| + ROCrate::Writer.new(crate).write_zip(f) + crate = ROCrate::Reader.read_zip(f) + end + end + + file = crate.dereference('C++%20notes.txt') + assert file + assert_equal 'C++%20notes.txt', file.id + assert_equal ['C++%20notes.txt'], crate['hasPart'].map { |ref| ref['@id'] } + assert_equal 'notes', file.source.read + end + test 'can read a 1.1 spec crate' do stub_request(:get, "http://example.com/external_ref.txt").to_return(status: 200, body: 'file contents')