From 2498912f7d11f4f92591c33d06b62ea36d1481b6 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 28 Apr 2022 14:38:57 +0100 Subject: [PATCH 1/3] Skip non JSON-encodable values in ``params_to_strings()`` Fix the error reported in https://github.com/galaxyproject/galaxy/issues/13455 , which was due to BioMart sending some binary empty strings as values, e.g. for the `hsapiens_gene_ensembl__filter.chromosomal_region__file` key. N.B.: the BioMart tool still doesn't work after this fix (probably BioMart's fault), but at least Galaxy doesn't fail with the mysterious: "Error executing tool id 'biomart': Object of type bytes is not JSON serializable" --- lib/galaxy/tools/parameters/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/galaxy/tools/parameters/__init__.py b/lib/galaxy/tools/parameters/__init__.py index 03fd992ad404..dbff2694f474 100644 --- a/lib/galaxy/tools/parameters/__init__.py +++ b/lib/galaxy/tools/parameters/__init__.py @@ -1,7 +1,7 @@ """ Classes encapsulating Galaxy tool parameters. """ - +import logging from json import dumps from boltons.iterutils import remap @@ -19,6 +19,8 @@ ) from .grouping import Conditional, Repeat, Section, UploadDataset +log = logging.getLogger(__name__) + REPLACE_ON_TRUTHY = object() # Some tools use the code tag and access the code base, expecting certain tool parameters to be available here. @@ -205,14 +207,21 @@ def params_to_strings(params, param_values, app, nested=False, use_security=Fals Convert a dictionary of parameter values to a dictionary of strings suitable for persisting. The `value_to_basic` method of each parameter is called to convert its value to basic types, the result of which - is then json encoded (this allowing complex nested parameters and + is then JSON encoded (this allowing complex nested parameters and such). + If the value is not JSON-encodable, the key/value pair is skipped. """ rval = dict() for key, value in param_values.items(): if key in params: value = params[key].value_to_basic(value, app, use_security=use_security) - rval[key] = value if nested or value is None else str(dumps(value, sort_keys=True)) + if not nested and value is not None: + try: + value = dumps(value, sort_keys=True) + except Exception as e: + log.warning(f"Error while serializing value {value} for key {key}: {e}") + continue + rval[key] = value return rval From cb2ead59168f8b582662e5a36a59ce092fc4ae12 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Tue, 12 Apr 2022 13:24:55 +0200 Subject: [PATCH 2/3] Fix mypy error --- lib/galaxy/util/bool_expressions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/galaxy/util/bool_expressions.py b/lib/galaxy/util/bool_expressions.py index 488e86878499..14fb641225b3 100644 --- a/lib/galaxy/util/bool_expressions.py +++ b/lib/galaxy/util/bool_expressions.py @@ -14,7 +14,6 @@ from pyparsing import ( alphanums, CaselessKeyword, - Forward, infixNotation, Keyword, opAssoc, @@ -132,7 +131,7 @@ def __init__(self, evaluator: TokenEvaluator, token_format: Optional[str] = None action.evaluator = evaluator boolOperand = TRUE | FALSE | Word(token_format or DEFAULT_TOKEN_FORMAT) boolOperand.setParseAction(action) - self.boolExpr: Forward = infixNotation( + self.boolExpr = infixNotation( boolOperand, [ (NOT_OP, 1, opAssoc.RIGHT, BoolNot), From 97a9001a11cc5e121d3600c2103f463bdda0d279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20L=C3=B3pez?= <46503462+davelopez@users.noreply.github.com> Date: Tue, 12 Apr 2022 13:42:57 +0200 Subject: [PATCH 3/3] Add type hint suggestion Co-authored-by: Nicola Soranzo --- lib/galaxy/util/bool_expressions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/galaxy/util/bool_expressions.py b/lib/galaxy/util/bool_expressions.py index 14fb641225b3..162ca0f8562f 100644 --- a/lib/galaxy/util/bool_expressions.py +++ b/lib/galaxy/util/bool_expressions.py @@ -131,7 +131,7 @@ def __init__(self, evaluator: TokenEvaluator, token_format: Optional[str] = None action.evaluator = evaluator boolOperand = TRUE | FALSE | Word(token_format or DEFAULT_TOKEN_FORMAT) boolOperand.setParseAction(action) - self.boolExpr = infixNotation( + self.boolExpr: ParserElement = infixNotation( boolOperand, [ (NOT_OP, 1, opAssoc.RIGHT, BoolNot),