LuisSanchez commented on code in PR #36277:
URL: https://github.com/apache/superset/pull/36277#discussion_r2572324450
##########
superset/sqllab/api.py:
##########
@@ -234,7 +234,34 @@ def format_sql(self) -> FlaskResponse:
"""
try:
model = self.format_model_schema.load(request.json)
- result = SQLScript(model["sql"], model.get("engine")).format()
+ sql = model["sql"]
+ template_params = model.get("template_params")
+ database_id = model.get("database_id")
+
+ # Process Jinja templates if template_params and database_id are
provided
+ if template_params and database_id is not None:
+ database = DatabaseDAO.find_by_id(database_id)
+ if database:
+ try:
+ template_params = (
+ json.loads(template_params)
+ if isinstance(template_params, str)
+ else template_params
+ )
+ except json.JSONDecodeError:
+ logger.warning(
+ "Invalid template parameter %s. Skipping
processing",
+ str(template_params),
+ )
+ template_params = {}
+
+ if template_params:
+ template_processor =
get_template_processor(database=database)
+ sql = template_processor.process_template(
+ sql, **template_params
+ )
Review Comment:
Moved the `if` statement inside the `try` block as discussed, thanks!
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]