Skip to content

Commit

Permalink
fix: apply template_params on external_metadata (apache#14996)
Browse files Browse the repository at this point in the history
* fix: apply template_params on external_metadata

* Fix test
  • Loading branch information
betodealmeida authored Jun 5, 2021
1 parent f849f7e commit 9dec053
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,9 @@ def external_metadata(self) -> List[Dict[str, str]]:
db_engine_spec = self.db_engine_spec
if self.sql:
engine = self.database.get_sqla_engine(schema=self.schema)
sql = self.get_template_processor().process_template(self.sql)
sql = self.get_template_processor().process_template(
self.sql, **self.template_params_dict
)
parsed_query = ParsedQuery(sql)
if not db_engine_spec.is_readonly_query(parsed_query):
raise SupersetSecurityException(
Expand Down
19 changes: 19 additions & 0 deletions tests/datasource_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ def test_external_metadata_for_virtual_table(self):
session.delete(table)
session.commit()

def test_external_metadata_for_virtual_table_template_params(self):
self.login(username="admin")
session = db.session
table = SqlaTable(
table_name="dummy_sql_table_with_template_params",
database=get_example_database(),
sql="select {{ foo }} as intcol",
template_params=json.dumps({"foo": "123"}),
)
session.add(table)
session.commit()

table = self.get_table_by_name("dummy_sql_table_with_template_params")
url = f"/datasource/external_metadata/table/{table.id}/"
resp = self.get_json_resp(url)
assert {o.get("name") for o in resp} == {"intcol"}
session.delete(table)
session.commit()

def test_external_metadata_for_malicious_virtual_table(self):
self.login(username="admin")
table = SqlaTable(
Expand Down

0 comments on commit 9dec053

Please sign in to comment.