vincbeck commented on code in PR #35723:
URL: https://github.com/apache/airflow/pull/35723#discussion_r1402324318


##########
airflow/models/connection.py:
##########
@@ -480,6 +481,34 @@ def get_connection_from_secrets(cls, conn_id: str) -> 
Connection:
     def to_dict(self) -> dict[str, Any]:
         return {"conn_id": self.conn_id, "description": self.description, 
"uri": self.get_uri()}
 
+    def to_json_dict(self, *, prune_empty: bool = False, validate: bool = 
True) -> dict[str, Any]:
+        """
+        Convert Connection to json-serializable dictionary.
+
+        :param prune_empty: Whether or not remove empty values.
+        :param validate: Validate is dictionary is JSON-serializable

Review Comment:
   ```suggestion
           :param validate: Validate dictionary is JSON-serializable
   ```



##########
docs/apache-airflow/howto/connection.rst:
##########
@@ -66,6 +66,43 @@ If serializing with JSON:
         }
     }'
 
+Generating a JSON connection representation
+"""""""""""""""""""""""""""""""""""""""""""
+
+.. versionadded:: 2.8.0
+
+
+To make connection JSON generation easier, the 
:py:class:`~airflow.models.connection.Connection` class has a
+convenience property :py:meth:`~airflow.models.connection.Connection.as_json`. 
It can be used like so:
+
+.. code-block:: pycon
+
+    >>> from airflow.models.connection import Connection
+    >>> c = Connection(
+    ...     conn_id="some_conn",
+    ...     conn_type="mysql",
+    ...     description="connection description",
+    ...     host="myhost.com",
+    ...     login="myname",
+    ...     password="mypassword",
+    ...     extra={"this_param": "some val", "that_param": "other val*"},
+    ... )
+    >>> print(f"AIRFLOW_CONN_{c.conn_id.upper()}='{c.as_json()}'")
+    AIRFLOW_CONN_SOME_CONN='{"conn_type": "mysql", "description": "connection 
description", "host": "myhost.com", "login": "myname", "password": 
"mypassword", "extra": {"this_param": "some val", "that_param": "other val*"}}'
+
+In additional same approach could be use for convert from URI Connection 
format to JSON

Review Comment:
   ```suggestion
   In addition, same approach could be used to convert Connection from URI 
format to JSON format
   ```



-- 
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]

Reply via email to