Skip to content

Commit 5e00561

Browse files
committed
inputs_schema_gen: type fixes
1 parent 8fe819d commit 5e00561

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

cwl_utils/inputs_schema_gen.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
"""Generate JSON Schema from CWL inputs object."""
77
import argparse
8+
import hashlib
89
import json
910
import logging
1011
import sys
12+
from collections.abc import Sequence
1113
from contextlib import suppress
1214
from copy import deepcopy
1315
from importlib.resources import files
@@ -16,6 +18,7 @@
1618
from urllib.parse import urlparse
1719

1820
import requests
21+
from schema_salad.utils import json_dumps
1922

2023
from cwl_utils.loghandler import _logger as _cwlutilslogger
2124
from cwl_utils.parser import (
@@ -34,6 +37,7 @@
3437
cwl_v1_2,
3538
load_document_by_uri,
3639
)
40+
from cwl_utils.types import is_sequence
3741
from cwl_utils.utils import (
3842
get_value_from_uri,
3943
is_local_uri,
@@ -252,7 +256,11 @@ def generate_json_schema_property_from_input_parameter(
252256
"""
253257
# Get the input name and documentation for description
254258
input_name = get_value_from_uri(str(input_parameter.id))
255-
doc = input_parameter.doc
259+
doc = (
260+
"\n".join(input_parameter.doc)
261+
if is_sequence(input_parameter.doc)
262+
else input_parameter.doc
263+
)
256264
required = get_is_required_from_input_parameter(input_parameter)
257265

258266
return JSONSchemaProperty(
@@ -263,27 +271,27 @@ def generate_json_schema_property_from_input_parameter(
263271
)
264272

265273

266-
def generate_definition_from_schema(schema: InputRecordSchema) -> dict[str, Any]:
267-
"""
268-
Given a schema, generate a JSON schema definition.
274+
def generate_definition_from_schema(
275+
schema: InputRecordSchema | InputArraySchema | InputEnumSchema,
276+
) -> dict[str, Any]:
277+
"""Given a schema, generate a JSON schema definition."""
278+
# TODO: handle InputArraySchema & InputEnumSchema (from SchemaDefRequirement.types)
269279

270-
:param schema:
271-
:return:
272-
"""
273280
# Sanitise each field of the schema
274281
sanitised_fields = {}
275282

276-
if schema.fields is None:
277-
return {}
283+
if isinstance(schema, InputRecordSchema):
284+
if schema.fields is None:
285+
return {}
278286

279-
for field in schema.fields:
280-
sanitised_fields.update(
281-
{
282-
get_value_from_uri(field.name): sanitise_schema_field(
283-
{"type": field.type_}
284-
)
285-
}
286-
)
287+
for field in schema.fields:
288+
sanitised_fields.update(
289+
{
290+
get_value_from_uri(field.name): sanitise_schema_field(
291+
{"type": field.type_}
292+
)
293+
}
294+
)
287295

288296
# Generate JSON properties
289297
property_list = []
@@ -316,16 +324,25 @@ def generate_definition_from_schema(schema: InputRecordSchema) -> dict[str, Any]
316324
)
317325
property_list.append(prop)
318326

327+
if not isinstance(schema, cwl_v1_0.InputArraySchema) or hasattr(schema, "name"):
328+
schema_name = to_pascal_case(get_value_from_uri(str(schema.name)))
329+
else:
330+
schema_name = (
331+
"AnonymousInputArraySchema"
332+
+ hashlib.sha1( # nosec
333+
json_dumps(schema.save()).encode("utf-8")
334+
).hexdigest()
335+
)
319336
return {
320-
to_pascal_case(get_value_from_uri(str(schema.name))): {
337+
schema_name: {
321338
"type": "object",
322339
"properties": {prop.name: prop.type_dict for prop in property_list},
323340
"required": [prop.name for prop in property_list if prop.required],
324341
}
325342
}
326343

327344

328-
def cwl_to_jsonschema(cwl_obj: Workflow | CommandLineTool) -> Any:
345+
def cwl_to_jsonschema(cwl_obj: Workflow | CommandLineTool) -> dict[str, object]:
329346
"""
330347
cwl_obj: A CWL Object.
331348
@@ -386,11 +403,9 @@ def get_complex_schema_values(idx_iter: str) -> InputRecordSchema:
386403

387404
return input_record_schema
388405

389-
complex_schema_values: list[InputRecordSchema] = list(
390-
map(
391-
get_complex_schema_values,
392-
complex_schema_keys,
393-
)
406+
complex_schema_values: map[InputRecordSchema] = map(
407+
get_complex_schema_values,
408+
complex_schema_keys,
394409
)
395410

396411
# Load in all $imports to be referred by complex input types
@@ -415,11 +430,9 @@ def get_complex_schema_values(idx_iter: str) -> InputRecordSchema:
415430
)
416431

417432
workflow_schema_definitions_list.extend(
418-
list(
419-
map(
420-
generate_definition_from_schema,
421-
schema_def_requirement.types,
422-
)
433+
map(
434+
generate_definition_from_schema,
435+
schema_def_requirement.types,
423436
)
424437
)
425438

@@ -432,7 +445,7 @@ def get_complex_schema_values(idx_iter: str) -> InputRecordSchema:
432445
properties = list(
433446
map(
434447
generate_json_schema_property_from_input_parameter,
435-
cwl_obj.inputs,
448+
cast(Sequence[WorkflowInputParameter], cwl_obj.inputs),
436449
)
437450
)
438451

0 commit comments

Comments
 (0)