Skip to content

Commit e891419

Browse files
committed
Fix crash when reading RO-Crates with a single value (instead of array) at hasPart
1 parent 58b6585 commit e891419

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

lib/ro_crate/reader.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ def self.initialize_crate(entity_hash, source, crate_class: ROCrate::Crate, cont
204204
# @param entity_hash [Hash] A Hash containing all the entities in the @graph, mapped by their @id.
205205
# @return [Array<ROCrate::File, ROCrate::Directory>] The extracted DataEntity objects.
206206
def self.extract_data_entities(crate, source, entity_hash)
207-
(crate.raw_properties['hasPart'] || []).map do |ref|
207+
parts = crate.raw_properties['hasPart'] || []
208+
parts = [parts] unless parts.is_a?(Array)
209+
parts.map do |ref|
208210
entity_props = entity_hash.delete(ref['@id'])
209211
next unless entity_props
210212
entity_class = ROCrate::DataEntity.specialize(entity_props)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"@context": [
3+
"https://w3id.org/ro/crate/1.1/context",
4+
"https://w3id.org/ro/terms/workflow-run/context"
5+
],
6+
"@graph": [
7+
{
8+
"@id": "./",
9+
"@type": "Dataset",
10+
"datePublished": "2025-01-28T02:44:51.523Z",
11+
"hasPart": {
12+
"@id": "a_file"
13+
},
14+
"dateCreated": "2025-01-28T02:45:05.906Z",
15+
"dateModified": "2025-01-28T02:44:51.523Z",
16+
"description": "Something",
17+
"license": {
18+
"@id": "https://spdx.org/licenses/MIT"
19+
},
20+
"mainEntity": {
21+
"@id": "a_file"
22+
},
23+
"name": "An RO-Crate containing just a single item"
24+
},
25+
{
26+
"@id": "ro-crate-metadata.json",
27+
"@type": "CreativeWork",
28+
"conformsTo": [
29+
{
30+
"@id": "https://w3id.org/ro/crate/1.1"
31+
},
32+
{
33+
"@id": "https://w3id.org/workflowhub/workflow-ro-crate/1.0"
34+
}
35+
],
36+
"about": {
37+
"@id": "./"
38+
}
39+
},
40+
{
41+
"@id": "a_file",
42+
"@type": [
43+
"File",
44+
"ComputationalWorkflow",
45+
"SoftwareSourceCode"
46+
],
47+
"contentSize": 4,
48+
"description": "A workflow",
49+
"encodingFormat": "text/plain",
50+
"name": "a_file",
51+
"programmingLanguage": {
52+
"@id": "https://example.com/workflow_format"
53+
}
54+
}
55+
]
56+
}
57+

test/reader_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ class ReaderTest < Test::Unit::TestCase
373373
assert_equal 'At the root', crate.name
374374
end
375375

376+
test 'reads crate with singleton hasPart' do
377+
crate = ROCrate::Reader.read(fixture_file('singleton-haspart').path)
378+
379+
data = crate.data_entities
380+
assert_equal 1, data.length
381+
assert_equal 'a_file', data.first.name
382+
end
383+
376384
private
377385

378386
def check_exception(exception_class)

0 commit comments

Comments
 (0)