bjoernpollex-sc commented on a change in pull request #4743: [AIRFLOW-3871]
render Operators template fields recursively
URL: https://github.com/apache/airflow/pull/4743#discussion_r262390862
##########
File path: airflow/models/__init__.py
##########
@@ -2452,9 +2454,35 @@ def render_template_from_field(self, attr, content,
context, jinja_env):
k: rt("{}[{}]".format(attr, k), v, context)
for k, v in list(content.items())}
else:
- result = content
+ result = self._render_template_object_recursively(content, context)
return result
+ def _render_template_object_recursively(self, content, context):
+ if id(content) not in self._rendered_template_object_ids:
+ self._rendered_template_object_ids.add(id(content))
+ try:
+ content_attributes = vars(content)
+ except TypeError:
+ # content has no __dict__ (e.g.: could be an int)
+ return content
+
+ for attribute, value in content_attributes.items():
+ try:
+ # Best effort approach here:
+ # try to render attribute value as a template,
+ # and try to set this attribute with the rendering result
+ # It may fail sometimes
+ # (e.g.: UUID has an int attribute but is immutable; see
UUID.__settattr__ )
+ # In case of failure, just let this attribute unchanged.
+ setattr(content,
+ attribute,
+ self.render_template(attribute, value, context))
+ except Exception:
Review comment:
I would strongly prefer more granular error handling here. Specifically, any
exception raised from `render_template` should be propagated out.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services