Skip to content

Commit

Permalink
Fix crash when reading RO-Crates with a single value (instead of arra…
Browse files Browse the repository at this point in the history
…y) at `hasPart`
  • Loading branch information
fbacall committed Jan 29, 2025
1 parent 58b6585 commit e891419
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ro_crate/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ def self.initialize_crate(entity_hash, source, crate_class: ROCrate::Crate, cont
# @param entity_hash [Hash] A Hash containing all the entities in the @graph, mapped by their @id.
# @return [Array<ROCrate::File, ROCrate::Directory>] The extracted DataEntity objects.
def self.extract_data_entities(crate, source, entity_hash)
(crate.raw_properties['hasPart'] || []).map do |ref|
parts = crate.raw_properties['hasPart'] || []
parts = [parts] unless parts.is_a?(Array)
parts.map do |ref|
entity_props = entity_hash.delete(ref['@id'])
next unless entity_props
entity_class = ROCrate::DataEntity.specialize(entity_props)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/singleton-haspart/a_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
57 changes: 57 additions & 0 deletions test/fixtures/singleton-haspart/ro-crate-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"@context": [
"https://w3id.org/ro/crate/1.1/context",
"https://w3id.org/ro/terms/workflow-run/context"
],
"@graph": [
{
"@id": "./",
"@type": "Dataset",
"datePublished": "2025-01-28T02:44:51.523Z",
"hasPart": {
"@id": "a_file"
},
"dateCreated": "2025-01-28T02:45:05.906Z",
"dateModified": "2025-01-28T02:44:51.523Z",
"description": "Something",
"license": {
"@id": "https://spdx.org/licenses/MIT"
},
"mainEntity": {
"@id": "a_file"
},
"name": "An RO-Crate containing just a single item"
},
{
"@id": "ro-crate-metadata.json",
"@type": "CreativeWork",
"conformsTo": [
{
"@id": "https://w3id.org/ro/crate/1.1"
},
{
"@id": "https://w3id.org/workflowhub/workflow-ro-crate/1.0"
}
],
"about": {
"@id": "./"
}
},
{
"@id": "a_file",
"@type": [
"File",
"ComputationalWorkflow",
"SoftwareSourceCode"
],
"contentSize": 4,
"description": "A workflow",
"encodingFormat": "text/plain",
"name": "a_file",
"programmingLanguage": {
"@id": "https://example.com/workflow_format"
}
}
]
}

8 changes: 8 additions & 0 deletions test/reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,14 @@ class ReaderTest < Test::Unit::TestCase
assert_equal 'At the root', crate.name
end

test 'reads crate with singleton hasPart' do
crate = ROCrate::Reader.read(fixture_file('singleton-haspart').path)

data = crate.data_entities
assert_equal 1, data.length
assert_equal 'a_file', data.first.name
end

private

def check_exception(exception_class)
Expand Down

0 comments on commit e891419

Please sign in to comment.