Skip to content

Commit

Permalink
Fix behavior of _ when searching for DAGs (#27448)
Browse files Browse the repository at this point in the history
On the homepage, when you search for DAGs, `_` wasn't behaving as most
people might expect since it is a single character wildcard in SQL. We
will automatically escape it now.
  • Loading branch information
jedcunningham committed Nov 1, 2022
1 parent 9b0ff3b commit eda0893
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions airflow/www/views.py
Expand Up @@ -654,9 +654,10 @@ def index(self):
dags_query = session.query(DagModel).filter(~DagModel.is_subdag, DagModel.is_active)

if arg_search_query:
escaped_arg_search_query = arg_search_query.replace("_", r"\_")
dags_query = dags_query.filter(
DagModel.dag_id.ilike('%' + arg_search_query + '%')
| DagModel.owners.ilike('%' + arg_search_query + '%')
DagModel.dag_id.ilike("%" + escaped_arg_search_query + "%", escape="\\")
| DagModel.owners.ilike("%" + escaped_arg_search_query + "%", escape="\\")
)

if arg_tags_filter:
Expand Down
10 changes: 9 additions & 1 deletion tests/www/views/test_views_home.py
Expand Up @@ -118,7 +118,7 @@ def client_single_dag(app, user_single_dag):
)


TEST_FILTER_DAG_IDS = ["filter_test_1", "filter_test_2", "a_first_dag_id_asc"]
TEST_FILTER_DAG_IDS = ["filter_test_1", "filter_test_2", "a_first_dag_id_asc", "filter.test"]


def _process_file(file_path, session):
Expand Down Expand Up @@ -185,6 +185,14 @@ def test_home_dag_list_filtered_singledag_user(working_dags, client_single_dag):
check_content_not_in_response(f"dag_id={dag_id}", resp)


def test_home_dag_list_search(working_dags, user_client):
resp = user_client.get("home?search=filter_test", follow_redirects=True)
check_content_in_response("dag_id=filter_test_1", resp)
check_content_in_response("dag_id=filter_test_2", resp)
check_content_not_in_response("dag_id=filter.test", resp)
check_content_not_in_response("dag_id=a_first_dag_id_asc", resp)


def test_home_robots_header_in_response(user_client):
# Responses should include X-Robots-Tag header
resp = user_client.get("home", follow_redirects=True)
Expand Down

0 comments on commit eda0893

Please sign in to comment.