From f1257e649ad93a1b841dbea5df2da9c9158b9fe0 Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Wed, 28 Sep 2022 16:35:42 -0700 Subject: [PATCH 1/6] Allow generation of connection URI to work when no conn type Previously if get_uri was called it would fail with `NoneType not iterable`, because of the check `if '-' in conn_type`. --- airflow/models/connection.py | 4 ++-- tests/always/test_connection.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/airflow/models/connection.py b/airflow/models/connection.py index da67410a394a9..7334e0f2fe9ae 100644 --- a/airflow/models/connection.py +++ b/airflow/models/connection.py @@ -206,13 +206,13 @@ def _parse_from_uri(self, uri: str): def get_uri(self) -> str: """Return connection in URI format""" - if "_" in self.conn_type: + if self.conn_type and "_" in self.conn_type: self.log.warning( "Connection schemes (type: %s) shall not contain '_' according to RFC3986.", self.conn_type, ) - uri = f"{str(self.conn_type).lower().replace('_', '-')}://" + uri = f"{str(self.conn_type or 'none').lower().replace('_', '-')}://" authority_block = "" if self.login is not None: diff --git a/tests/always/test_connection.py b/tests/always/test_connection.py index dac7bb110458f..99215b822b237 100644 --- a/tests/always/test_connection.py +++ b/tests/always/test_connection.py @@ -735,3 +735,6 @@ def test_extra_warnings_non_json(self): def test_extra_warnings_non_dict_json(self): with pytest.warns(DeprecationWarning, match="not parse as a dictionary"): Connection(conn_id="test_extra", conn_type="none", extra='"hi"') + + def test_get_uri_no_conn_type(self): + assert Connection().get_uri() == 'none://' From 88b5a3d25304f05fa9aaf9aebc2df069e36b913d Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Thu, 29 Sep 2022 20:27:11 -0700 Subject: [PATCH 2/6] remove redundancy --- airflow/models/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airflow/models/connection.py b/airflow/models/connection.py index 7334e0f2fe9ae..7ecc0e241d96b 100644 --- a/airflow/models/connection.py +++ b/airflow/models/connection.py @@ -212,7 +212,7 @@ def get_uri(self) -> str: self.conn_type, ) - uri = f"{str(self.conn_type or 'none').lower().replace('_', '-')}://" + uri = f"{str(self.conn_type).lower().replace('_', '-')}://" authority_block = "" if self.login is not None: From ef918695c098cc6cc7faba394899bc7f55e1dba1 Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Tue, 4 Oct 2022 13:44:55 -0700 Subject: [PATCH 3/6] update to output scheme-relative URI --- airflow/models/connection.py | 5 ++++- tests/always/test_connection.py | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/airflow/models/connection.py b/airflow/models/connection.py index 7ecc0e241d96b..7ea1e957add5e 100644 --- a/airflow/models/connection.py +++ b/airflow/models/connection.py @@ -212,7 +212,10 @@ def get_uri(self) -> str: self.conn_type, ) - uri = f"{str(self.conn_type).lower().replace('_', '-')}://" + if self.conn_type: + uri = f"{str(self.conn_type).lower().replace('_', '-')}://" + else: + uri = '//' authority_block = "" if self.login is not None: diff --git a/tests/always/test_connection.py b/tests/always/test_connection.py index 99215b822b237..fd6633b8305d4 100644 --- a/tests/always/test_connection.py +++ b/tests/always/test_connection.py @@ -737,4 +737,9 @@ def test_extra_warnings_non_dict_json(self): Connection(conn_id="test_extra", conn_type="none", extra='"hi"') def test_get_uri_no_conn_type(self): - assert Connection().get_uri() == 'none://' + # no conn type --> scheme-relative URI + assert Connection().get_uri() == '//' + # with host, still works + assert Connection(host='abc').get_uri() == '//abc' + # parsing back as conn still works + assert Connection(uri='//abc').host == 'abc' From e9b447276c8d807df9f5e0afa461742d9cba52b7 Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Thu, 29 Dec 2022 13:28:43 -0800 Subject: [PATCH 4/6] static --- tests/always/test_connection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/always/test_connection.py b/tests/always/test_connection.py index fd6633b8305d4..750b36ef53cfc 100644 --- a/tests/always/test_connection.py +++ b/tests/always/test_connection.py @@ -738,8 +738,8 @@ def test_extra_warnings_non_dict_json(self): def test_get_uri_no_conn_type(self): # no conn type --> scheme-relative URI - assert Connection().get_uri() == '//' + assert Connection().get_uri() == "//" # with host, still works - assert Connection(host='abc').get_uri() == '//abc' + assert Connection(host="abc").get_uri() == "//abc" # parsing back as conn still works - assert Connection(uri='//abc').host == 'abc' + assert Connection(uri="//abc").host == "abc" From aeb087504ee68c2c577b9639264ad18dc65bbb4c Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Thu, 29 Dec 2022 14:27:43 -0800 Subject: [PATCH 5/6] fix --- airflow/models/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airflow/models/connection.py b/airflow/models/connection.py index 7ea1e957add5e..7afe0f75bc40a 100644 --- a/airflow/models/connection.py +++ b/airflow/models/connection.py @@ -215,7 +215,7 @@ def get_uri(self) -> str: if self.conn_type: uri = f"{str(self.conn_type).lower().replace('_', '-')}://" else: - uri = '//' + uri = "//" authority_block = "" if self.login is not None: From 55b2763a81afda688cee63480761554a68fbd53e Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Thu, 29 Dec 2022 20:51:30 -0800 Subject: [PATCH 6/6] Update airflow/models/connection.py --- airflow/models/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airflow/models/connection.py b/airflow/models/connection.py index 7afe0f75bc40a..5f7406d9d4835 100644 --- a/airflow/models/connection.py +++ b/airflow/models/connection.py @@ -213,7 +213,7 @@ def get_uri(self) -> str: ) if self.conn_type: - uri = f"{str(self.conn_type).lower().replace('_', '-')}://" + uri = f"{self.conn_type.lower().replace('_', '-')}://" else: uri = "//"