diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1ca9049fc8..4715252e39 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,10 @@ Fixed Contributed by @momokuri-3 +* Fix exception thrown if action parameter contains {{ or {% and no closing jinja characters. #5556 + + contributed by @guzzijones12 + Added ~~~~~ diff --git a/st2common/st2common/util/param.py b/st2common/st2common/util/param.py index 52e1f025fd..90617a78d9 100644 --- a/st2common/st2common/util/param.py +++ b/st2common/st2common/util/param.py @@ -19,7 +19,7 @@ import six import networkx as nx -from jinja2 import meta +from jinja2 import meta, exceptions from oslo_config import cfg from st2common import log as logging from st2common.util.config_loader import get_config @@ -133,19 +133,24 @@ def _process(G, name, value): ) or jinja_utils.is_jinja_expression(complex_value_str) if is_jinja_expr: - G.add_node(name, template=value) - - template_ast = ENV.parse(value) - LOG.debug("Template ast: %s", template_ast) - # Dependencies of the node represent jinja variables used in the template - # We're connecting nodes with an edge for every depencency to traverse them - # in the right order and also make sure that we don't have missing or cyclic - # dependencies upfront. - dependencies = meta.find_undeclared_variables(template_ast) - LOG.debug("Dependencies: %s", dependencies) - if dependencies: - for dependency in dependencies: - G.add_edge(dependency, name) + try: + template_ast = ENV.parse(value) + G.add_node(name, template=value) + + LOG.debug("Template ast: %s", template_ast) + # Dependencies of the node represent jinja variables used in the template + # We're connecting nodes with an edge for every depencency to traverse them + # in the right order and also make sure that we don't have missing or cyclic + # dependencies upfront. + dependencies = meta.find_undeclared_variables(template_ast) + LOG.debug("Dependencies: %s", dependencies) + if dependencies: + for dependency in dependencies: + G.add_edge(dependency, name) + except exceptions.TemplateSyntaxError: + G.add_node(name, value=value) + # not jinja after all + # is_jinga_expression only checks for {{ or {{% for speed else: G.add_node(name, value=value) diff --git a/st2common/tests/unit/test_param_utils.py b/st2common/tests/unit/test_param_utils.py index c2e5810815..77c88b1cd3 100644 --- a/st2common/tests/unit/test_param_utils.py +++ b/st2common/tests/unit/test_param_utils.py @@ -54,6 +54,26 @@ class ParamsUtilsTest(DbTestCase): action_system_default_db = FIXTURES["actions"]["action_system_default.yaml"] runnertype_db = FIXTURES["runners"]["testrunner1.yaml"] + def test_process_jinja_exception(self): + + action_context = {"api_user": "noob"} + config = {} + G = param_utils._create_graph(action_context, config) + name = "a1" + value = {"test": "http://someurl?value={{a"} + param_utils._process(G, name, value) + self.assertEquals(G.nodes.get(name, {}).get("value"), value) + + def test_process_jinja_template(self): + + action_context = {"api_user": "noob"} + config = {} + G = param_utils._create_graph(action_context, config) + name = "a1" + value = "http://someurl?value={{a}}" + param_utils._process(G, name, value) + self.assertEquals(G.nodes.get(name, {}).get("template"), value) + def test_get_finalized_params(self): params = { "actionstr": "foo",