Skip to content

Commit 6af9320

Browse files
authored
Fix '--make-template' for union types (#1760)
1 parent 14f402f commit 6af9320

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

cwltool/main.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,15 @@ def generate_example_input(
195195
else:
196196
comment = "optional"
197197
else:
198-
example = CommentedSeq()
199-
for index, entry in enumerate(inptype):
198+
example, comment = generate_example_input(inptype[0], default)
199+
type_names = []
200+
for entry in inptype:
200201
value, e_comment = generate_example_input(entry, default)
201-
example.append(value)
202-
example.yaml_add_eol_comment(e_comment, index)
202+
if e_comment:
203+
type_names.append(e_comment)
204+
comment = "one of " + ", ".join(type_names)
203205
if optional:
204-
comment = "optional"
206+
comment = f"{comment} (optional)"
205207
elif isinstance(inptype, Mapping) and "type" in inptype:
206208
if inptype["type"] == "array":
207209
first_item = cast(MutableSequence[CWLObjectType], inptype["items"])[0]

tests/test_make_template.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@
88
def test_anonymous_record() -> None:
99
inputs = cmap([{"type": "record", "fields": []}])
1010
assert main.generate_example_input(inputs, None) == ({}, "Anonymous record type.")
11+
12+
13+
def test_union() -> None:
14+
"""Test for --make-template for a union type."""
15+
inputs = cmap(["string", "string[]"])
16+
assert main.generate_example_input(inputs, None) == (
17+
"a_string",
18+
'one of type "string", type "string[]"',
19+
)
20+
21+
22+
def test_optional_union() -> None:
23+
"""Test for --make-template for an optional union type."""
24+
inputs = cmap(["null", "string", "string[]"])
25+
assert main.generate_example_input(inputs, None) == (
26+
"a_string",
27+
'one of type "string", type "string[]" (optional)',
28+
)

0 commit comments

Comments
 (0)